Email Reminder AppScript
I created this AppScript for Google Sheets to fit the need of helping my team make sure they meet deadlines or at least send out the proper communication before a deadline is reached. The script needs a very specific google sheet input but I figured I could at least share the setup that I used if it potentially can help someone with something that they’re working on. The way the script works, is it checks for start and end dates of a timeline and then checks for the job name and who is supposed to be working on it. It will then loop through the process to check multiple different jobs and verify if the date is 2 days away. If it is, it will send out an email to the person working on the job.
function emailReminder() { // Pull Work Order and Date Information var woList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YourSpreadsheet"); var lr = woList.getLastRow(); // Get Email Template Text from Cell // var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ValidationOptions").getRange(2,5).getValue(); var startValidation = false; var endValidation = false; const dateToday = new Date(new Date().setHours(0,0,0,0)); Logger.log("Todays Date:" + dateToday); // Loop to search Work Order and Date info for (var i = 2;i<=lr;i++) { var qcSendDateList = woList.getRange(i, 16).getValue(); var qcEndDateList = woList.getRange(i, 17).getValue(); const sendDate = new Date(qcSendDateList); const endDate = new Date(qcEndDateList); const date2DaysB4 = new Date(dateToday.getTime() + 48 * 60 * 60 * 1000); const date4DaysB4 = new Date(dateToday.getTime() + 96 * 60 * 60 * 1000); Logger.log("Send Date:" + sendDate); // Adjust notification Date to exclude Weekends if (dateToday.toString().includes("Fri")||dateToday.toString().includes("Thu")){ var daysB4 = date4DaysB4; } else if (dateToday.toString().includes("Sat")||dateToday.toString().includes("Sun")){ var daysB4 = 0 }else { var daysB4 = date2DaysB4; } // Output List of Work Orders that meet notification requirements if (sendDate.valueOf() == daysB4.valueOf()){ var currentWO = woList.getRange(i, 1).getValue(); var startingWOList = [currentWO]; var startValidation = true; Logger.log("Start Validation:" + startValidation.valueOf()); Logger.log("Start List:" + startingWOList); // Get List of Names and Emails var emailList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Validation"); var rangeValue = emailList.getRange("D2").getValue(); var emailRange = emailList.getRange(rangeValue); var emailLr = emailRange.getLastRow(); // Loop through Names and Emails for (var s = 2;s<=emailLr;s++) { var currentEmail = emailList.getRange(s, 2).getValue(); var currentName = emailList.getRange(s, 1).getValue(); // Apply Name and Work Orders to Email Template for (var n = 0;n<=0;n++){ var messageBody = "Hey " + currentName + ",<BR/><BR/>This is a reminder that <B>" + currentWO + "</B> has it's Start date in <B>2 days</B>. <BR/>Please notify those needed if the start will be late or attempt to deliver on time. <BR/><BR/>All the best,<BR/>Pepe Tracker Reminder"; var subjectLine = "Start Reminder: " + startingWOList; // Add Image into email var pepeUrl = "https://cdn3.emoji.gg/emojis/monkaS.png"; var pepeBlob = UrlFetchApp .fetch(pepeUrl) .getBlob() .setName("pepeBlob") messageBody = messageBody + "<BR/><BR/>" + "<img src='cid:pepe' style='width:64px; height:64px;'/>"; // Deliver Mail MailApp.sendEmail(currentEmail, subjectLine, "", { htmlBody: messageBody, cc: 'yourEmail@yourEmail.com', inlineImages: { pepe: pepeBlob, } }); //Logger.log(messageBody) } } } if (endDate.valueOf() == daysB4.valueOf()){ var currentWO = woList.getRange(i, 1).getValue(); var endingWOList = [currentWO]; var endValidation = true; Logger.log("End Validation:" + endValidation); Logger.log("End List:" + endingWOList); // Get List of Names and Emails var emailList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Validation"); var rangeValue = emailList.getRange("D2").getValue(); var emailRange = emailList.getRange(rangeValue); var emailLr = emailRange.getLastRow(); // Loop through Names and Emails for (var s = 2;s<=emailLr;s++) { var currentEmail = emailList.getRange(s, 2).getValue(); var currentName = emailList.getRange(s, 1).getValue(); // Apply Name and Work Orders to Email Template for (var n = 0;n<=0;n++){ var messageBody = "Hey " + currentName + ",<BR/><BR/>This is a reminder that <B>" + currentWO + "</B> has an End date in <B>2 days</B>! <BR/>Please notify those needed if the final delivery will be late and a timeline extension may be needed, or attempt to deliver on time. <BR/><BR/>All the best,<BR/>Pepe Tracker Reminder"; var subjectLine = "End Reminder: " + endingWOList; // Add Image into email var pepeUrl = "https://media.tenor.com/fKoX5NhmPSYAAAAM/this-is-fine.gif"; var pepeBlob = UrlFetchApp .fetch(pepeUrl) .getBlob() .setName("pepeBlob") messageBody = messageBody + "<BR/><BR/>" + "<img src='cid:pepe' style='width:64px; height:64px;'/>"; // Deliver Mail MailApp.sendEmail(currentEmail, subjectLine, "", { htmlBody: messageBody, cc: 'yourEmail@yourEmail.com', inlineImages: { pepe: pepeBlob, } }); //Logger.log(messageBody) } } } } }