简体   繁体   中英

Google App Script: Format data and paste to google sheet

I'm trying to learn Google App Script as I feel it can be very useful for my work. The data I've pasted below is data I got from an external API and the truncated version shows demand & web visits for the last 14 days for one of our websites. I need your help to take this data and paste it in a google sheet in a regular tabular format.

{
"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]]
        },

In my python script, this data can be put in a dataframe with pd.read_csv(). I can't figure out how to do it with google app script. Ideally, i want to see the data look like this:

在此处输入图片说明

Thanks for your help!

Using this as sample data fetched from an external 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]
      ]
    }
  ]
}

You can use this to extract/format your data

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]);
}

hopefully it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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