如何使用Google Sheets应用脚本将JSON中的多个数组添加到Google Sheets中

sqxo8psd  于 2023-01-18  发布在  Go
关注(0)|答案(1)|浏览(132)

如何添加以下字段:
1.权重层信息〉名称
1.重量层信息〉克量
对于此代码:

function test(e) {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetsheet = ss.getSheetByName("test");

var options = {
     //"async": true,
     //"crossDomain": true,
     "method" : "GET",
     "headers" : {
       "clientId" : "1",
       "key": "1",
       "Prefer": "code=200",
       "Prefer":"dynamic=true"
       
       }}



  var text = UrlFetchApp.fetch("https://stoplight.io/mocks/flowhub/public-developer-portal/24055485/v0/locations/1/inventory",options).getContentText();
  var json = JSON.parse(text);



var values = json.data.flatMap(({ productId,cannabinoidInformation }) =>  cannabinoidInformation.map(({lowerRange,name}) => [productId,lowerRange,name])
);


targetsheet.getRange(2, 1,values.length, values[0].length).setValues(values);

}

在输出填充A-C列时,我想使用上面的子字段填充D-E

mbskvtky

mbskvtky1#

在这种情况下,下面的修改怎么样?

发件人:

var values = json.data.flatMap(({ productId,cannabinoidInformation }) =>  cannabinoidInformation.map(({lowerRange,name}) => [productId,lowerRange,name])

);

收件人:

var values = json.data.flatMap(({ productId, cannabinoidInformation, weightTierInformation }) => cannabinoidInformation.map(({ lowerRange, name }, i) => [productId, lowerRange, name, weightTierInformation[i].name, weightTierInformation[i].gramAmount]));
  • 通过这种修改,productIdcannabinoidInformationweightTierInformation的值被置于列“A”、列“B”至“C”、列“D”至“E”.

相关问题