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

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

问题描述

我正在尝试编写一个谷歌应用程序脚本,其中列出了我的谷歌驱动器中的所有文件夹和文件。下面的代码返回 - 异常:无法检索下一个对象:迭代器到达结尾。在循环文件时。这适用于大多数文件夹,直到它在例外情况下失败。关于是什么导致它被炸出来的任何想法?

  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(文件夹ID:+ childFolder.getId());
Logger.log(Folder:+ childFolder.getName());

var files = childFolder.getFiles();

while(files.hasNext()){
//打印文件夹内的文件列表
Logger.log( - >文件名:+ files。 。下一个()的getName());
Logger.log( - >所有者:+ files.next()。getOwner()。getEmail());
}

//递归调用任何子文件夹
getChildFolders(childFolder);


$ / code $ / pre
$ b $ p感谢。
<我认为出于某种原因,调用 next()两次就像寻找另一个文件一样。因此,如果文件夹中只有一个文件,那么下次打电话时,在您的情况下寻找所有者,它会出错。

  while(files.hasNext()){}在文件夹循环中有正确的想法,将文件循环更改为此。 
var childFile = files.next();
//打印文件夹内的文件列表
Logger.log( - >文件名:+ childFile.getName());
Logger.log( - >所有者:+ childFile.getOwner()。getEmail());
}


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);
  } 
}

Thanks.

解决方案

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天全站免登陆