1

js in my angular js project to export a grid to excel, heres my code:

 inventaire.exportMyDataVille = function(data) {

        var city='Safi';
        alasql('SELECT * INTO XLSX("data.xlsx",{headers:true}) FROM ? WHERE secteur='+city+' GROUP BY secteur,agence,serie', [inventaire.myDataSource]);
    };

what im trying to do is to pass a parametre in the alasql but it gives a empty excel file??is there somthing wrong in the request

  • alasql('SELECT * INTO XLSX("data.xlsx",{headers:true}) FROM ? as dummy WHERE dummy.secteur='+city+' GROUP BY dummy.secteur,agence,serie', [inventaire.myDataSource]); – Orange Dec 26 '16 at 11:24

2 Answers2

1

You need to wrap string values in quotes when applying them in SQL clauses.

Based on your previous choice, use double quotes as follows:

alasql('SELECT * INTO XLSX("data.xlsx",{headers:true}) FROM ? WHERE secteur="'+city+'" GROUP BY secteur,agence,serie', [inventaire.myDataSource]);.

Note the change from '+city+' to "'+city+'"

Phil Cap
  • 209
  • 2
  • 10
  • That really helped me!!! I was trying to do something like this: (... WHERE name LIKE "%?%"',[name]) and it wasn't working. I did this: (... WHERE name like "%' + $scope.name + '%"') – Lanlan82 Jan 24 '18 at 13:05
  • This might be vulnerable to SQL-injection attacks. Isn't there a alasql way of doing this? – svenema Mar 11 '20 at 15:56
1

In this case, the alasql function requires the format ( query, [[data], argument, argument, argumentN] )

So it should be:

alasql('... FROM ? WHERE secteur = ? ...', [inventaire.myDataSource, city]);

inventaire.myDataSource must follow the format [{secteur:'aaa'}, {secteur:'bbb'}, {secteur:'ccc'}]