【发布时间】:2014-02-07 06:07:06
【问题描述】:
我编写了一个 Google 应用程序电子表格脚本,该脚本应该从我的驱动器中提取一个 pdf,并将其作为附件通过电子邮件发送给许多人,每个人都将通过电子邮件发送一个独特的文件。数据在电子表格中。第一列是收件人的电子邮件地址,第二列是必须附加的 pdf 文件名,第三列是人名。
function sendPDFs() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getDataRange().getLastRow() -1; // Number of rows to process
// Fetch the range of cells A2:C3
var nummails = 0
var numdrive = 0
var checkCol = 4
var dataRange = sheet.getRange(startRow, 1, numRows, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = "Some subject";
var body = 'Dear ' + row[2] + '\n\nPlease find your ' + subject + ' \n\nThank you \n\n XYZ'
var attachment = row[1]; // Second column.
var files = DriveApp.getFilesByName(attachment); // Get all files with name.
var blobs = []; // Array for attachment.
// Move files into blobs
while (files.hasNext()) {
var file = files.next();
blobs.push(file.getAs("application/PDF"));
}
// dont sent to invalid email ids or those without email ids
if (emailAddress == "" || emailAddress.indexOf("@") <= -1){
sheet.getRange(i+2, checkCol+1).setValue('Not Emailed')
numdrive = numdrive+1
}
else{
Utilities.sleep(1000)
MailApp.sendEmail(emailAddress, subject, body, {attachments: blobs, name: "XYZ" })
sheet.getRange(i+2, checkCol+1).setValue('Emailed')
nummails = nummails +1
}
}
Browser.msgBox( nummails+ " Mails sent successfully.")
}
我在序列化继续时收到错误意外异常。但是,电子邮件已发送。看起来代码有效,但我不知道为什么会出错。我已经看到存在相同错误的现有线程,但它们没有帮助。
感谢有关如何解决此问题的任何帮助。
【问题讨论】: