I wrote a script to iterate a large number of files in a Google Drive folder. Due to the processing that I do in these files, it exceeds the maximum execution time. Naturally, I wrote in a script to use DriveApp.continueFileIterator (continueationToken): the token is stored in the project properties, and when the script starts, it checks if there is a token if it creates a FileIterator from the token if it does not start again.
What I found, even if restarting the script with the continuation of the token, it still starts from the beginning of the iteration, trying to process the same files that lose time for subsequent executions. Did I miss something vital, like in a team or method, to start from where it left off? Should I update the continuation token at different stages while while (contents.hasNext ())?
Here, the sample code has been reduced to give you an idea:
function listFilesInFolder() { var id= '0fOlDeRiDg'; var scriptProperties = PropertiesService.getScriptProperties(); var continuationToken = scriptProperties.getProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN'); var lastExecution = scriptProperties.getProperty('LAST_EXECUTION'); if (continuationToken == null) { // first time execution, get all files from drive folder var folder = DriveApp.getFolderById(id); var contents = folder.getFiles(); // get the token and store it in a project property var continuationToken = contents.getContinuationToken(); scriptProperties.setProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN', continuationToken); } else { // we continue to import from where we left var contents = DriveApp.continueFileIterator(continuationToken); } var file; var fileID; var name; var dateCreated; while(contents.hasNext()) { file = contents.next(); fileID = file.getId(); name = file.getName(); dateCreated = file.getDateCreated(); if(dateCreated > lastExecution) { processFiles(fileID); } } // Finished processing files so delete continuation token scriptProperties.deleteProperty('IMPORT_ALL_FILES_CONTINUATION_TOKEN'); var currentExecution = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd HH:mm:ss"); scriptProperties.setProperty('LAST_EXECUTION',currentExecution); };
google-drive-sdk google-apps-script
user3412868
source share