简体   繁体   English

如何每月使用导入数据自动更新Google电子表格?

[英]How to auto-update Google Spreadsheet with import data each month?

I want to store UK police crime data for my local area. 我想将英国警方的犯罪数据存储到我所在地区。 UK police update police.uk with new data each month. 英国警方每个月都会向police.uk提供新数据。 This is accessible two ways: 1. CSV. 这有两种方式:1。CSV。 Crime for my area is this This is downloadable as CSV ( 1. & 2. 2. JSON via API. Info here - I haven't tackled this. 我的区域的犯罪是这个可以下载为CSV( 1.2. 2. JSON通过API。信息在这里 - 我还没有解决这个问题。

The problem: The web page and CSV only show data for the last six months. 问题:网页和CSV仅显示过去六个月的数据。 I want to create a Google Drive Spreadsheet that updates each month to add latest data on an ongoing basis, so as to retain ALL data over time. 我想创建一个Google Drive电子表格,每月更新一次,以便持续添加最新数据,以便随时保留所有数据。

I don't really know how to tackle this, especially on the API side. 我真的不知道如何解决这个问题,特别是在API方面。 Should I be using standard Spreadsheet functions like ImportData, Google Spreadsheets' Script editor or even use a programming language like Python, which I don't know? 我应该使用像ImportData,Google Spreadsheets的脚本编辑器这样的标准电子表格函数,还是使用像Python这样的编程语言,我不知道?

Thanks. 谢谢。

How interesting! 多么有趣! I never knew the police had a data API we could use! 我从来不知道警察有我们可以使用的数据API! I would suggest using Google Apps Script, you can easily import the JSON data using that. 我建议使用Google Apps脚本,您可以使用它轻松导入JSON数据。

For example to first fetch the data you can use the URL Fetch Service like so: 例如,要首先获取数据,您可以使用URL提取服务,如下所示:

var data = UrlFetchApp.fetch("http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2013-01");

This will return the raw JSON data for you to play with how you see fit. 这将返回原始JSON数据,供您根据自己的需要进行播放。

You can then iterate over the objects, adding them to an array to add to the spreadsheet. 然后,您可以迭代对象,将它们添加到数组中以添加到电子表格中。 A complete function might look something like the below. 完整的功能可能如下所示。 I know it's extremely messy. 我知道它非常凌乱。 Now I know this police API exists I plan (when I get some free time) to set up something similar, I'll update this answer when I have a better solution but I have tested the below and it does write all the data from a URL to the spreadsheet. 现在我知道这个警察API存在我计划(当我得到一些空闲时间)设置类似的东西时,我会更新这个答案,当我有一个更好的解决方案,但我已经测试了下面的,它确实写了所有的数据来自电子表格的URL。

function importPoliceData() {
  var data = UrlFetchApp.fetch("http://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2013-01");
  var jsonData = JSON.parse(data.getContentText());
  var dataArray = [];
  for(var i = 0; i < jsonData.length; i++){
    var thisRow = [];
    for(var a in jsonData[i]){
      if(typeof jsonData[i][a] == 'object'){
        for(var o in jsonData[i][a]){
          if(typeof jsonData[i][a][o] == 'object'){
            for( var p in jsonData[i][a][o]){
              thisRow.push(jsonData[i][a][o][p]);
            }
          } else {
            thisRow.push(jsonData[i][a][o]);
          }
        }
      } else {
        thisRow.push(jsonData[i][a]);
      }
    }
    while(thisRow.length != 13){
      thisRow.push("");
    }
    dataArray.push(thisRow);
  }
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange(1,1,dataArray.length, dataArray[0].length);
  range.setValues(dataArray);
}

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

相关问题 如何将 Yahoo Finance 中的历史数据导入 Google 表格并使其自动更新? - How to Import Historical Data from Yahoo Finance into Google Sheets and make it Auto-Update? 将 zip 数据导入谷歌电子表格 - Import zip data into google spreadsheet 从Google电子表格导入数据以与ExtendScript Toolkit一起使用 - Import data from google spreadsheet to use with ExtendScript Toolkit 使用 Python 从 Google 电子表格导入 CSV - Import CSV from Google Spreadsheet using Python 将YAML(或类似)转换或导入Google电子表格(或CSV) - Convert or Import YAML (or similar) to Google Spreadsheet (or CSV) Rails 4:如何验证和导入电子表格 - Rails 4: How to validate and import spreadsheet 如何将数据从CSV附加到Google云端硬盘文件夹中的Google电子表格的特定工作表中? - How to append data into a specific sheet in a google spreadsheet from a CSV in a folder in google Drive? 在 Jekyll 数据中参考 Google 电子表格 (CSV) - Reference Google Spreadsheet (CSV) in Jekyll Data 如何将别人的 Google 电子表格添加到我的 Pandas 数据框? - How do I get a someone else's Google Spreadsheet to my Pandas Data Frame? 如何以可想象的最简单的方式将Google Spreadsheet的内容作为CSV数据获取? - How can I get the contents of a Google Spreadsheet as CSV data in the simplest way imaginable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM