Google-Apps-Script 异常:无法检索下一个对象:迭代器已到达末尾 [英] Google-Apps-Script exception: Cannot retrieve the next object: iterator has reached the end

查看:21
本文介绍了Google-Apps-Script 异常:无法检索下一个对象:迭代器已到达末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个谷歌应用脚​​本,其中列出了我的谷歌驱动器中的所有文件夹和文件.下面的代码返回 - 异常:无法检索下一个对象:迭代器已到达末尾. 循环文件时.这适用于大多数文件夹,直到它因异常而失败.关于导致这个爆炸的任何想法?

I'm trying to write a google app script which lists all folders and files in my google drive. The code below returns - Exception: Cannot retrieve the next object: iterator has reached the end. while looping through files. This works on most folders until it fails on the exception. Any ideas on what causes this to bomb out?

function generateFolderTree() {
  try {
    var parentFolder = DriveApp.getRootFolder();
    getChildFolders(parentFolder);
  } catch (e) { 
    Logger.log(e.toString());
  }
}

function getChildFolders(parent) {
  var childFolders = parent.getFolders();
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    Logger.log("Folder ID: " + childFolder.getId());
    Logger.log("Folder: " + childFolder.getName());

    var files = childFolder.getFiles();

    while (files.hasNext()) {  
      // Print list of files inside the folder
      Logger.log("--> File Name:  " + files.next().getName());
      Logger.log("--> Owner:  " + files.next().getOwner().getEmail());
    }

    // Recursive call for any sub-folders
    getChildFolders(childFolder);
  } 
}

推荐答案

我认为出于某种原因,调用 next() 两次就像在寻找另一个文件.因此,如果文件夹中只有一个文件,那么下次您调用它时,在您寻找所有者的情况下,它会出错.您在文件夹循环中的想法是正确的,将文件循环更改为此.

I think for some reason, calling next() twice is like looking for another file. So, if there is only one file in the folder, then the next time you call it, in your case looking for the owner it errors out. You had the right idea in the folder loop, change the file loop to this.

while (files.hasNext()) {  
      var childFile = files.next();
      // Print list of files inside the folder
      Logger.log("--> File Name:  " + childFile.getName());
      Logger.log("--> Owner:  " + childFile.getOwner().getEmail());
    }

这篇关于Google-Apps-Script 异常:无法检索下一个对象:迭代器已到达末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆