我在运行以下代码时遇到错误:
无法发送电子邮件:没有收件人
这是正在运行或谷歌电子表格。我假设它要在电子邮件地址栏中查找电子邮件地址,在读完最后一个电子邮件地址后,它会转到下一个空白单元格并且看不到任何内容,然后给出一个错误。
如果是这种情况,如果电子邮件地址转到列中的最后一个地址,我怎么能停止查找?
错误在第18行:MailApp.sendEmail(emailAddress, subject, message);
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var startRow = 2;
var numRows = 1000;
var sheet = ss.getSheetByName ('Administrators')
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1];
var message = row[28];
var emailSent = row[29];
if (emailSent != EMAIL_SENT) {
var subject = "Tuition Reimbursement Status";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 30).setValue(EMAIL_SENT);
var sheet = ss.getSheetByName ('Teachers')
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1];
var message = row[28];
var emailSent = row[29];
if (emailSent != EMAIL_SENT) {
var subject = "Tuition Reimbursement Status";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 30).setValue(EMAIL_SENT);
var sheet = ss.getSheetByName ('Support Staff')
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1];
var message = row[28];
var emailSent = row[29];
if (emailSent != EMAIL_SENT) {
var subject = "Tuition Reimbursement Status";
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 30).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
}
}
}
}
答案 0 :(得分:0)
您的numRows应使用getLastRow()
函数 [Ref]设置,而不是将其设置为1000.
答案 1 :(得分:0)
“无法发送电子邮件:没有收件人”的解决方案
if (emailSent != EMAIL_SENT && emailAddress!="") { // Prevents sending duplicates