在第一栏Google脚本中输入任何值时发送电子邮件

时间:2019-05-24 10:34:32

标签: email google-apps-script email-notifications

当数据添加到该特定行的第一列时,我希望向收件人发送电子邮件(clientEmail)。第一列中的数据将是数字和字母的混合。我尝试使用下面的代码使用其他方法,但永远无法让它仅在第一列的值包含值时才发送。

var EMAIL_DRAFTED = "EMAIL DRAFTED";

function draftMyEmails() {
var sheet = SpreadsheetApp.getActiveSheet(); // Use data from the active 
sheet
var startRow = 2;                            // First row of data to process
var numRows = sheet.getLastRow() - 1;        // Number of rows to process
var lastColumn = sheet.getLastColumn();      // Last column
var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
var data = dataRange.getValues();            // Fetch values for each row in the range

// Work through each row in the spreadsheet
for (var i = 0; i < data.length; ++i) {
var row = data[i];  
// Assign each row a variable
var clientName = row[1];                // Col B: Client name
var clientEmail = row[2];               // Col C: Client email
var message1 = row[3];                       // Col D: First part of message
var message2 = row[4];                   // Col E: Second part of message
var emailStatus = row[lastColumn - 1];  // Col F: Email Status

// Prevent from drafing duplicates and from drafting emails without a recipient
if (emailStatus !== EMAIL_DRAFTED && clientEmail) {  

  // Build the email message
  var emailBody =  '<p>Hi ' + clientName + ',<p>';
      emailBody += '<p>' + message1 + ', your requested data, ' + message2 + ', is ready.<p>';


  //Send the emaiil
  MailApp.sendEmail(
    clientEmail,            // Recipient
    'Here is your data',  // Subject
    '',                     // Body (plain text)
    {
    htmlBody: emailBody    // Options: Body (HTML)
    }
  );

  sheet.getRange(startRow + i, lastColumn).setValue(EMAIL_DRAFTED); // Update the last column with "EMAIL_DRAFTED"
  SpreadsheetApp.flush(); // Make sure the last cell is updated right away
}
}
}

1 个答案:

答案 0 :(得分:0)

通过更改for循环开始,了解++ii++之间的区别,在这种情况下,您希望使用后者。参见:difference between ++i and i++

for (var i = 0; i < data.length; i++) {

此后,您所需要做的就是在您的if语句中添加有关该列的检查。注意:您可以像定义其他变量一样单独定义它。我将提供2个示例,您可以选择要使用的示例,两者的功能相同。

//option 1
if (emailStatus !== EMAIL_DRAFTED && clientEmail && row[0]) { 

//option 2
var checkData = row[0];
if (emailStatus !== EMAIL_DRAFTED && clientEmail && checkData) {

最后,您的代码应如下所示:

var EMAIL_DRAFTED = "EMAIL DRAFTED";

function draftMyEmails() {
  var sheet = SpreadsheetApp.getActiveSheet(); // Use data from the active sheet
  var startRow = 2;                            // First row of data to process
  var numRows = sheet.getLastRow() - 1;        // Number of rows to process
  var lastColumn = sheet.getLastColumn();      // Last column
  var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn); // Fetch the data range of the active sheet
  var data = dataRange.getValues();            // Fetch values for each row in the range

  // Work through each row in the spreadsheet
  for (var i = 0; i < data.length; i++) {
    var row = data[i];  
    // Assign each row a variable
    var clientName = row[1];                // Col B: Client name
    var clientEmail = row[2];               // Col C: Client email
    var message1 = row[3];                  // Col D: First part of message
    var message2 = row[4];                  // Col E: Second part of message
    var emailStatus = row[lastColumn - 1];  // Col F: Email Status

    // Prevent from drafing duplicates and from drafting emails without a recipient
    if (emailStatus !== EMAIL_DRAFTED && clientEmail && row[0]) { 

      // Build the email message
      var emailBody =  '<p>Hi ' + clientName + ',<p>';
      emailBody += '<p>' + message1 + ', your requested data, ' + message2 + ', is ready.<p>';


      //Send the emaiil
      MailApp.sendEmail(
        clientEmail,            // Recipient
        'Here is your data',    // Subject
        '',                     // Body (plain text)
        {
          htmlBody: emailBody   // Options: Body (HTML)
        }
      );

      sheet.getRange(startRow + i, lastColumn).setValue(EMAIL_DRAFTED); // Update the last column with "EMAIL_DRAFTED"
      SpreadsheetApp.flush(); // Make sure the last cell is updated right away
    }
  }
}