Given - A have sheet of employees who have requested for reimbursement.
Requirement - We should sent out an email to the employee when their reimbursement is processed
So, the sheet is as follows:
The function for sending out an email is as follows:
function getDataSheet() {
sheet = SpreadsheetApp.getActiveSheet();
startRow = 2; // First row of data to process
numRows = 100; // Number of rows to process
startCol = 1; //First column of data to process
numCols = 15; // Number of columns to process
var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
return data;
}
function getMessage(name, amount) {
return "Hello " + name + ", Your reimbursement for amount " + amount + " is processed successfully";
}
function sendEmail() {
var emailSent = "Yes";
var reimbursed = "Yes";
var emailCol = 5;
var data = getDataSheet();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var isReimbursed = row[3];
var isEmailSent = row[4];
var name = row[0];
var amount = row[2];
if(isReimbursed == reimbursed && isEmailSent != emailSent) {
var subject = "Reimbursement details";
var message = getMessage(name, amount);
var recipientEmail = row[1];
MailApp.sendEmail(recipientEmail, subject, message);
//Sheet range starts from index 1 and data range starts from index 0
sheet.getRange(1 + i, emailCol).setValue(emailSent);
}
}
}