如何获取REST API JSON数据并将其作为表提取到Google Spreadsheet?

时间:2018-10-03 04:18:14

标签: arrays json rest google-apps-script google-sheets

我们使用AskNicely通过发送调查并使用从响应中获得的数据来衡量我们的绩效来确定我们的NetPromoter得分。然后,我们将响应拉出,并从下面获取示例JSON数据:

{
  "success": true,
  "total": "14836",
  "totalpages": "2968",
  "pagenumber": "1",
  "pagesize": "50000",
  "since_time": "0",
  "data": [
    {
      "response_id": "20352",
      "person_id": "11007",
      "name": "Mark Williams",
      "email": "markwilliams_23@hotmail.com",
      "answer": "5",
      "answerlabel": "5",
      "data": null,
      "comment": "Response time is too long. But was able to resolve my issue",
      "note": null,
      "status": "",
      "dontcontact": null,
      "sent": "2018-10-03 08:53:21",
      "opened": "2018-10-03 08:53:21",
      "responded": "2018-10-03 09:21:30",
      "lastemailed": "2018-10-03 08:53:21",
      "created": "2018-10-03 08:53:02",
      "segment": "",
      "published": "",
      "publishedname": "-",
      "publishedavatar": null,
      "customernumber_c": "171502",
      "companyname_c": "Williams, Mark",
      "agentname_c": "MICHAEL S",
      "city_c": "RIDGEHAVEN",
      "segment_b_c": null,
      "state_c": "SA",
      "workflow_custom_alerts_c": null,
      "workflow_check_for_publish_c": null,
      "workflow_detractor_alerts_c": null,
      "company_c": null,
      "segment_a_c": null,
      "deliverymethod": "email",
      "dashboard": "recommend",
      "email_token": "568e62f94c"
    },
    {
      "response_id": "20351",
      "person_id": "11006",
      "name": "Susie White",
      "email": "Whites61@bigpond.net.au",
      "answer": "9",
      "answerlabel": "9",
      "data": null,
      "comment": "Best service i've gotten so far!",
      "note": null,
      "status": "",
      "dontcontact": null,
      "sent": "2018-10-03 08:53:21",
      "opened": "2018-10-03 08:53:21",
      "responded": "2018-10-03 08:53:21",
      "lastemailed": "2018-10-03 08:53:21",
      "created": "2018-10-03 08:53:02",
      "segment": "",
      "published": "",
      "publishedname": "-",
      "publishedavatar": null,
      "customernumber_c": "171382",
      "companyname_c": "White, Susie",
      "agentname_c": "SAM N",
      "city_c": "Magill",
      "segment_b_c": null,
      "state_c": "SA",
      "workflow_custom_alerts_c": null,
      "workflow_check_for_publish_c": null,
      "workflow_detractor_alerts_c": null,
      "company_c": null,
      "segment_a_c": null,
      "deliverymethod": "email",
      "dashboard": "recommend",
      "email_token": "e257aab272"
    },

我可以使用以下脚本成功提取数据:

function callAPI() {

// Call the Numbers API for random math fact
var response = UrlFetchApp.fetch("insert URL");
var a = response.getContentText();
Logger.log(a);   
}

但这就是我的知识范围。我需要的是将“数据”中的每个项目拉出,并使标签成为第1行中的表标题,然后将它们的值每行拉出并粘贴。我一直在搜索stackoverflow和google,但到目前为止,我所看到的是仅获取1层数据的脚本。有人可以将我路由到正确的资源吗?

1 个答案:

答案 0 :(得分:0)

我能够找到一个完美的代码。来自ImportJSON by Trevor Lohrbeer

但是我使用的是旧版本。不确定为什么它对我有用:Version 1.1