简体   繁体   English

谷歌表:发送 email 数据范围问题

[英]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.

相关问题 我正在尝试在 email 中将谷歌表格中的范围作为 pdf 发送。 我的脚本卡在 getid() - I am trying to send a range in google sheets as a pdf in an email. My script is getting stuck on getid() Google表格-根据条件发送电子邮件 - Google Sheets - Send Email based on condition 使用谷歌表和脚本发送电子邮件 - use google sheets and script to send email 谷歌应用脚本表 if 语句发送 email - Google apps script sheets if statement to send email 从 Google 表格范围内以图像/博客/png 形式发送迷你图 - Email Sparkline graphs as image/blog/png from Google Sheets range 提取列符合条件的行,然后通过电子邮件发送(Google表格) - Fetch rows with column matching a criteria and send them by email (Google Sheets) 更改为一列后,从Google表格发送电子邮件 - Send an email from Google Sheets after a change to one column 根据谷歌表格中的单元格值更改将 email 发送到特定地址 - Send email to specific addresses based on cell value change in google sheets 如果 3 列中的任何日期 = 今天,Google Sheets Appscript 发送电子邮件 - Google Sheets Appscript Send Email if any of the Dates in 3 columns = today Google图表+ Google表格:指定范围无法正确识别数据 - Google Charts + Google Sheets: Specified range does not identify data correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM