当我 运行 响应命令时,我试图防止在 google 工作表中重复电子邮件响应

I'm trying to prevent duplicate email response in google sheets when I run response command

我从 Google 表格教程中获得了这段代码并且它有效,我想在我的同事填写 google 表格后发送一封电子邮件回复。电子邮件有效,因此第一步已完成,但即使我已指定它不发送重复电子邮件,它仍会继续发送电子邮件。我的sheet如下:

    Time Stamp/Email Address/Message/Complete for columns A-D
    date-time/test@email.com/   Test1/  EMAIL_SENT
    date-time/ test@email.com/  Test2/  EMAIL_SENT
    date-time/test@email.com/   Test3/  EMAIL_SENT
    // This constant is written in column C for rows for which an email
    // has been sent successfully.
    var EMAIL_SENT = 'EMAIL_SENT';

    /**
     * Sends non-duplicate emails with data from the current spreadsheet.
     */
    function sendEmails2() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var startRow = 2; // First row of data to process
      var numRows = 3; // Number of rows to process
      // Fetch the range of cells B1:D3
      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[1]; // Second column
        var message = row[2]; // Third column
        var emailSent = row[3]; // Fourth column
        if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
          var subject = 'Your PFB Request';
          MailApp.sendEmail(emailAddress, subject, message);
          sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);
          // Make sure the cell is updated right away in case the script is interrupted
          SpreadsheetApp.flush();
        }
      }
    }

var dataRange = sheet.getRange(startRow, 1, numRows, 3);

getRange 不是从零开始的方法。您的定义定义了一个只有 3 列宽的范围。它不包括 "Complete" 列。

将此更改为: var dataRange = sheet.getRange(startRow, 1, numRows, 4);

"Complete" 列将等同于行 [3](从零开始)。当前行 [3] 没有值,可能 "undefined",并且可以解释为什么 "IF" 语句不起作用。