简体   繁体   English

使用alasql导出Excel工作表时映射键值对

[英]map key value pairs while export excel sheet using alasql

I am working on export into excel sheet requirement using 'alasql'. 我正在使用'alasql'导出到excel工作表要求中。

My Javasript object to be given as input to alasql is 我要作为alasql输入提供的Javasript对象是

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 下面是我的alasql脚本要导出到excel工作表

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”键值和标头,如下所示:

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

JS Includes: JS包括:

<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. 谢谢。

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. 我已将其修复,因为首先我尝试通过直接给出不正确的方法来提供javascript对象,因此,我已将javascript对象转换为数组,然后转换为对象,而且该数组也应位于每个数组的key:value对中对象迭代。 I think you may be confused little bit but after watching it you will get clarity. 我认为您可能会有些困惑,但是观看之后您会变得清晰。 My array-object is like below: 我的数组对象如下所示:

arrayToExport: 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: 我的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM