[英]How do I print values from different levels of a multi-element JSON object into Google Sheets?
I have parsed the below JSON object using var json = JSON.parse(ticket_orders)
, and now I'm trying to use Google Sheets script editor to make a table with four columns: event_id , display (from costs.base_price), name (來自配置文件)和狀態(來自條形碼,這是一個與其他數組不同的數組)。
下面的 JSON object 只顯示了兩個元素,但我有 40 個(但這總是在變化)。 我在創建 function 時遇到問題,它迭代地提取我上面提到的值並將它們整齊地放在 Google 表格中的表格中。
var json = [{
"team": null, //This is the start of element 1
"costs": {
"base_price": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"eventbrite_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"gross": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"payment_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"tax": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
}
},
"resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/",
"id": "12345",
"changed": "2018-11-02T22:36:48Z",
"created": "2018-11-02T19:07:24Z",
"quantity": 1,
"variant_id": null,
"profile": {
"first_name": "Kennedy",
"last_name": "Singleton",
"email": "kennedy@gmail.com",
"name": "Kennedy Singleton",
"addresses": {
"home": {},
"ship": {},
"work": {},
"bill": {},
"fulfillment": {}
}
},
"barcodes": [
{
"status": "used",
"barcode": "1234567435234",
"created": "2018-11-02T19:07:24Z",
"changed": "2018-11-02T22:36:48Z",
"checkin_type": 0,
"checkin_method": "search",
"is_printed": false
}
],
"answers": [],
"checked_in": true,
"cancelled": false,
"refunded": false,
"affiliate": null,
"guestlist_id": null,
"invited_by": null,
"status": "Checked In",
"ticket_class_name": "General Admission",
"delivery_method": "electronic",
"event_id": "12345",
"order_id": "123123123",
"ticket_class_id": "123123123"
},
{
"team": null, //This is the start of element 2
"costs": {
"base_price": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"eventbrite_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"gross": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"payment_fee": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
},
"tax": {
"display": "CA$0.00",
"currency": "CAD",
"value": 0,
"major_value": "0.00"
}
},
"resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/",
"id": "12345",
"changed": "2018-11-02T22:36:48Z",
"created": "2018-11-02T19:07:24Z",
"quantity": 1,
"variant_id": null,
"profile": {
"first_name": "Kennedy",
"last_name": "Singleton",
"email": "kennedy@gmail.com",
"name": "Kennedy Singleton",
"addresses": {
"home": {},
"ship": {},
"work": {},
"bill": {},
"fulfillment": {}
}
},
"barcodes": [
{
"status": "used",
"barcode": "1234567435234",
"created": "2018-11-02T19:07:24Z",
"changed": "2018-11-02T22:36:48Z",
"checkin_type": 0,
"checkin_method": "search",
"is_printed": false
}
],
"answers": [],
"checked_in": true,
"cancelled": false,
"refunded": false,
"affiliate": null,
"guestlist_id": null,
"invited_by": null,
"status": "Checked In",
"ticket_class_name": "General Admission",
"delivery_method": "electronic",
"event_id": "12345",
"order_id": "123123123",
"ticket_class_id": "123123123"
}]
你能幫我創建一個 function 來完成這個嗎?
我試圖用谷歌搜索,但找不到包含以下三個部分的問答:1) 針對 JSON object 層次結構的不同級別中的值,2) 具有多個需要迭代的元素,以及 3) 放置它們到 Google 表格中。
到目前為止,這是我能夠編寫的腳本:
function getAndInsertConent(url) {
const makeService = getApiService(); //Authorizes my access to the API via OAuth
const apiFetechedContent = UrlFetchApp.fetch(url,{
headers: {
Authorization: 'Bearer ' + makeService.getAccessToken()
}
});
const content = apiFetechedContent.getContentText(); //Makes the raw, fetched JSON response accessible
const json = JSON.parse(content); //Turns the JSON into a JavaScript object
var rows = [Object.keys(json)]; // Retrieve headers, but this equals ['pagination', 'attendees']
var jsonAttendees = json.attendees //stores just the 'attendees' branch of the JavaScript object
//Store our target spreadsheet into a variable
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
//code to access the specific values I need
}
我不能比我在我的代碼中做出最后一條評論的地方更進一步。 我不知道如何訪問這些值並將它們整齊地放入表格中。
任何幫助將不勝感激。 非常感謝。
我相信你的目標如下。
event_id, display (from costs.base_price), name (from profile), and status (from barcodes, which is an array unlike the others)
。 從json
的值。 從您的json
樣本值來看,我認為包括數組在內的 JSON 數據的每個元素的結構始終相同。 我認為這也許可以使用。 那么,為了檢索如何使用map()
和解構賦值呢? 當你的腳本被修改后,它變成如下。
請設置要放置值的工作表名稱。
function getAndInsertConent(url) {
const sheetName = "Sheet1"; // Please set the sheet name.
const makeService = getApiService();
const apiFetechedContent = UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + makeService.getAccessToken() } });
const content = apiFetechedContent.getContentText();
const json = JSON.parse(content);
const values = json.map(({ event_id, costs: { base_price: { display } }, profile: { name }, barcodes: [{ status }] }) => [event_id, display, name, status]);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
json
const json = JSON.parse(content);
的 json 與您問題中樣本值的json
相同。 請注意這一點。
jsonAttendees
與您問題中示例值的json
相同,請修改上述腳本。 當檢查上述腳本的values
時,可以使用以下測試腳本。
const json = [{ "team": null, //This is the start of element 1 "costs": { "base_price": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "eventbrite_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "gross": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "payment_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "tax": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" } }, "resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/", "id": "12345", "changed": "2018-11-02T22:36:48Z", "created": "2018-11-02T19:07:24Z", "quantity": 1, "variant_id": null, "profile": { "first_name": "Kennedy", "last_name": "Singleton", "email": "kennedy@gmail.com", "name": "Kennedy Singleton", "addresses": { "home": {}, "ship": {}, "work": {}, "bill": {}, "fulfillment": {} } }, "barcodes": [ { "status": "used", "barcode": "1234567435234", "created": "2018-11-02T19:07:24Z", "changed": "2018-11-02T22:36:48Z", "checkin_type": 0, "checkin_method": "search", "is_printed": false } ], "answers": [], "checked_in": true, "cancelled": false, "refunded": false, "affiliate": null, "guestlist_id": null, "invited_by": null, "status": "Checked In", "ticket_class_name": "General Admission", "delivery_method": "electronic", "event_id": "12345", "order_id": "123123123", "ticket_class_id": "123123123" }, { "team": null, //This is the start of element 2 "costs": { "base_price": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "eventbrite_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "gross": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "payment_fee": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" }, "tax": { "display": "CA$0.00", "currency": "CAD", "value": 0, "major_value": "0.00" } }, "resource_uri": "https://www.eventbriteapi.com/v3/events/12345/attendees/12345/", "id": "12345", "changed": "2018-11-02T22:36:48Z", "created": "2018-11-02T19:07:24Z", "quantity": 1, "variant_id": null, "profile": { "first_name": "Kennedy", "last_name": "Singleton", "email": "kennedy@gmail.com", "name": "Kennedy Singleton", "addresses": { "home": {}, "ship": {}, "work": {}, "bill": {}, "fulfillment": {} } }, "barcodes": [ { "status": "used", "barcode": "1234567435234", "created": "2018-11-02T19:07:24Z", "changed": "2018-11-02T22:36:48Z", "checkin_type": 0, "checkin_method": "search", "is_printed": false } ], "answers": [], "checked_in": true, "cancelled": false, "refunded": false, "affiliate": null, "guestlist_id": null, "invited_by": null, "status": "Checked In", "ticket_class_name": "General Admission", "delivery_method": "electronic", "event_id": "12345", "order_id": "123123123", "ticket_class_id": "123123123" }]; const values = json.map(({ event_id, costs: { base_price: { display } }, profile: { name }, barcodes: [{ status }] }) => [event_id, display, name, status]); console.log(values);
如果上面的示例腳本沒有用,並且包含數組的 JSON 數據的每個元素的結構並不總是相同,您能否提供更多示例值?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.