因为这个产品是新产品,所以我期待开发一个应用程序,因此对API使用有任何限制,例如:
我编写了一个脚本来迭代Google云端硬盘文件夹中的大量文件.由于我对这些文件进行的处理,它超过了最大执行时间.当然,我写入脚本使用DriveApp.continueFileIterator(continuationToken):令牌存储在Project Properties中,当脚本运行时,它会检查是否有令牌,如果有则从令牌创建FileIterator,如果不是重新开始.
我发现的是,即使脚本使用延续令牌重新启动,它仍然从迭代开始时开始,尝试再次处理相同的文件,这浪费了后续执行的时间.我是否错过了一些重要的东西,如命令或方法,让它从它停止的地方开始?我是否应该在while(contents.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 …Run Code Online (Sandbox Code Playgroud) 由于 Drive API 配额、服务配额和脚本执行时间的限制,6 min将 Google Drive 文件操作拆分成块通常很重要。
我们可以用PropertiesService存储continuationToken为FolderIterator或FileIterator。这样我们就可以停止我们的脚本,并在下一次运行时从我们停止的地方继续。
// Logs the name of every file in the User's Drive
// this is useful as the script may take more that 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 == …Run Code Online (Sandbox Code Playgroud)