I am using this simple google app script to parse through all available Google Sites and dump the html content of individual pages. There are quite many pages so the script will eventually run into 6 minute time limit.
Is it possible to somehow use the PropertiesService to save the current progress (especially in the array loops) and continue where left off later on?
var sites = SitesApp.getAllSites("somedomain.com");
var exportFolder = DriveApp.getFolderById("a4342asd1242424folderid-");
// Cycle through all sites
for (var i in sites){
var SiteName = sites[i].getName();
var pages = sites[i].getAllDescendants();
// Create folder in Drive for each site name
var siteFolder = exportFolder.createFolder(SiteName)
for (var p in pages){
// Get page name and url
var PageUrl = pages[p].getUrl();
//Dump the raw html content in the text file
var htmlDump = pages[p].getHtmlContent();
siteFolder.createFile(PageUrl+".html", htmlDump)
}
}
I can image how one can use the Properties Service to store current line number in the Spreadsheet, and continute where left off. But how can this be done with array containing objects like Sites or Pages?
Using Objects with Properties Service
According to the quotas the maximum size of something you can store in the properties service is 9kb. With a total of 500kb. So if your object is less than this size, it should be no problem. That said, you will need to convert the object to a string with
JSON.stringify()
and when you retrieve it, useJSON.parse
.Working around the run time limit
What is commonly done to work around the limit is to structure a process around the properties service and triggers. Essentially you make the script keep track of time, and if it starts to take a long time, you get it to save its position and then create a trigger so that the script runs again in 10 seconds (or however long you want), for example:
Explanation
i
toj
, it is just an example of a long job that potentially goes over the run time limit.startJob
which callsmainJob(0)
.mainJob
Date
object to get the start time of themainJob
.0
and uses it to initialize thefor
loop to0
as you would normally initialise afor
loop.Date
object to compare with the one created at the beginning ofmainJob
. In the example, it is set to see if the script has been running for 30 seconds, this can obviously be extended but keep it well below the limit.i
in the properties service and then creates a trigger to runjobContinue
in 10 seconds.jobContinue
calls the properties service for the value fori
, and callsmainJob
with the value returned from the properties service.jobContinue
also deletes the trigger it just created to keep things clean.References
JSON.stringify()
JSON.parse
.