Hello, right below I have a script function to send email when the schedule date reaches TODAY formula on Google Sheets. Now I just want to make sure there's no chance for sending email 2 times. And I wanted to appear SENT EMAIL when the email is sent .
Can someone help me and fix the script for me to be able to do that?
function myProbation() {
//by Mirzan
//this function runs based on daily trigger that will be set in project trigger.
//declare variable
var ss = SpreadsheetApp.getActiveSpreadsheet(); //declare the spreadsheet
var sheet = ss.getSheetByName("Sheet1"); //please change according to your sheet name
var range = sheet.getDataRange(); //to set the range as array
var values = range.getDisplayValues(); //to get the value in the array
var lastRow = range.getLastRow();
//array [R][C] started from [0][0]
var curDate = values[1][9]; //this to declare cell H2/today() value, [1][7] means cell [2][H]
var anyMatches = false;
var message = "";
//this to get the email from spreadsheet account
var email = Session.getActiveUser().getEmail();
var optionalEmail = values[2][1]; //this for
var targetRecipient= "";
//now we use FOR function for looping the process
var i;
var logContent = '';
for (i = 5; i < lastRow; i++) //set variable i as index that start from row 5 and continue down
{
var ProbationDueDate = values[i][6]; //get index value that set the send date
//Now the IF functions, if today matches the send date, send an email
//need to convert the date format to string first
if (ProbationDueDate.toString() == curDate.toString())
{
var NewHireName = values[i][0]; //[New Hire Name] cell A++
var NewHireEmail = values[i][1]; // [New Hire Email] cell B++
var SupervisorEmail = values[i][3]; //[Supervisor Email] cell D++
var HrbpEmail = values[i][5]; //[HRBP Email] cell F++
var SupervisorName = values[i][4]; //[supervisor name] cell E++
var DueDate = values[i][2]; //[probation end date] cell C++
var SendEmailDate = ProbationDueDate;
var message = values[i][7];
// if there is a match, set anyMatches to true so email gets sent
anyMatches = true;
// footer for check log message
logContent += "Content No. " + i + targetRecipient + " -- " + message + " ++++++++++++++++++++<br/>";
// Now using API to send the email
MailApp.sendEmail({
to: SupervisorEmail,
cc: HrbpEmail + "," + NewHireEmail + "," + optionalEmail,
subject: 'Probation Review Reminder ' + NewHireName,
htmlBody: message});
}
} // ends for loop
Logger.log(logContent);
}