使用 fetch(url) 和 json 数据有数组对象,输出数组作为分隔字符串
Using fetch(url) and the json data has array object, output array as a delimited string
我有一个脚本可以使用 fetch(URL)
将 json 解析为 Google sheet
来自@Tanaike,脚本运行良好,除非 json 数据是数组
例如
"elements": [
{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
}
因此,在此示例中,"Issues Checkbox"
是一个字符串值数组
当数组在 json 数据中时,脚本的输出变为:
1).空数组
"Issues Checkbox": ""
输出为空单元格值
2).数组中的单个对象:
"Issues Checkbox": [
"Option 1"
]
输出的是对象,所以单元格值为Option 1
3).数组中的多个对象:输出为空单元格值`
"Issues Checkbox": [
"Option 1",
"Option 2",
"Option 3",
.,
.,
.,
"Option n"
]
输出为空单元格值
我需要单元格值是数组对象的管道分隔字符串
Option 1|Option 2|Option 3| ....|Option n
谢谢
// =GETCONNECTIONS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETCONNECTIONS(url) {
var response = UrlFetchApp.fetch(url);
var responseText = response.getContentText();
var responseJson = JSON.parse(responseText);
var connectionKeys = Object.keys(responseJson.connections[0]);
// At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
// var connectionKeys = ["Id","From","To","Name From","Name To","Initial Date","Last Date","Type","Weight","Checkbox ZZZ","Text Area","Radio AAA","Select bbb"];
var data = responseJson.connections.map(e => connectionKeys.map(f => e[f]));
data.unshift(connectionKeys);
return data;
}
// =GETELEMENTS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETELEMENTS(url) {
var response = UrlFetchApp.fetch(url);
var responseText = response.getContentText();
var responseJson = JSON.parse(responseText);
var elementKeys = Object.keys(responseJson.elements[0]);
// At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
// var elementKeys = ["Id","Type","Label","First Name","Last Name","Description","Segment","Image","Project Name","Initial Date","Last Date","Issues Checkbox","IssuesRadio","Notes"];
var data = responseJson.elements.map(e => elementKeys.map(f => e[f]));
data.unshift(elementKeys);
return data;
}
您好,我认为您可以继续前进。您想在将其写入 sheet.
之前执行此操作
let api =
{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
};
const checkboxes = api["Issues Checkbox"];
const arr = [];
const array = checkboxes.forEach(box => {
arr.push(box);
})
api["Issues Checkbox"] = arr.join(" | ");
console.log(api);
“问题复选框”中的数组无法作为数组传递。一个选项是将它们作为以逗号分隔的一串元素传递。一种方法是使用 join
.
var responseJson = {
"elements": [{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
}]
}
var elementKeys = Object.keys(responseJson.elements[0]);
var data = responseJson.elements.map(e => elementKeys.map(f => {
return e[f] instanceof Array ? e[f].join(',') : e[f];
}));
data.unshift(elementKeys);
console.log(data)
我有一个脚本可以使用 fetch(URL)
将 json 解析为 Google sheet
来自@Tanaike,脚本运行良好,除非 json 数据是数组
例如
"elements": [
{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
}
因此,在此示例中,"Issues Checkbox"
是一个字符串值数组
当数组在 json 数据中时,脚本的输出变为:
1).空数组
"Issues Checkbox": ""
输出为空单元格值
2).数组中的单个对象:
"Issues Checkbox": [
"Option 1"
]
输出的是对象,所以单元格值为Option 1
3).数组中的多个对象:输出为空单元格值`
"Issues Checkbox": [
"Option 1",
"Option 2",
"Option 3",
.,
.,
.,
"Option n"
]
输出为空单元格值
我需要单元格值是数组对象的管道分隔字符串
Option 1|Option 2|Option 3| ....|Option n
谢谢
// =GETCONNECTIONS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETCONNECTIONS(url) {
var response = UrlFetchApp.fetch(url);
var responseText = response.getContentText();
var responseJson = JSON.parse(responseText);
var connectionKeys = Object.keys(responseJson.connections[0]);
// At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
// var connectionKeys = ["Id","From","To","Name From","Name To","Initial Date","Last Date","Type","Weight","Checkbox ZZZ","Text Area","Radio AAA","Select bbb"];
var data = responseJson.connections.map(e => connectionKeys.map(f => e[f]));
data.unshift(connectionKeys);
return data;
}
// =GETELEMENTS("https://sum-app.net/projects/14312620200623668/download_data/kumu_json")
function GETELEMENTS(url) {
var response = UrlFetchApp.fetch(url);
var responseText = response.getContentText();
var responseJson = JSON.parse(responseText);
var elementKeys = Object.keys(responseJson.elements[0]);
// At JSON object, the order of keys are not guaranteed. So the following array including the keys might be suitable.
// var elementKeys = ["Id","Type","Label","First Name","Last Name","Description","Segment","Image","Project Name","Initial Date","Last Date","Issues Checkbox","IssuesRadio","Notes"];
var data = responseJson.elements.map(e => elementKeys.map(f => e[f]));
data.unshift(elementKeys);
return data;
}
您好,我认为您可以继续前进。您想在将其写入 sheet.
之前执行此操作let api =
{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
};
const checkboxes = api["Issues Checkbox"];
const arr = [];
const array = checkboxes.forEach(box => {
arr.push(box);
})
api["Issues Checkbox"] = arr.join(" | ");
console.log(api);
“问题复选框”中的数组无法作为数组传递。一个选项是将它们作为以逗号分隔的一串元素传递。一种方法是使用 join
.
var responseJson = {
"elements": [{
"Id": 49645,
"Type": "Person",
"Label": "Sally Yager",
"First Name": "Sally",
"Last Name": "Yager",
"Description": "",
"Segment": "555",
"Image": null,
"Project Name": "test222",
"Initial Date": "09/29/2020 17:44",
"Last Date": "09/29/2020 17:47",
"Issues Checkbox": [
"Option 1",
"Option 6"
],
"IssuesRadio": "Option 3",
"Notes": "222"
}]
}
var elementKeys = Object.keys(responseJson.elements[0]);
var data = responseJson.elements.map(e => elementKeys.map(f => {
return e[f] instanceof Array ? e[f].join(',') : e[f];
}));
data.unshift(elementKeys);
console.log(data)