var ss_id = "xxxxxxx";//Update with your own sheet.
var sheetName = "A"; //Change the sheet name
var sheetName = "B"; //Change the sheet name
var dueDateCell = "C2"; //The cell that indicates the due date.
var startRow = 6; //First row after your header.
var reminderColStart = 6; //What row your reminder starts on.
//Email data
var subject = "URGENT Attendance Reminder "; //The email subject
/******************************************************************************************
* The main run function. It loops thorugh each user and calls the following functions:
* - sendEmail()
* - openCompleteWindow()
*/
function main() {
var ss = SpreadsheetApp.openById(ss_id);
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
var dueDate = sheet.getRange(dueDateCell)
.getValue();
dueDate = Utilities.formatDate(dueDate, "GMT+7", "EEE, dd MMM yyyy");
var range = sheet.getRange(startRow,1,lastRow - startRow+1, lastCol);
var rangeVals = range.getValues();
var badEmailList = [];
var reminder = [];
//Loop through range values. If tick box is not ticked continue.
for(row in rangeVals){
if(rangeVals[row][2]){
var badEmail = sendEmail(rangeVals[row], ss.getUrl, dueDate);
//Update Redminder Sent column F(5) and Last Reminder Date column G(6);
if(!badEmail){
rangeVals[row][5] += 1;
rangeVals[row][6] = new Date();
}else{
badEmailList.push(badEmail);
};
};
reminder.push([rangeVals[row][5],rangeVals[row][6]])
};
var reminderRange = sheet.getRange(startRow, reminderColStart,lastRow - startRow+1, 2);
reminderRange.setValues(reminder);
var complete = openCompleteWindow(badEmailList);
};
/******************************************************************************************
* Google trigger function. When the sheet is opened, a custom menu is produced.
*
*/
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu("Send Reminder")
.addItem("Send Reminder", "main")
.addToUi();
};
/******************************************************************************************
* Sends HTML email to user who has not completed task.
* The HTML template is created from the email.html file.
*
* @param {array} overDueStaff - 2d array of overdue staff with full row data.
* @param {string} sheetURL - url of current Spreadsheet.
* @param {date} dateDue - formatted date.
*
* @return {array} - Returns array of email addresses that could not be attemplted due to errors.
*/
function sendEmail(overDueStaff, sheetURL, dateDue){
//Staff Details
var staff = {
"name": overDueStaff[0],
"email": overDueStaff[1],
"time": overDueStaff[8],
"ZoomLink": overDueStaff[9],
"clientName": overDueStaff[10],
"dueDate": dateDue,
"daysOverdue": overDueStaff[4]
};
staff.daysOverdue = (staff.daysOverdue > 1) ? staff.daysOverdue + " days" : staff.daysOverdue + " day";
var subject1 = staff.name;
var subj = subject.concat(subject1);
var body = HtmlService.createTemplateFromFile("email");
body.name = staff.name;
body.due = staff.dueDate;
body.overdue = staff.daysOverdue;
body.time = staff.time;
body.ZoomLink = staff.ZoomLink;
body.clientName = staff.clientName;
body.Tasktype = staff.Tasktype;
try{
MailApp.sendEmail({
to: staff.email,
subject: subj,
htmlBody: body.evaluate().getContent(),
});
}
catch(error){
return staff.email;
}
};
/******************************************************************************************
* Opens a completion alert once the script is run with a list of emails that could not be sent.
*
* @param {string} badEmailList - array of email addresses that could not be attemplted due to errors.
*
*/
function openCompleteWindow(badEmailList){
SpreadsheetApp.getUi().alert(
"Emails Sent\n"+
"The following emails could not be sent:\n"+
badEmailList.join("\n")
);
};