[英]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.