【问题标题】:Exception: Cannot retrieve the next object: iterator has reached the end. Line 26 Error异常:无法检索下一个对象:迭代器已到达末尾。第 26 行错误
【发布时间】:2021-08-29 17:35:11
【问题描述】:
function sendMail() {
  var ofo = 1;
  var restaurant = 2;
  var name = 3;
  var email = 4;
  var sent = 5;

  var emailTemp = HtmlService.createTemplateFromFile("Email");

  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invalid Login MM Pull Internal");

  var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();

  data.forEach(function(row){

    emailTemp.ofo = row[ofo];
    var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf");
    var htmlMessage = emailTemp.evaluate().getContent();
    var startRow = 2;
    for (var i = 0; i < data.length; ++i){
    if (row[sent] !== "EMAIL_SENT"){
        GmailApp.sendEmail(row[email], "Otter Onboarding: Invalid " + row[ofo] + " Logins Submitted", 
        "Your email doesn't support HTML, please trying using another browser.",
        {name: "The Otter Onboarding team", 
        htmlBody: htmlMessage, replyTo: "onboarding@tryotter.com",
        attachments: [file.next().getAs(MimeType.PDF)]
        });
        ws.getRange(2 + i,6).setValue("EMAIL_SENT");
      };
    };
     });
}

我正在尝试将此 PDF 附加到我的电子邮件中,但它一直说第 26 行出现错误:异常:无法检索下一个对象:迭代器已到达末尾。

【问题讨论】:

  • 第 26 行是哪一行? (我们看不到您的行号)。
  • 我可以看到 file.next() 而不检查 file.hasNext() - 因此您的文件迭代器可能只是不断超出可迭代数据的末尾。见here。如果您只希望获得一个 PDF 文件,请在进入for 循环之前获得该文件。
  • 如果现有的评论或答案不能为您解决问题,您可以添加Minimal Reproducible Example吗?

标签: google-apps-script google-sheets


【解决方案1】:

只是猜测。而不是这个:

var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf");

...

attachments: [file.next().getAs(MimeType.PDF)]

你可能需要这个:

var file = DriveApp.getFilesByName("Guide to Resetting Online Delivery Passwords.pdf").next();

...

attachments: [file.getAs(MimeType.PDF)]

getFiles() 为您提供了一个迭代器(类似于数组)。 .next() 方法为您提供了迭代器的第一个(实际上是唯一的)元素。

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2017-03-29
    • 2021-10-24
    • 2022-06-13
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多