Search code examples
angularjsexport-to-excelcustom-headersalasql

field name "action" cause issues alasql export to Excel


I am using alasql to export to excel and I am trying to do custom headers like this below. All other field names are fine except when it is "action"

function myCtrl($scope) {
$scope.exportData = function () {
    alasql('SELECT name as MY_NAME, action as MY_ACTION INTO XLSX("john.xlsx",{headers:true}) FROM ?',[$scope.items]);
};

$scope.items = [{
    name: "John Smith",
    action: "[email protected]",
    dob: "1985-10-10"
}, {
    name: "Jane Smith",
    action: "[email protected]",
    dob: "1988-12-22"
}, {
    name: "Jan Smith",
    action: "[email protected]",
    dob: "2010-01-02"
}, {
    name: "Jake Smith",
    action: "[email protected]",
    dob: "2009-03-21"
}, {
    name: "Josh Smith",
    action: "[email protected]",
    dob: "2011-12-12"
}, {
    name: "Jessie Smith",
    action: "[email protected]",
    dob: "2004-10-12"
}];
};

is giving me this below error

Error: Parse error on line 1:
...CT name as MY_NAME, action as MY_ACTION 
-----------------------^
Expecting 'LITERAL', 'BRALITERAL', 'LPAR', 'NUMBER', 'STRING', 'SHARP',     'DOLLAR', 'AT', 'COLON', 'NOT', 'PLUS', 'STAR', 'QUESTION', 'CURRENT_TIMESTAMP', 'JAVASCRIPT', 'NEW', 'CAST', 'CONVERT', 'SUM', 'COUNT', 'MIN', 'MAX', 'AVG', 'FIRST', 'LAST', 'AGGR', 'ARRAY', 'TRUE', 'FALSE', 'NSTRING', 'NULL', 'EXISTS', 'BRAQUESTION', 'CASE', 'MINUS', 'ATLBRA', 'LCUR', got 'ACTION'
at Object.parseError (alasql.min.js:3)
at Object.parse (alasql.min.js:3)
at Function.alasql.parse (alasql.min.js:3)`

Solution

  • action is an alasql keyword. To resolve this issue I wrapped action with [] like below

    "[action] AS MY_ACTION,"
    

    This solved that issue