防止发送重复项 - 不起作用

时间:2017-06-19 12:18:34

标签: google-apps-script gmail-api

我创建了一个脚本,根据单元格值发送自动电子邮件,为审批工作流程创建。

脚本工作正常,但是,我无法阻止发送重复的电子邮件

请提供一些提示

// 
var now = new Date();//mail semt date
var EMAIL_SENT = "EMAIL_SENT";// mail sent status
var approvalstatus = 'Approved';// Approved Status 

function SecondaryApproval() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 100;   // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var Ticket = row[0];
var RequesterEmail = row[2];
var Item = row[3];
var emailAddress = row[6];  // First column
var message = row[7];       // Second column
var emailSent = row[8];     // Third column
var date = row[9];
var yesNo = row[5];
if (yesNo === approvalstatus) { 
  var subject = "Secondary Approval  ";
  GmailApp.sendEmail(emailAddress, subject, "Dear " +  + "RequesterEmail" +  Ticket + ", \n\nThis is an email report of your link. \n\nYour link is " + Item + " \n\nKind regards,\nName ", {
                     from: "Accounts@mydomain.com",
                     name: "Account"});
  sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
  sheet.getRange(startRow + i, 9).setValue(now);

  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
}
  else (emailSent === EMAIL_SENT); {  // Prevents sending duplicates
  }
}

1 个答案:

答案 0 :(得分:1)

您需要在发送电子邮件之前检查emailSent !== EMAIL_SENT

// 
var now = new Date();//mail semt date
var EMAIL_SENT = "EMAIL_SENT";// mail sent status
var approvalstatus = 'Approved';// Approved Status 

function SecondaryApproval() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 100;   // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var Ticket = row[0];
var RequesterEmail = row[2];
var Item = row[3];
var emailAddress = row[6];  // First column
var message = row[7];       // Second column
var emailSent = row[8];     // Third column
var date = row[9];
var yesNo = row[5];
if (yesNo === approvalstatus && emailSent !== EMAIL_SENT) { 
  var subject = "Secondary Approval  ";
  GmailApp.sendEmail(emailAddress, subject, "Dear " +  + "RequesterEmail" +  Ticket + ", \n\nThis is an email report of your link. \n\nYour link is " + Item + " \n\nKind regards,\nName ", {
                     from: "Accounts@mydomain.com",
                     name: "Account"});
  sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
  sheet.getRange(startRow + i, 9).setValue(now);

  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
}
}