3

I'd like to know whether it is possible to write a Google Apps Script that will trigger daily importhtml in Google spreadsheets. For example let's assume I want to import updates of following table everyday at 1 pm.

=IMPORTHTML("https://en.wikipedia.org/wiki/Lubbock,_Texas","table",5)

I need this automatic checking for table updates even when my computer is offline. Thank you

Rubén
  • 34,714
  • 9
  • 70
  • 166
Behzod A
  • 173
  • 1
  • 2
  • 14
  • When you say import updates? Do you mean make a copy of the table every day at 1 pm or look for changes to previous days table and store only the change? App script run on google server so they will work even when your computer is offline/ switch off. – Jack Brown Feb 11 '17 at 08:38
  • I meant to import new table even though there are no changes. But I'm sure everyday there will be new table data – Behzod A Feb 11 '17 at 20:03

1 Answers1

5

Yes, it is possible to write a script to make a copy of the table every day. In short, you will have to make a copy of the imported table and paste it into a new Sheet. ImportHTML a gets updated every time you open/access the spreadsheet. So only way to store table is to make a copy and paste it into a new sheet.

Here is code that does just that:

function getImportData()
{
 var ss = SpreadsheetApp.getActive()
var sheet =ss.getSheetByName("ImportSheet")

if (sheet == null)
{  
sheet = ss.insertSheet("ImportSheet")
sheet.getRange(1,1).setValue( "=IMPORTHTML(\"https://en.wikipedia.org/wiki/Lubbock,_Texas\",\"table\",5)")
}
var dataRange = sheet.getDataRange()
//waitforLoading waits for the function importHTML to finish loading the table, only a problem if table takes a while to load
//for more discussion on this visit : http://stackoverflow.com/questions/12711072/how-to-pause-app-scripts-until-spreadsheet-finishes-calculation
var wait = waitForLoading(dataRange,sheet, 10)
var logSheet = ss.getSheetByName("ImportLogSheet")
 if (logSheet == null)
  {  
    logSheet = ss.insertSheet("ImportLogSheet")
  }
  var timeStampRow = []
  timeStampRow[0] = new Date()
if(wait)
{
  logSheet.appendRow(timeStampRow)
  var lastRow = logSheet.getLastRow() + 1
  var destinationRange = logSheet.getRange(lastRow,1)
  dataRange.copyTo(destinationRange, {contentsOnly:true})  

}
  else {
    timeStampRow[1] = "Scripted timeout waiting for the table to Load " 
    logSheet.appendRow(timeStampRow)
  }
}

function waitForLoading(dataRange, sheet, maxWaitTimeInSec)
{
 // This function is only required if it takes a while for the importHTML to load your data!
 // if not you can skip this function

  // Function looks to see if the value of the  
for(i = 0; i< maxWaitTimeInSec ; i++)
{
  var value = dataRange.getCell(1,1).getValue()
  if(value.search("Loading") !== -1) {
    Utilities.sleep(1000);
    dataRange = sheet.getDataRange()
  } else {
   return true
  }

}
  return false

}

Edit: Forgot to mention about setting up triggers. You can setup any function to be triggered using time-driven triggers. Details on how to set it up is given here: https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_manually

Jack Brown
  • 5,802
  • 2
  • 12
  • 27