【发布时间】:2021-10-24 10:56:27
【问题描述】:
我有一个脚本,它从 Google 表单创建 PDF 并将其保存到特定文件夹。这完全没问题。
另一个脚本应该按名称获取特定文件夹中的文件以将其作为附件发送。每次运行此脚本时,都会有始终一个唯一的文件。 但是,有时脚本会在“var PDF_Participation”行遇到异常“无法检索下一个对象:迭代器已到达末尾”,尽管确实存在所需的文件。如果我手动运行脚本到“关键行”并记录文件名,它将记录正确的文件名。我真的不明白为什么脚本会不定期地失败。
//get the email template from google doc using the url of the file
var templateUrl = 'https://docs.google.com/document/d/1I4igdjEYeV46fGbo_MiMClrmJGUrN73ZByCvNBvIVxM/edit'
//Get that template
var template = DocumentApp.openByUrl(templateUrl)
//Extract the text in the body of the document
var text = template.getBody().getText()
//Define Variables from Sheet
var sSheet = SpreadsheetApp.openById("1jGQMmLykfnL7t6JNQ7ZzRSlvEDQBJwvqMxCRpNYtZ20");
var FormResponses = sSheet.getSheetByName("Form Responses");
var LastRowRes = FormResponses.getRange(["A1:A"]).getValues().filter(String).length;
var Firstname= FormResponses.getRange(LastRowRes,3).getValue();
var Lastname = FormResponses.getRange(LastRowRes, 4).getDisplayValue();
var Id = FormResponses.getRange(LastRowRes, 5).getDisplayValue();
var Recipient = FormResponses.getRange(LastRowRes,2).getValue();
var Subject = "Participation"
var PDFfolder = DriveApp.getFolderById("1Y9fhQQjbfBu83Vq1UbvZFB3Cdub9dFGI");
var PDF_Participation = PDFfolder.getFilesByName(Id+" "+Lastname+", "+Firstname+" - Participation").next();
//send mails
var emailBody = text.replace('{Firstname}', Firstname)
MailApp.sendEmail(Recipient, Subject, emailBody, {
noReply: true,
name: "Organization Team",
replyTo: "example@google.com",
attachments: [PDF_Participation.getAs(MimeType.PDF)]
})}
有人可以帮忙吗? :)
【问题讨论】:
-
什么是迭代器?请提供代码的必要部分。
-
迭代器是下一行中的 next(): var PDF_Participation = PDFfolder.getFilesByName(Id+" "+Lastname+", "+Firstname+" - Participation").next() (参见上面提供的脚本)
-
不,
next()不是迭代器。getFilesByName是,但你没有迭代它。 Supposed to have awhile -
好的,谢谢!我是那个领域的绝对初学者。所以我会努力做到这一点! :)
标签: file google-apps-script next drive getfiles