【问题标题】:Fetch not out-putting all data to Google sheet获取不将所有数据输出到谷歌表
【发布时间】:2021-05-09 21:50:00
【问题描述】:

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

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

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

但是如果它在 JSON 中添加所有数据应该是这样的

您可以看到 NR 的列未添加到工作表中

这是我的 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 中的所有数据方面提供的任何帮助

【问题讨论】:

    标签: javascript google-apps-script google-sheets fetch


    【解决方案1】:

    我的建议是替换

    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)
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多