简体   繁体   English

Google App Script:格式化数据并粘贴到 Google 表格

[英]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.我正在尝试学习 Google App Script,因为我觉得它对我的工作非常有用。 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.我在下面粘贴的数据是我从外部 API 获得的数据,截断版本显示了我们网站之一过去 14 天的需求和网络访问。 I need your help to take this data and paste it in a google sheet in a regular tabular format.我需要您的帮助来获取这些数据并将其以常规表格格式粘贴到 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]]
        },

In my python script, this data can be put in a dataframe with pd.read_csv().在我的 python 脚本中,可以使用 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将此用作从外部 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!希望它有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script 从工作表 A 复制粘贴到工作表 B 的最后一行谷歌应用程序脚本 - Copy fro Sheet A to paste in Sheet B's last row google app script 0 谷歌脚本中谷歌表格中的前导数字格式 - 0 leading number format in google sheet in google script 在 Google Script 中创建一个 function 将 JSON 数据调用到数组中并将其粘贴到 Google Sheet 中 - in Google Script create a function to call JSON data into an array and paste it in Google Sheet 基于 email 列在工作表中对行数据进行分组的 Google App 脚本 - Google App Script to Group Row data in sheet based on email column 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script 如何在谷歌应用脚本中获取我的谷歌表格的 json 格式? - How can I get the json format of my google sheet in google app script? 使用谷歌应用脚本将数据从谷歌表格中提取到一个谷歌文档文件中 - Pulling data into ONE google docs file from a google sheet using google app script Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM