无法检索下一个对象:在共享的云端硬盘上进行迭代时,迭代器已达到结束错误 [英] Cannot retrieve the next object: iterator has reached the end error when iterating over shared Drive

查看:43
本文介绍了无法检索下一个对象:在共享的云端硬盘上进行迭代时,迭代器已达到结束错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件夹共享在Google共享驱动器上不起作用,因此我试图通过Google Apps脚本自动进行共享.

Folder sharing does not work on Google Shared Drive, so I am trying to automate sharing through Google Apps Script.

我尝试参考几个示例来编写代码,但我不断遇到错误并提出问题.

I tried to write code with reference to several examples, but I kept getting errors and asked questions.

我收到一个错误消息:

无法检索下一个对象:迭代器已到达末尾",我确认未设置某些文件的共享.

'Cannot retrieve the next object: iterator has reached the end', and I confirmed that sharing of some files was not set.

下面是我编写的代码.我该如何解决这个问题?

Below is the code I wrote. How can I fix this problem?

function myFunction() {
  var folderid = ""
  var folder = DriveApp.getFolderById(folderid);
  var files = folder.getFiles();
  while (files.hasNext()) {
    Logger.log(files.next().getName());
    files.next().setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
  }
}

推荐答案

问题:

.next()返回文件夹中的 next 文件.如果 folder 有1个文件,则 files.hasNext()返回true,而 files.next()返回 file1 .再次调用 files.next()会返回

Issue:

.next() returns the next file in the folder. If folder has 1 file, files.hasNext() returns true and files.next() returns file1. Calling files.next() again returns

无法检索下一个对象:迭代器已到达末尾'

Cannot retrieve the next object: iterator has reached the end'

  • 首次调用iterator:file#1

    • First call to iterator:file#1

      Logger.log(files.next().getName());

      Logger.log(files.next().getName());

    • 对迭代器的第二次调用:下一个文件(不可用)

    • Second call to iterator: next file(not available)

      files.next().setSharing(DriveApp.Access.ANYONE_WITH_LINK,DriveApp.Permission.VIEW);

      files.next().setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

    • 如果文件夹中有5个文件,则 Logger 将记录文件1,3和5的名称.将在文件2和4上设置共享.

      If folder had 5 files, Logger would've logged names of files 1,3 and 5. Sharing would've been set on files 2 and 4.

      使用 files.hasNext()

        while (files.hasNext()) {
          const nextFile = files.next();//one call
          Logger.log(nextFile.getName());
          nextFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
        }
      

      参考文献:

      • 迭代器
      • 文件迭代器
      • 这篇关于无法检索下一个对象:在共享的云端硬盘上进行迭代时,迭代器已达到结束错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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