[英]Google sheets: send email data range question
I am using this script to send reminder emails.我正在使用此脚本发送提醒电子邮件。 It works when I run it manually, but when I try to run it on a trigger, it fails.
当我手动运行它时它可以工作,但是当我尝试在触发器上运行它时,它会失败。
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmail() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Job Checkup Tool');
var startRow = 4; // First row of data to process
var numRows = 13; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
var subject = 'Job Checkup Reminder';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
The problem appears to be a result of the script finding no email address in column 1.问题似乎是脚本在第 1 列中找不到 email 地址的结果。
When I run it manually, though it has an error, it will successfully send the emails even if there isn't an email value in column 1.当我手动运行它时,虽然它有错误,但即使第 1 列中没有 email 值,它也会成功发送电子邮件。
When the trigger runs it, it shows the same error当触发器运行它时,它显示相同的错误
Exception: Failed to send email: no recipient
例外:发送失败 email:没有收件人
but doesn't send any emails.但不发送任何电子邮件。
If I adjust the range to focus only on rows with an email address in column 1, it runs great manually and via the trigger.如果我将范围调整为仅关注第 1 列中具有 email 地址的行,则它可以通过触发器手动运行。
Is there a good way to request the script to send the email IF there is an email present within column 1 but then ignore the other rows that don't have an email yet?如果第 1 列中存在 email 但忽略其他还没有 email 的行,是否有一种好方法可以请求脚本发送 email?
Replace代替
if (emailSent !== EMAIL_SENT) {
by经过
if (emailAddress !== '' && emailSent !== EMAIL_SENT) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.