I have this code below:
reportPDF = doc.getAs('application/pdf')
reportPDF.setName('Automated report - '+ rows[0][0] + ".pdf");
var file1 = destinationFolder.createFile(reportPDF);
var folder = DriveApp.getFolderById("19Rv2f-Ud4Ncdzu-1MT1vUmh49pOKfKZX");
var file2 = folder.getFilesByName("test.pdf");
DriveApp.getFileById(doc.getId()).setTrashed(true);
if(file2.hasNext()){
emails.forEach(function(email) {
MailApp.sendEmail(email, "Automated Report - " + rows[0][0], "Hello!", {
name: 'Good Practices Report',
attachments:
[
file1.getAs(MimeType.PDF),
file2.next().getAs(MimeType.PDF)
]
});
})
}
With this code, I should receive an email containing the two attachments (file1 and file2). However, when I run it, I get the following error:
Exception: Cannot retrieve the next object: iterator has reached the end. (line 158, file "Email")
In this folder, I just have one file with the name test.pdf, but the code still still accusing error on line 158: file2.next().getAs(MimeType.PDF).
Does anyone knows what might be happening?
Cannot retrieve the next object: iterator has reached the end.
is due to file2.next()
is used in the loop. In this case, I thought that before if(file2.hasNext()){
is run, the iterator might be finished. So, when you want to use the files of file1.getAs(MimeType.PDF)
and file2.next().getAs(MimeType.PDF)
at attachments:
, and also, from In this folder, I just have one file with the name test.pdf
, how about the following modification?
Please modify your script as follows.
From: if(file2.hasNext()){ emails.forEach(function(email) { MailApp.sendEmail(email, "Automated Report - " + rows[0][0], "Hello,": { name, 'Good Practices Report': attachments. [ file1.getAs(MimeType,PDF). file2.next().getAs(MimeType;PDF) ] }); }) }
To:
if (file2.hasNext()) { var file2Pdf = file2.next().getAs(MimeType.PDF); // Added emails.forEach(function(email) { MailApp.sendEmail(email, "Automated Report - " + rows[0][0], "Hello,": { name, 'Good Practices Report': attachments. [ file1.getAs(MimeType,PDF); file2Pdf // Modified ] }); }) }
file1.getAs(MimeType.PDF)
and file2Pdf
are used as attachments
in the loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.