我写了一个脚本,可以通过在谷歌云端硬盘文件夹有大量的文件进行迭代。由于我对这些文件所做的处理超出了最大执行时间。当然,我写入脚本使用DriveApp.continueFileIterator(continuationToken):令牌存储在项目属性中,当脚本运行时,它会检查是否有令牌,如果存在则从令牌创建FileIterator,如果不是则重新开始。正确使用
我发现了什么是即使脚本与它仍然具有迭代的开头开始延续标记重新启动,再次尝试处理相同的文件,这是浪费时间的后续执行。我是否错过了一些至关重要的命令或方法,使它从离开的地方开始?我是否应该在while(content.hasNext())循环中的各个阶段更新延续令牌?
这里的瘦身,给你一个想法的示例代码:
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);
};
第一次执行或完成时是否超时? – Jonathon