美好的一天!我需要调试以下错误的帮助:“发送电子邮件失败:无收件人”。该功能根据范围的值发送电子邮件。请检查下面的代码。
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Send-Emails"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), 5);
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var subject = 'DepEd Email/Account';
var FirstName = rowData[0];
var LastName = rowData[1];
var GeneratedEmail = rowData[2];
var Password = rowData[3];
var emailAddress = rowData[4];
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: '<h3>' + 'Good day Mr./Mrs./Ms. '+ FirstName + ' ' + LastName +'!</h3>' +
'<p>Here is your email/account (email and password):</p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="gEmail">' + GeneratedEmail + '</a></strong></span></p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="pword">' + Password + '</a></strong></span></p>' +
'<p>You can now login to <a href="https://mail.google.com">mail.google.com</a> to check your account.</p>' +
'<p>If you want to change password, login first then follow this <a href="https://myaccount.google.com/intro/signinoptions/password">link</a>.</p>'
});
}
ss.getSheetByName('Form Responses 2').getRange(254, 12, sheet.getLastRow(), 1).copyTo(ss.getSheetByName('Form Responses 2').getRange(254, 14, sheet.getLastRow(), 1));
}
该函数运行并发送所有电子邮件,但随后出现错误。我怀疑它循环到空行并且找不到收件人,但是我已经具有带有sheet.getLastRow()的dataRange。
答案 0 :(得分:0)
通常,Range#getValues()
调用的末尾会返回一个空白行。如果行中没有数据,我们可以添加一条if语句以跳过发送:
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Send-Emails"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange(2, 1, sheet.getLastRow(), 5);
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
if(!rowData[4]) continue; //Skip this iternation if no email
var subject = 'DepEd Email/Account';
var FirstName = rowData[0];
var LastName = rowData[1];
var GeneratedEmail = rowData[2];
var Password = rowData[3];
var emailAddress = rowData[4];
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: '<h3>' + 'Good day Mr./Mrs./Ms. '+ FirstName + ' ' + LastName +'!</h3>' +
'<p>Here is your email/account (email and password):</p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="gEmail">' + GeneratedEmail + '</a></strong></span></p>' +
'<p><span style="background-color: #ffff00;"><strong><a name="pword">' + Password + '</a></strong></span></p>' +
'<p>You can now login to <a href="https://mail.google.com">mail.google.com</a> to check your account.</p>' +
'<p>If you want to change password, login first then follow this <a href="https://myaccount.google.com/intro/signinoptions/password">link</a>.</p>'
});
}
ss.getSheetByName('Form Responses 2').getRange(254, 12, sheet.getLastRow(), 1).copyTo(ss.getSheetByName('Form Responses 2').getRange(254, 14, sheet.getLastRow(), 1));
}
或者,我们无法获取最后一行:var dataRange = sheet.getRange(2, 1, sheet.getLastRow()-1, 5);