小编use*_*868的帖子

正确使用DriveApp.continueFileIterator(continuationToken)

我编写了一个脚本来迭代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)

google-apps-script google-drive-api

8
推荐指数
2
解决办法
4866
查看次数