Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom header names with ALASQL and XLSX

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?

like image 529
Luis Crespo Avatar asked Dec 06 '22 21:12

Luis Crespo


2 Answers

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]);
like image 166
Aquiles Avatar answered Dec 17 '22 06:12

Aquiles


I managed to customize the headers by using plain SQL:

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

That worked, the header for the firstName would be FirstName.

like image 36
Luis Crespo Avatar answered Dec 17 '22 07:12

Luis Crespo