以可下载格式转换为CSV

时间:2017-02-20 11:26:03

标签: javascript arrays

我想将一组对象转换为CSV。

请查看代码。我做错了什么?当我从console.log("csv",array)传递printAccountsGa时,我得到一个数组。此外,我已经在全局声明了JSONGA = [],但我仍然得到了包含安慰的每个对象的数组。但当我将其传递给csvConverter函数时,它会显示undefined

function printAccountsGa(results) {
  if (results && !results.error) {
    var accounts = results.items;
    for (var i = 0, account; account = accounts[i]; i++) {
      //console.log('Account Id: ' + account.id);
      listProperties(account.id);
      //console.log('Account Name: ' + account.name);
    }
    console.log(JSONGA);
    csvConverter(JSONGA);
  }
}

function printAccountsGtm(results) {
    console.log("result is",results);
    if (results && !results.error) {
    var accounts = results.accounts;
    console.log(accounts);
    for (var i = 0, account; account = accounts[i]; i++) {
      //console.log('Account Id: ' + account.accountId);
      listContainers(account.accountId);
      //console.log('Account Name: ' + account.name);
    }
  }
}

function listProperties(accountID){
  var request = gapi.client.analytics.management.webproperties.list({
    'accountId': accountID
  });
  request.execute(printProperties);
}

function printProperties(results){
  if (results && !results.error) {
    var properties = results.items;
    //console.log(properties[0].accountId);
    $.each(properties,function(element){
      //console.log(properties[element].accountId);
      var tempObj={};
      tempObj={
        'AccountID':properties[element].accountId,
        'PropertyID':properties[element].id,
        'name':properties[element].name,
      }
      JSONGA.push(tempObj);
    })
 }
}






function csvConverter(array){
  console.log("csv",array);
   var result = array.map(function(d){
        return JSON.stringify(values(d));
    })
    .join('\n') 
    .replace(/(^\[)|(\]$)/mg, '');

  window.open('data:text/csv;charset=utf-8,' + escape(result));
}


window.onload = function(){
  var el = document.getElementById('auth-button-ga');
  el.addEventListener('click', authorizeGa);
  var elt = document.getElementById("auth-button-gtm");
  elt.addEventListener('click', authorizeGtm);
}

0 个答案:

没有答案