将数据从JSON回复传递到Google电子表格

时间:2016-11-22 00:36:30

标签: json google-apps-script

我正在构建一个脚本来调用FB api并将响应导入到google工作表中。使用JSON.stringify后,我得到以下回复:

    {
   "data":[
      {
         "id":"page_id/insights/page_impressions/day",
         "name":"page_impressions",
         "period":"day",
         "values":[
            {
               "value":26023596,
               "end_time":"2016-11-17T08:00:00+0000"
            },
            {
               "value":24447386,
               "end_time":"2016-11-18T08:00:00+0000"
            },
            {
               "value":31057386,
               "end_time":"2016-11-19T08:00:00+0000"
            }
         ],
         "title":"Daily Total Impressions",
         "description":"Daily: The number of impressions seen of any content associated with your Page. (Total Count)"
      },
      {
         "id":"page_id/insights/page_impressions/week",
         "name":"page_impressions",
         "period":"week",
         "values":[
            {
               "value":233007217,
               "end_time":"2016-11-17T08:00:00+0000"
            },
            {
               "value":200263630,
               "end_time":"2016-11-18T08:00:00+0000"
            },
            {
               "value":194289364,
               "end_time":"2016-11-19T08:00:00+0000"
            }
         ],
         "title":"Weekly Total Impressions",
         "description":"Weekly: The number of impressions seen of any content associated with your Page. (Total Count)"
      },
      {
         "id":"page_id/insights/page_impressions/days_28",
         "name":"page_impressions",
         "period":"days_28",
         "values":[
            {
               "value":867302439,
               "end_time":"2016-11-17T08:00:00+0000"
            },
            {
               "value":868201060,
               "end_time":"2016-11-18T08:00:00+0000"
            },
            {
               "value":874965509,
               "end_time":"2016-11-19T08:00:00+0000"
            }
         ],
         "title":"28 Days Total Impressions",
         "description":"28 Days: The number of impressions seen of any content associated with your Page. (Total Count)"
      }
   ],
   "paging":{
      "previous":"https://graph.facebook.com/v2.3/page_id/insights/page_impressions?access_token=my_token&since=1479068112&until=1479327312",
      "next":"https://graph.facebook.com/v2.3/page_id/insights/page_impressions?access_token=my_token&since=1479586512&until=1479845712"
   }
}

我想在我的Google工作表中导入Daily Total ImpressionsWeekly Total Impressions28 Days Total Impressions的值,但我不知道该如何处理。我知道如何访问特定值,例如:data.data[0].values[0].value。但这就是全部。我真的不知道下一步该做什么。

理想情况下,我希望能够创建一个新数组并将数据推送到那里。 (var data = []; data.push(???)

但是我在多个数组/对象中完全迷失了。

谢谢!

1 个答案:

答案 0 :(得分:3)

以下脚本怎么样? “jsondata”是您的数据。请将此脚本用作电子表格的容器绑定脚本。

var data = JSON.parse(jsondata);
var ar = [];
for (var i in data.data){
    for (var k in data.data[i].values) {
        ar.push([data.data[i].title, data.data[i].values[k].value]);
    }
}
ass = SpreadsheetApp.getActiveSpreadsheet();
ass.getRange('a1').offset(0, 0, ar.length, ar[0].length).setValues(ar);

结果如下。

enter image description here