xyz*_*333 2 javascript fetch google-sheets google-apps-script
我正在使用 Fetch 将 JSON 数据输出到 Google 表格
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 中所有数据的函数的任何帮助
我的建议是更换
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)
| 归档时间: |
|
| 查看次数: |
56 次 |
| 最近记录: |