【问题标题】:Append new api data to google sheet将新的 api 数据附加到谷歌表
【发布时间】:2018-03-26 10:21:00
【问题描述】:

我有各种 (JSON) api 数据源,我想将它们附加到谷歌表中进行分析 - 理想情况下,每隔一两个小时运行一次,并附上时间戳。

我已经格式化了源代码,并且我发现了如何在 google 表格中使用触发器来使其每“x”小时运行一次——这是我正在努力解决的附加位。我猜我需要一些代码来计算最后使用的行或列在哪里,并为下一个追加添加一个?

就数据源而言,作为示例,我正在使用以下代码: http://blog.fastfedora.com/projects/import-json#ImportJSON 和我的一个数据源 (reddit) 是这样提取的:

=ImportJSON("http://reddit.com/r/redditdev/about.json", "/data/url,/data/subscribers", "")

这给出如下输出:

Subscribers  Url
12114        /r/redditdev/

我希望最终输出的样子:

Url            TimeStamp             Subscribers
/r/redditdev/  14/10/2017 11:00:00   12114    
/r/redditdev/  14/10/2017 12:00:00   12118
/r/<others>    x                     y          

任何帮助表示赞赏!谢谢

【问题讨论】:

    标签: json google-apps-script google-sheets


    【解决方案1】:

    不久前我写了一个小小的 reddit 爬虫。似乎在这里会有所帮助:github

    但是要对其进行一些定制以完全满足您的需求:

    var date = Utilities.formatDate(new Date(), "America/Los_Angeles", "dd/MM/yyyy HH:mm:ss");
    var subs = json_twoDarray[1][0], //first index is row, second is column
        url = json_twoDarray[1][1];
    //Assuming there's a "sheet" variable for the sheet you are logging data to,
    //get one past the last row and the first three columns and set the values
    sheet.getRange(sheet.getLastRow + 1, 1, 1, 3).setValues([[url, date, subs]]); 
    

    我通常还会将新数据附加到电子表格的顶部,以便首先显示最新数据,这通常非常好。如果你想这样做,你可以将上面 sn-p 的最后一行更改为:

    sheet.insertRowBefore(2); //Assuming the first row is headers, insert a new second row
    sheet.getRange(2,1,1,3).setValues([[url, date, subs]]); //add the data
    

    我没有机会测试这些 sn-ps,但它们应该可以工作。如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多