问题
Good day! I need help in debugging this error: "Failed to send email: no recipient". The function sends emails based on the values of a range. Please check the code below.
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));
}
The function runs and sends all emails but then it gives the error. I suspect it loops to an empty row and can't find a recipient but I already have dataRange with sheet.getLastRow().
回答1:
Often there will be a blank row returned at the end from a Range#getValues()
call. We can add an if statement to skip sending if there is no data in a row:
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));
}
Alternatively, we can not grab the last row: var dataRange = sheet.getRange(2, 1, sheet.getLastRow()-1, 5);
来源:https://stackoverflow.com/questions/54819561/failed-to-send-email-no-recipient