1

I'm using this function to sort and export data to excel

window.exportData = function () {
     data.sort(function(a, b){
        return a.destino - b.destino;
        });
     alasql("SELECT * INTO XLSX('cities.xlsx',{headers:true}) FROM ? ",  [data]);

it works well. I need to change the name 'cities.xlsx' for a name like, for example,

var filename = 'cities'+variable+'.xlsx';

and obtain

alasql("SELECT * INTO XLSX("filename",{headers:true}) FROM ? ",[data]);

in order to avoid confusions with saved files in excel.

Thank you in advance.

RobertoFRey
  • 674
  • 1
  • 5
  • 10

2 Answers2

0

You can create SQL query as usual JavaScript string, like below:

 alasql("SELECT * INTO XLSX('"+filename+"',{headers:true}) FROM ? ",  [data]);
agershun
  • 4,077
  • 38
  • 41
  • var filename = 'cities'+h+g+'.xlsx'; alasql("SELECT * INTO XLSX('"filename"',{headers:true}) FROM ? ",[data]); – RobertoFRey Aug 02 '15 at 20:07
  • I have this variable: var filename = 'puertos'+h+g+'.xlsx'; what do I have to write in the blank alasql("SELECT * INTO XLSX( ,{headers:true}) FROM ? ",[data]); because I have tried many variants but it doesn't work. – RobertoFRey Aug 02 '15 at 20:17
  • 1
    I got the solution: window.exportData = function () { data.sort(function(a, b){ return a.destino - b.destino; }); var filename = 'puertos'+h+g+'.xlsx'; alasql("SELECT * INTO XLSX('"+filename+"',{headers:true}) FROM ? ",[data]); data.length = 0; } I made a mistake with the filename. – RobertoFRey Aug 02 '15 at 22:28
0

var filename = 'myFile.xlsx';

alasql("SELECT * INTO XLSX( ? ,{headers:true}) FROM ? ", [filename, data]);

I think this is the better solution like the way in sql world.