How to set custom header names with ALASQL and XLSX

Luis Crespo picture Luis Crespo · Jun 12, 2015 · Viewed 8.3k times · Source

I'm exporting some tables to excel, using angular, alasql and xlsx. I'm using it as follows:

var options = {
    headers: true,
    sheetid: 'users',
    columns: [{
      columnid: 'a',
      title: 'Username'
    }, {
      columnid: 'b',
      title: 'First name'
    }, {
      columnid: 'c',
      title: 'Last name'
    }]
  };

alasql('SELECT * INTO XLSX("test.xlsx", ?) FROM ?', [options, $scope.users]);

I was expecting the columnns option to customize my table headers. But it ain't doing it.

Any clue why?

Answer

Aquiles picture Aquiles · Feb 18, 2016

Sometimes you want to use Headers including blank spaces (Separated By) or... using headers that are reserved words (Deleted), in both cases you can use [] like this:

alasql('SELECT firstName AS FirstName, [Deleted] AS [Erased], Separated AS [Separated By] INTO XLSX("test.xlsx", ?) FROM ?', [options, $scope.users]);