Google App脚本:格式化数据并粘贴到Google工作表

时间:2018-12-03 00:36:35

标签: javascript google-apps-script google-sheets-api

我正在尝试学习Google App脚本,因为它对我的工作非常有用。我下面粘贴的数据是我从外部API获得的数据,截短的版本显示了我们其中一个网站最近14天的需求和网络访问。我需要您的帮助,以获取这些数据并将其以常规表格格式粘贴到Google工作表中。

{"chart":{"renderTo":"container","defaultSeriesType":"line"},"title":{"text":"Test data"},"xAxis":{"categories":["2018-11-18 00:00:00","2018-11-19 00:00:00","2018-11-20 00:00:00","2018-11-21 00:00:00","2018-11-22 00:00:00","2018-11-23 00:00:00","2018-11-24 00:00:00","2018-11-25 00:00:00","2018-11-26 00:00:00","2018-11-27 00:00:00","2018-11-28 00:00:00","2018-11-29 00:00:00","2018-11-30 00:00:00","2018-12-01 00:00:00"],"title":{"text":null},"labels":{"rotation":-90,"step":1,"align":"right"}},"yAxis":{"plotLines":[{"value":0,"width":1,"color":"#808080"}],"title":{"text":null},"startOnTick":false},"legend":{"symbolPadding":1,"symbolWidth":10,"enabled":true},"plotOptions":{"pie":{"dataLabels":{"enabled":"false"}}},"series":[{"name":"Web Demand","data":[["2018-11-18 00:00:00",91829.33],["2018-11-19 00:00:00",70922.5],["2018-11-20 00:00:00",71637.51],["2018-11-21 00:00:00",87323.88],["2018-11-22 00:00:00",110634.95],["2018-11-23 00:00:00",189215.81],["2018-11-24 00:00:00",146681.11],["2018-11-25 00:00:00",229967.43],["2018-11-26 00:00:00",330220.47],["2018-11-27 00:00:00",137886.3],["2018-11-28 00:00:00",108642.67],["2018-11-29 00:00:00",126365.65],["2018-11-30 00:00:00",140458.59],["2018-12-01 00:00:00",181448.12]]},{"name":"Web Visits","data":[["2018-11-18 00:00:00",52167],["2018-11-19 00:00:00",39785],["2018-11-20 00:00:00",50454],["2018-11-21 00:00:00",51053],["2018-11-22 00:00:00",59999],["2018-11-23 00:00:00",92882],["2018-11-24 00:00:00",78601],["2018-11-25 00:00:00",99010],["2018-11-26 00:00:00",111094],["2018-11-27 00:00:00",79914],["2018-11-28 00:00:00",51760],["2018-11-29 00:00:00",60145],["2018-11-30 00:00:00",63811],["2018-12-01 00:00:00",65633]]},

在我的python脚本中,可以使用pd.read_csv()将这些数据放入数据框。我不知道如何使用Google App脚本。理想情况下,我想查看数据如下:

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

将其用作从外部API获取的示例数据

{
  "chart": { "renderTo": "container", "defaultSeriesType": "line" },
  "title": { "text": "Test data" },
  "xAxis": {
    "categories": [
      "2018-11-18 00:00:00",
      "2018-11-19 00:00:00",
      "2018-11-20 00:00:00",
      "2018-11-21 00:00:00",
      "2018-11-22 00:00:00",
      "2018-11-23 00:00:00",
      "2018-11-24 00:00:00",
      "2018-11-25 00:00:00",
      "2018-11-26 00:00:00",
      "2018-11-27 00:00:00",
      "2018-11-28 00:00:00",
      "2018-11-29 00:00:00",
      "2018-11-30 00:00:00",
      "2018-12-01 00:00:00"
    ],
    "title": { "text": null },
    "labels": { "rotation": -90, "step": 1, "align": "right" }
  },
  "yAxis": {
    "plotLines": [{ "value": 0, "width": 1, "color": "#808080" }],
    "title": { "text": null },
    "startOnTick": false
  },
  "legend": { "symbolPadding": 1, "symbolWidth": 10, "enabled": true },
  "plotOptions": { "pie": { "dataLabels": { "enabled": "false" } } },
  "series": [
    {
      "name": "Web Demand",
      "data": [
        ["2018-11-18 00:00:00", 91829.33],
        ["2018-11-19 00:00:00", 70922.5],
        ["2018-11-20 00:00:00", 71637.51],
        ["2018-11-21 00:00:00", 87323.88],
        ["2018-11-22 00:00:00", 110634.95],
        ["2018-11-23 00:00:00", 189215.81],
        ["2018-11-24 00:00:00", 146681.11],
        ["2018-11-25 00:00:00", 229967.43],
        ["2018-11-26 00:00:00", 330220.47],
        ["2018-11-27 00:00:00", 137886.3],
        ["2018-11-28 00:00:00", 108642.67],
        ["2018-11-29 00:00:00", 126365.65],
        ["2018-11-30 00:00:00", 140458.59],
        ["2018-12-01 00:00:00", 181448.12]
      ]
    },
    {
      "name": "Web Visits",
      "data": [
        ["2018-11-18 00:00:00", 52167],
        ["2018-11-19 00:00:00", 39785],
        ["2018-11-20 00:00:00", 50454],
        ["2018-11-21 00:00:00", 51053],
        ["2018-11-22 00:00:00", 59999],
        ["2018-11-23 00:00:00", 92882],
        ["2018-11-24 00:00:00", 78601],
        ["2018-11-25 00:00:00", 99010],
        ["2018-11-26 00:00:00", 111094],
        ["2018-11-27 00:00:00", 79914],
        ["2018-11-28 00:00:00", 51760],
        ["2018-11-29 00:00:00", 60145],
        ["2018-11-30 00:00:00", 63811],
        ["2018-12-01 00:00:00", 65633]
      ]
    }
  ]
}

您可以使用它来提取/格式化数据

var abstractRows = {};

//* Web Demand
var webDemand = data.series[0];
for (var a = 0; a < webDemand.data.length; a++) {
  const serie = webDemand.data[a];
  abstractRows[serie[0]] = { web_demand: serie[1] };
}

//* Web Visits
var webVisits = data.series[1];
for (var a = 0; a < webVisits.data.length; a++) {
  const serie = webVisits.data[a];
  abstractRows[serie[0]].web_visits = serie[1];
}

var rows = [];
var keys = Object.keys(abstractRows);
for (var key of keys) {
  var row = abstractRows[key];

  //* Append each row to spreadsheet
  rows.push([key, row.web_demand, row.web_visits]);
}

希望它会有所帮助!