0

I am working on export into excel sheet requirement using 'alasql'.

My Javasript object to be given as input to alasql is

0:
  ContactEmail: "email1@example.com"
  ContactName: "abcd"
  CustomerName: "defg"
  SubdomainName: "adasdasd"
1: 
  ContactEmail: "email2@example.com"
  ContactName: "abcd"
  CustomerName: "defg"
  SubdomainName: "adasdasd"
2: 
  ContactEmail: "email3@example.com"
  ContactName: "abcd"
  CustomerName: "defg"
  SubdomainName: "adasdasd"

below is my alasql script to export into excel sheet

var sheet_name = 'clients.xlsx'
alasql('SELECT * INTO XLSX("'+sheet_name+'",{headers:true}) FROM ?', arrayToExport);

My probem here is, it is exporting only the first key that is '0' & '1' key values and headers like below:

      0          1
CustomerName    name1
ContactName     contact1
ContactEmail    email1@example.com
SubdomainName   adasdasd

JS Includes:

<script src="{{ asset(auto_version('public/js/alasql.min.js')) }}"></script>
<script src="{{ asset(auto_version('public/js/alasql_xlsx.js')) }}"></script>

Can anyone please assist me in this. Thanks.

Prasad Patel
  • 707
  • 3
  • 16
  • 53

1 Answers1

1

I have fixed this issue. I hope this may be helpful for others. I have fixed it as first I have tried by giving directly a javascript object which is not the right way so, I have converted the javascript object to array and then object, also the array should be in a key:value pair for each array-object iteration. I think you may be confused little bit but after watching it you will get clarity. My array-object is like below:

arrayToExport:

[Array(11)]
0: Array(11)
   0: {CustomerName: "CName1", ContactName: "contact1", ContactEmail: "email1@example.com", SubdomainName: "domain1", Region: "USA", …}
   1: {CustomerName: "CName2", ContactName: "contact2", ContactEmail: "email2@example.com", SubdomainName: "domain2", Region: "USA", …}
   2: {CustomerName: "CName3", ContactName: "contact3", ContactEmail: "email3@example.com", SubdomainName: "domain3", Region: "USA", …}
   3: {CustomerName: "CName4", ContactName: "contact4", ContactEmail: "email4@example.com", SubdomainName: "domain4", Region: "USA", …}
   4: {CustomerName: "Sudhakar", ContactName: "contact5", ContactEmail: "email5@example.com", SubdomainName: "domain5", Region: "USA", …}
   5: { …}

My Javascript:

$scope.doExport = function(list){
  var sheet_name = 'clients.xlsx'
  var arrayToExport = {};
  var arrData = [];
  $scope.list = list;
  var i = 0;
  angular.forEach($scope.list, function(value, key){
      var status = (value.status == 0) ? 'Pending' : 'Active';
      var region = value.region;
      region = region.toUpperCase();
      var initial = value.subscr_end_date.split(/\-/);
      var finalDate = [ initial[1], initial[2], initial[0] ].join('/'); 
      //console.log( finalDate ); 
      arrData[i] = {CustomerName:value.client_info.first_name,ContactName:value.client_info.last_name, ContactEmail:value.client_info.email,SubdomainName:value.sub_domain_name,Region:region,Status:status,SubscriptionEndDate:finalDate
            };
    i++;
  });
  arrayToExport = [arrData];
  console.log(arrayToExport);
  alasql('SELECT * INTO XLSX("'+sheet_name+'",{headers:true}) FROM ?', arrayToExport);
}
Prasad Patel
  • 707
  • 3
  • 16
  • 53