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

xyz*_*333 2 javascript fetch google-sheets google-apps-script

我正在使用 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
}
Run Code Online (Sandbox Code Playgroud)

这是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 中所有数据的函数的任何帮助

Mik*_*son 5

我的建议是更换

var connectionKeys = Object.keys(responseJson.connections[0]); 
Run Code Online (Sandbox Code Playgroud)

经过

  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)
  })
Run Code Online (Sandbox Code Playgroud)

  • 哇,那太好了!谢谢迈克!! (2认同)