获取不将所有数据输出到Google表格

我正在使用 Fetch 将 JSON 数据输出到 Google 表格

Fetch 函数未将 JSON 中的所有数据输出到工作表

这是我运行 Fetch 函数时得到的结果

但是如果它在 JSON 中添加所有数据,它应该是什么

你可以看到,列N通过R没有被添加到表

这是我的 Fetch 功能

function fetchData() {
  // Set Fetch Variables
  var url = "https://sum-app.net/projects/13435720200118544/download_data/combined_json"
  var response     = UrlFetchApp.fetch(url);
  var responseText = response.getContentText();
  var responseJson = JSON.parse(responseText); 

  var connectionKeys = Object.keys(responseJson.connections[0]); 
  var data = responseJson.connections.map(e => connectionKeys.map(f => {
      return e[f] instanceof Array ? e[f].join("|") : e[f];
  })); 
  data.unshift(connectionKeys);
  data = data.map(x => x.map(y => typeof y === 'undefined' || null ? "" : y));

  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  sh.clearContents();//clear sheet
  sh.getRange(1,1,data.length,data[0].length).setValues(data);//load the data
}

这是JSON:
https://sum-app.net/projects/13435720200118544/download_data/combined_json

如果有帮助,这里有一个带有 Fetch 功能的 Google 表格
https://docs.google.com/spreadsheets/d/1CuP2DDO-rB1henrMAurBgb-CYmh7RNBbSHOjF6BqG5c/edit?usp=sharing

在此先感谢您对获取fetchData输出 JSON 中所有数据的函数的任何帮助

回答

我的建议是更换

var connectionKeys = Object.keys(responseJson.connections[0]); 

经过

  let myDico = new Map()
  responseJson.connections.forEach(function(key){
    Object.keys(key).forEach(function(item){
       myDico.set(item,'')
    })
  })
  var connectionKeys = []
  myDico.forEach(function(value, key) {
    connectionKeys.push(key)
  })

  • wow, that works great! Thank you Mike!!

以上是获取不将所有数据输出到Google表格的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>