Google Apps Script: How to use ContinuationToken with recursive folder iterator -
because of drive api quotas, services quotas , limit of script execution time 6 min
it's critical split google drive files manipulations on chunks.
we can use propertiesservice store continuationtoken
folderiterator or fileiterator. way can stop our script , on next run continue place stop.
working example (linear iterator)
// logs name of every file in user's drive // useful script may take more 5 minutes (max execution time) var userproperties = propertiesservice.getuserproperties(); var continuationtoken = userproperties.getproperty('continuation_token'); var start = new date(); var end = new date(); var maxtime = 1000*60*4.5; // max safe time, 4.5 mins if (continuationtoken == null) { // firt time execution, files drive var files = driveapp.getfiles(); } else { // not first time, pick left off var files = driveapp.continuefileiterator(continuationtoken); } while (files.hasnext() && end.gettime() - start.gettime() <= maxtime) { var file = files.next(); logger.log(file.getname()); end = new date(); } // save place setting token in user properties if(files.hasnext()){ var continuationtoken = files.getcontinuationtoken(); userproperties.setproperty('continuation_token', continuationtoken); } else { // delete token propertiesservice.getuserproperties().deleteproperty('continuation_token'); }
problem (recursive iterator)
for retrieve tree-like structure of folder , it's files have use recursive function. somethiong this:
dofolders(driveapp.getfolderbyid('root folder id')); // recursive iteration function dofolders(parentfolder) { var childfolders = parentfolder.getfolders(); while(childfolders.hasnext()) { var child = childfolders.next(); // folder // go subfolders dofolders(child); } }
however, in case have no idea how use continuationtoken
.
question
how use continuationtoken
recursive folder iterator, when need go throw folder structure?
assumption
is make sense construct many tokens name based on id
of each parent
folder?
Comments
Post a Comment