All Collections
Connect your spreadsheets
Prepare your spreadsheet
Set refresh rate for spreadsheets widgets powered by import functions
Set refresh rate for spreadsheets widgets powered by import functions

Set your own refresh interval by writing a script using Google Apps Scripts.

Updated over a week ago

Spreadsheet widgets powered by import functions, such as ImportRange and ImportData, automatically refresh around every 15 minutes. If this isn't often enough for your needs, you can set your own refresh interval by writing a script.

Note: Be aware that when data from multiple import functions – each with their own refresh intervals – are used in the same connected spreadsheet, it can affect the accuracy of the data displayed.

Writing the Apps Scripts

We've included steps for ImportData, ImportHTML, ImportRange, ImportXML, and GoogleFinance.

ImportData script

  1. Click on Tools > Script Editor.

  2. Replace the default function with the following script.

    function myFunction() {
          SpreadsheetApp.getActive().getRange('A1').setValue('=importdata("https://www.quandl.com/api/v3/datasets/BCHARTS/BITFINEXUSD.csv")')
    }
  3. Make sure the cell in your sheet exactly matches the one in the script; otherwise, ImportData will not update automatically.

  4. Select File > Save, give it a name, and then skip to setting up a trigger for your new script.

ImportHTML script

  1. Click on Tools > Script Editor.

  2. Replace the default code with the following script:

    function getData() { 
      var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("<sheet-name>"); 
      var queryString = Math.random(); 
      var cellFunction = '=IMPORTHTML("<url>?","table",<index>)';    sheetName.getRange('<import-cell>').setValue(cellFunction); 
    }
  3. Replace <sheet-name> with the name of your sheet (the tab name, not the name of the file).

  4. Replace <url> with the URL of your web page, e.g. https://www.bloomberg.com/markets/currencies/cross-rates.

  5. Replace <index> with the table position on the page, e.g. 1.

  6. Replace <import-cell> with the cell where you want to put your import statement, e.g. A1.

  7. In the title field, add a name to your project.

  8. Select File > Save. Then skip to setting up a trigger for your new script.

Note: the random number at the end of our URL is required. Without it, the website will be cached, and the function stops importing the latest data.

ImportRange script

  1. Click on Tools > Script Editor.

  2. Replace the default function with the following script.

    function myFunction() {
      
      SpreadsheetApp.getActive().getRange('A1').setValue('IMPORTRANGE('https://docs.google.com/spreadsheets/d/abcd123abcd123', 'sheet1!A:B')')
    }
  3. Edit the script by first replacing the .setValue('IMPORTRANGE') function with your own. Then making sure the .getRange('cell') in your sheet exactly matches the one in the script; otherwise, ImportRange will not update automatically.

  4. Select File > Save, give it a name, and then skip to setting up a trigger for your new script.

ImportXML script

  1. In a new Google Sheet, we'll first need to split the ImportXML function into two logical pieces: the URL and the XPath query. Write the URL in cell A1 and the XPath query in cell A2.

  2. If you want to add another ImportXML function to this sheet so that you can do calculations with the data, add the second URL in cell A4 and the XPath query in cell A5.

  3. Give your Google Sheet a name.

    Prepare_google_sheet
  4. To write the script, click on Tools > Script Editor.

  5. Replace the default function with the following script:

    function getData() {
      var queryString = Math.random();  var cellFunction1 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A2').getValue() + '")';
      SpreadsheetApp.getActiveSheet().getRange('C1').setValue(cellFunction1);  var cellFunction2 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A4').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A5').getValue() + '")';
      SpreadsheetApp.getActiveSheet().getRange('C2').setValue(cellFunction2);
    }
  6. Double-check the following aspects of the first section of the script (var cellFunction1...):

    • Cell reference A1 should reflect where your first URL is written in your Google Sheet.

    • A2 should reflect where your first XPath query is written in your Google Sheet.

    • C1 should reflect where you want to display the result of your ImportXML function.

  7. If you want to include a second ImportXML function, double-check the following in the second section of the script (var cellFunction2...):

    • Cell reference A4 should reflect where your first URL is written in your Google Sheet.

    • A5 should reflect where your first XPath query is written in your Google Sheet.

    • C2 should reflect where you want to display the result of your second ImportXML function.

  8. If you don't want to include a second ImportXML function, delete everything from var cellFunction2... to ...setValue(cellFunction2); leaving just the following script:

    function getData() {
      var queryString = Math.random();  var cellFunction1 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A2').getValue() + '")';
      SpreadsheetApp.getActiveSheet().getRange('C1').setValue(cellFunction1);  var cellFunction2 = '=IMPORTXML("' + SpreadsheetApp.getActiveSheet().getRange('A4').getValue() + '?' + queryString + '","'+ SpreadsheetApp.getActiveSheet().getRange('A5').getValue() + '")';
      SpreadsheetApp.getActiveSheet().getRange('C2').setValue(cellFunction2);
    }
  9. Select File > Save, give it a name, and then skip to setting up a trigger for your new script.

Note: the random number at the end of our URL is required. Without it, the website will be cached, and the function stops importing the latest data.

GoogleFinance script

  1. Click on Tools > Script Editor.

  2. Replace the default function with the following script:

    function getData() { SpreadsheetApp.getActiveSheet().getRange('A2').setValue('=GOOGLEFINANCE("GOOG", "price")');}
  3. Select File > Save, give it a name, and then skip to setting up a trigger for your new script.

Set up a trigger for your script

After writing your script set up a trigger to run as often as you need.

  1. From inside the Script Editor, click on the Edit menu and select the option Current project's triggers.

  2. Click the + Add Trigger button in the bottom right corner of the page.

    add_trigger.png
  3. Set a trigger for the myFunction function that runs from Head on a Time-driven source that's triggered on a Minute timer.

    trigger settings in Google Sheets
  4. Once you've set the trigger, run the script for the first time. This means you don't have to wait 1 hour to get data.

    run_script.png

Tip: You can force the widget to update by reloading the spreadsheet in Geckoboard.

Did this answer your question?