[英]apps script parse nested json to sheets
几天来,我一直在努力寻找最优雅的方法来解析 Json 字符串并将其写入谷歌表格。 它源自 woocommerce 的 webhook。
我有一个 Json ,如下所示:
"product_variations": [
{
"id": 47131,
"on_sale": false,
"regular_price": 1678,
"sale_price": 0,
"sku": "GexKiPe-Linen",
"quantity": "",
"stock": null,
"attributes": [
{
"name": "color",
"slug": "pa_color",
"option": "linen"
},
{
"name": "material",
"slug": "pa_material",
"option": "permanent"
}
]
},
{
"id": 47132,
"on_sale": false,
"regular_price": 1678,
"sale_price": 0,
"sku": "GexKiPe-Heather Beige",
"quantity": "",
"stock": null,
"attributes": [
{
"name": "color",
"slug": "pa_color",
"option": "heather-beige"
},
{
"name": "material",
"slug": "pa_material",
"option": "permanent"
}
]
}
如下所示的应用程序脚本:
function doPost(e) {
var data = JSON.parse(e.postData.contents);
var res = data.product_variations.map(({id, sku, attributes: [{name, option}]}) => [new Date(),id, sku, name, option]);
var sheet = SpreadsheetApp.openById('1x5m427kv_viOLm7nsJRl1FR0_-e5tOcK-ItEVw0LdEY').getSheetByName('test');
sheet.getRange(sheet.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}
工作表中的结果是:
我希望结果显示如下:
这是电子表格: 解析 Json
任何帮助将不胜感激
我用注释掉的行对其进行了测试。 所以我不知道其他人会发生什么。
function doPost(e) {
const data = JSON.parse(e.postData.contents);
//const test = '{ "product_variations": [{ "id": 47131, "on_sale": false, "regular_price": 1678, "sale_price": 0, "sku": "GexKiPe-Linen", "quantity": "", "stock": null, "attributes": [ { "name": "color", "slug": "pa_color", "option": "linen" }, { "name": "material", "slug": "pa_material", "option": "permanent" } ]},{ "id": 47132, "on_sale": false, "regular_price": 1678, "sale_price": 0, "sku": "GexKiPe-Heather Beige", "quantity": "", "stock": null, "attributes": [ { "name": "color", "slug": "pa_color", "option": "heather-beige" }, { "name": "material", "slug": "pa_material", "option": "permanent" }]}]}';
//const data = JSON.parse(test);
var res = data.product_variations.map(obj => [new Date(), obj.id, obj.sku, obj.attributes[0].option, obj.attributes[1].option]);
var sheet = SpreadsheetApp.openById('1x5m427kv_viOLm7nsJRl1FR0_-e5tOcK-ItEVw0LdEY').getSheetByName('Sheet0');
//const ss = SpreadsheetApp.getActive();
//const sheet = ss.getSheetByName("Sheet0");
res.unshift(["DateTime","ID","SKU","color","material"]);
sheet.getRange(sheet.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}
试验结果:
约会时间 | ID | 库存单位 | 颜色 | 材料 | |
---|---|---|---|---|---|
2022 年 8 月 14 日 | 47131 | GexKiPe-亚麻 | 亚麻布 | 永恒的 | |
2022 年 8 月 14 日 | 47132 | GexKiPe-Heather 米色 | 希瑟米色 | 永恒的 |
这是最终代码。 它就像一个魅力:
function doPost(e) {
var data = JSON.parse(e.postData.contents);
var search = "color"
var search2 = "material"
var res = data.product_variations.map(obj => [new Date(),data.acf.product_id,data.id,data.sku,obj.id,obj.sku,obj.regular_price,obj.attributes.filter(function (f) { return f.name == search })[0].option, obj.attributes.filter(function (f) { return f.name == search2 })[0].option]);
var sheet = SpreadsheetApp.openById('1F8pgmiSJV0K66lbFO343O8H_LaIi0CGfrukXhD-Gw3g').getSheetByName('woo product variations');
sheet.getRange(sheet.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.