Kero
Kero

Reputation: 1944

export multiple json objects to xlsx file using alasql

first time using alasql, and sql in general, however trying to export multiple json objects to xlsx file i am using

  alasql('SELECT * INTO XLSX("Report.xlsx",{headers:true}) FROM ?', [$scope.children, $scope.answer, $scope.optional, $scope.user]);

which works fine only for first object in array ($scope.children), but never show up rest of these objects. Is there any clue how i can add all of these object to only one table.

also i tried to merge all objects to only one object, but didn't work, i only get table header right, however it didn't show the right data it only shows (Object object) inside table cells.

Upvotes: 1

Views: 2719

Answers (1)

agershun
agershun

Reputation: 4107

What result do you expect in the Excel file?

If this is multiple sheet file with different sheets for each array it should be

 var opts = [{sheetid:'Children'},{sheetid:'Answer'}, 
    {sheetid:'Optional'},{sheetid:'User'}];

 alasql('SELECT INTO XLSX("Report.xlsx") FROM ?', 
      [opts,[$scope.children, $scope.answer, $scope.optional, $scope.user]]);

In case if you want to merge all these arrays into one array, you need to prepare it first, for example:

var arr = [];
$scope.children.forEach(function(ch,idx){
    arr.push({child:$scope.children[idx], answer:$scope.answer[idx],
                  optional:$scope.optional[idx],$scope.user[idx]
    });
});

alasql('SELECT * INTO XLSX("Report.xlsx") FROM ?',[arr]);

If you can provide more details about the structure of $scope.children, $scope.answer, and other objects (or arrays) I can adjust the answer to your needs.

Upvotes: 0

Related Questions