简体   繁体   中英

Google App Script IF statement for Sending Emails

Just want to ask some help regarding with my coding in Google App Script.

Objective: Am trying to send emails once I satisfy the 3 condition (the 2 condition is in the same column). And once the script already sent an email to that specific control number the script shouldn't send again to that transaction. However it still sending emails even if the script already sent to that transaction. I can't find where the problem is or if am not satisfying the condition I set.

Column SubmissionStatus have "Final, Cancelled, Draft" Column processBy have ("ADMIN" only)

  if (submissionStatus !== "Draft" && processBy !== "") { // Prevents sending duplicates  
  var subject = "Cash_Advance ";
  MailApp.sendEmail(currentEmail, subjectline, messageBody);
  sheet.getRange(startRow + i, 38).setValue(EMAIL_SENT +" "+ Date()); // column AL
  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
  } //close if statement

  if (submissionStatus !== "Cancelled" && processBy !== "") { // Prevents sending duplicates  
  var subject = "Cash_Advance ";
  MailApp.sendEmail(currentEmail, subjectline, messageBody);
  sheet.getRange(startRow + i, 38).setValue(EMAIL_SENT +" "+ Date()); // column AL
  // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
  } //close if statement

You can simply merge those two if statement into one, maybe that's why it is sending email more than once.

By using submissionStatus === "Final"

In context of your latest edit:-

Column SubmissionStatus have "Final, Cancelled, Draft" Column processBy have ("ADMIN" only)

Modification:

if (submissionStatus === "Final" && processBy !== "") // Prevents sending duplicates  
{ 
          var subject = "Cash_Advance ";
          MailApp.sendEmail(currentEmail, subjectline, messageBody);
          sheet.getRange(startRow + i, 38).setValue(EMAIL_SENT +" "+ Date()); // column AL
          // Make sure the cell is updated right away in case the script is interrupted
          SpreadsheetApp.flush();
 } //close if statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM