IMPORTHTML function not refreshing data automatically in Google Sheets

I’m having trouble with the IMPORTHTML function in Google Sheets. I wrote a script that pulls table data from a website using IMPORTHTML and set up a trigger to run it every minute. The initial import works perfectly, but when the source data on the website updates, my Google Sheet doesn’t reflect these changes. It keeps showing the old cached values instead of fetching the new ones.

Interestingly, if I manually delete the IMPORTHTML formula from the cell and paste it back, it refreshes correctly with the updated data. However, this manual workaround doesn’t work when executed through my automated script.

Here’s my current code:

function fetchWebData() {
  var workbook = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = workbook.getSheetByName("Inventory456");
    
  var htmlFormula = '=IMPORTHTML("URLwithTABLEdata","table", 1)';
  
  targetSheet.getRange("A1").setValue(htmlFormula);
}

What could be causing this caching issue and how can I force the IMPORTHTML function to refresh properly when triggered automatically?

Yeah, this is a known IMPORTHTML quirk in Google Sheets. The function caches results for performance, which is exactly why your automated refreshes aren’t working. I’ve hit this same wall on multiple projects.

The most reliable fix I’ve found is clearing the target range completely before setting the new formula. You need clearContent() plus a short delay to kill the cache properly.

Try this modified function:

function fetchWebData() {
  var workbook = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = workbook.getSheetByName("Inventory456");
  var range = targetSheet.getRange("A1");
  
  range.clearContent();
  SpreadsheetApp.flush();
  Utilities.sleep(2000);
  
  var htmlFormula = '=IMPORTHTML("URLwithTABLEdata","table", 1)';
  range.setValue(htmlFormula);
}

The flush() and sleep() combo forces Sheets to process the clear operation before applying the new formula, which usually breaks the cache cycle. I’ve found 2 seconds works in most cases, but you might need to tweak it depending on your data source.

Another trick I use is wrapping IMPORTHTML in an IF statement with NOW() - forces it to refresh when Sheets recalculates. Try =if(now()>0,IMPORTHTML("yoururl","table",1)). Not as solid as lucask’s timestamp method, but way better than refreshing manually.

Had this exact issue last year pulling inventory data from supplier sites. Cache invalidation works, but I’ve found something more reliable. Skip the clearing/re-setting dance and use a query parameter trick instead. Most sites ignore random URL parameters, so just tack on “?refresh=” plus a timestamp. Google Sheets thinks it’s hitting a totally different source every time. Here’s how I do it: javascript function fetchWebData() { var workbook = SpreadsheetApp.getActiveSpreadsheet(); var targetSheet = workbook.getSheetByName("Inventory456"); var timestamp = new Date().getTime(); var htmlFormula = '=IMPORTHTML("URLwithTABLEdata?refresh=' + timestamp + '","table", 1)'; targetSheet.getRange("A1").setValue(htmlFormula); } This completely sidesteps caching since every request looks unique to Google’s systems. Been running this for 8+ months with zero cache problems, and it’s way faster than the sleep/clear method since there’s no artificial delay.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.