首页 > 解决方案 > Google 驱动器脚本异常“异常:无法检索下一个对象:迭代器已到达末尾。”

问题描述

H,我创建了一个脚本,如果文件存在,我会在其中检查谷歌驱动器上的目标文件夹我获取文件的 ID,否则我在那里创建文件并获取 ID,我面临的问题是它有时可以完美运行,但是有时我收到异常“异常:无法检索下一个对象:迭代器已到达末尾。” 我可以确认该文件位于目标文件夹中,但仍会发生此异常。如果创建文件有任何延迟,我已经使用了睡眠功能。我也检查了其他人分享的几个问题。 列出文件时出现“无法检索下一个对象:迭代器已结束”错误

   //Copy File to Destination
function CopyFileNew(file,dest,source,filename){

  var destination = DriveApp.getFolderById(dest);
  var filecheck  = destination.getFilesByName(filename);

  if(!filecheck.hasNext()){
    destination.addFile(file);//file is undefined

    Utilities.sleep(2000);
    var newfile=destination.getFilesByName(filename);
    if(newfile.hasNext()){
      Utilities.sleep(2000);
      return "https://drive.google.com/open?id="+newfile.next().getId(); // HERE I GET THE ERROR
    }
  }
  else{
    return "https://drive.google.com/open?id="+filecheck.next().getId(); 
  }
}

标签: google-apps-scriptgoogle-sheetsgoogle-drive-api

解决方案


推荐阅读