function import_inventory_test(e) {
var options = {
"method": "GET",
"headers": {
'Content-Type': 'application/json',
'Prefer': 'code=200',
'Prefer': 'dynamic=true',
"clientId": "1",
"key": "1",
}
}
var text = UrlFetchApp.fetch("https://stoplight.io/mocks/flowhub/public-developer-portal/24055485/v0/inventory?max=100", options).getContentText();
var json = JSON.parse(text);
var values = json.data.map(({ sku, quantity, productName, brand }) => [sku, quantity, productName, brand]);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set your sheet name.
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
- how can I add to the "json.data.map" the "gramAmount" which is under "weightTierInformation":
"weightTierInformation": [
{
"name": "string",
"gramAmount": 0,
"pricePerUnitInMinorUnits": 0
} ]
- How can I store the fields names in a "var" so I won't have to write them in 2 different locations? X=>X
CodePudding user response:
In your situation, how about the following modification?
From:
var values = json.data.map(({ sku, quantity, productName, brand }) => [sku, quantity, productName, brand]);
To:
In this case, all rows are filled by sku, quantity, productName, brand, gramAmount
.
var values = json.data.flatMap(({ sku, quantity, productName, brand, weightTierInformation }) =>
weightTierInformation.map(({gramAmount}) => [sku, quantity, productName, brand, gramAmount])
);
Or, in the following case, each 1st row has sku, quantity, productName, brand, gramAmount
, and other rows have null, null, null, null, gramAmount
.
var values = json.data.flatMap(({ sku, quantity, productName, brand, weightTierInformation }) =>
weightTierInformation.map(({gramAmount}, j) => j == 0 ? [sku, quantity, productName, brand, gramAmount] : [...Array(4).fill(null), gramAmount])
);