How to set up automatic updates on a Google Sheet

This is a tutorial on how to upload and import a live-updating CSV File into a Google Spreadsheet using Google Apps Script

Importing a static CSV data into Google spreadsheet can be as easy as doing File > Import > Upload. However, when you have a link to a dynamic CSV file that is being regularly updated, and want to import it into your own Google Spreadsheet to edit the data, it's not as simple. 

👉 This academy article would be useful for: 

  • anyone using Google sheet to import dynamic CSV data to edit/clean/calculate before importing it into Datawrapper 
  • anyone reusing charts from Datawrapper River and want to edit the data source that is not your own by importing the data source in your own spreadsheet
1
Create a new document, and go to  Tools > Script editor 
2
Paste in the following script. Make sure to replace the link “ https:// your-data-source.csv" with your data link. Also note that if you give your sheet a different name, you'll have to swap " Sheet1" for your chosen name.
If you don't want to set this up yourself, you can simply copy this Google sheet we've prepared for you and swap out the link for your CSV link and jump to step 7 to set up the Trigger. To copy, you'd have to be logged-in to your google account.. Then go to the menu bar at the top of the spreadsheet and click on File > Make a copy. 
function myFunction() {
  function importData()
  {
    var ss = SpreadsheetApp.getActive();
    var url = 'https://your-data-source.csv';
    var text = UrlFetchApp.fetch(url).getContentText();
    var csvData = Utilities.parseCsv(text);
    var sheet = ss.getSheetByName('Sheet1');
    for (var i = 0; i < csvData.length; i++) {
      sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
    }
  }
  importData();
}
	
3
File > Save to save and name the project, choose whatever name you like.
4
Test that it works by clicking the play ▶ button (a popup will come up that you need to give the script the edit permissions it needs first, just follow the instructions there) 
5
Look at the sheet - you should now find that the data is in there: 
6
Now you need to set up that the script runs automatically at regular time intervals, to do so, you'll have to navigate to the Google Apps Script Dashboard by clicking on the blue box in the script editor: 
7
Click on the three dots, and then go to Triggers 
8
Click + Add Trigger 
9
In the dialogue that opens choose the event source as Time-driven 
10
You can then chose how often you want it to run: 
11
That's it! Your sheet is ready, you can go back to your google sheet then turn on sharing by clicking the "Share" button on the top-right corner: 
Make sure that your google sheet is set to " Anyone with the link" can view. Click on " Copy link" and paste it into Datawrapper in the Link external dataset configuration. If you're not familiar with what this is, check out our academy article "Automatic chart updates: How to publish charts with external data sources ":