繁体   English   中英

如何将多元素 JSON object 不同级别的值打印到 Google 表格中?

[英]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的值。
  • 您想将检索到的值放到电子表格中。
  • 您想使用 Google Apps 脚本实现此目的。

从您的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);
}
  • 此脚本假定 const 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM