Google表格 - 根据其他单元格的文字发送电子邮件

时间:2017-09-30 16:52:23

标签: email text google-sheets cell send

我有一个脚本,可以为一系列单元格中的每个单元格发送电子邮件。为了防止在重新运行脚本时发送重复的电子邮件,每次发送电子邮件时,它都会在每行中标记一个单元格(C列)。请参阅'Sheet2'中的示例。

https://docs.google.com/spreadsheets/d/1_oMbZZjOhfdKuYNWOuyF3fao8yO6W3juFjUsFbFBJaU/edit?usp=sharing

但是,我想将其转换为“Sheet1”的脚本,只要相应行中的单元格包含特定文本,它就会发送电子邮件。在这个例子中,每当列I包含一个复选标记(✓)时,我想要一条消息(列E)与其相应的主题行(列D)一起发送。我还想防止重复的电子邮件,就像'Sheet2'中的示例一样。换句话说,一旦第I列包含复选标记,该邮件就会被发送,完成,并且不会重新发送,除非删除并重新输入复选标记。

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Sheet2');
  var startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = s.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 = "ta.ri...@gmail.com"; 
    var message = row[1];       // Second column
    var emailSent = row[2];     // Third column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = row[0];   // First column
      MailApp.sendEmail(emailAddress, subject, message);
      s.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    }
  }
}

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

function sendEmails2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Sheet1');
  var startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = s.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 = "ta.ri...@gmail.com"; 
    var message = row[1];       // Second column
    var emailSent = row[2];     // Third column
    if (row[8]) {  // Prevents sending duplicates
      var subject = row[0];   // First column
      MailApp.sendEmail(emailAddress, subject, message);
      s.getRange(startRow + i, 9).setValue("");
      SpreadsheetApp.flush();
    }
  }
}