Β
Please copy & the below code to the Google sheet for the Form response. You might replace the spreadsheet Id that youβre using for the Form response.
Β
const WELCOME_EMAIL_SUBJECT = 'Automated Notification: Reminder App';
// Convert text message to html using https://www.textfixer.com/html/convert-text-html.php
const WELCOME_EMAIL_BODY = '<p>Hi there,</p><p>This is an automated email to inform you about the newly created reminder submission. Please note that this message is generated automatically.</p><p>Thank you,<br>Makeinfo Team</p>'
const SpreadsheetId = '1o-4xdO1daQTmgRV6zdqUY9mb-S3EjC6Qf2-50qQK1-0' // Get the spreadsheet id from response spreadsheet
/**
* Sends a customized email for every response on a form.
*
* @param {Object} e - Form submit event
*/
function onFormSubmit(e) {
var responses = e.namedValues;
// If the question title is a label, it can be accessed as an object field.
// If it has spaces or other characters, it can be accessed as a dictionary.
/**
NOTE: One common issue people are facing is an error that says 'TypeError: Cannont read properties of undefined'
That usually means that your heading cell in the Google Sheet is something else than exactly 'Email address'.
The code is Case-Sesnsitive so this HAS TO BE exactly the same on line 25 and your Google Sheet.
*/
var email = responses['Email Address'][0].trim();
MailApp.sendEmail({
to: email,
subject: WELCOME_EMAIL_SUBJECT,
htmlBody: WELCOME_EMAIL_BODY,
});
}
function expiryReminderCron() {
const spreadsheet = SpreadsheetApp.openById(SpreadsheetId),
sheet = spreadsheet.getActiveSheet(),
lastRow = sheet.getLastRow(),
lastCol = sheet.getLastColumn(),
data = sheet.getRange(2, 2, lastRow - 1, lastCol).getValues();
for (let index = 0; index < lastRow - 1; index++) {
const expDateSheetVal = data[index][2];
const docName = data[index][1];
const email = data[index][0];
const expirationDate = Utilities.formatDate(expDateSheetVal, 'GMT', 'MM/dd/yyyy');
const selectedDaysSheetVal = String(data[index][4]);
let selectedDays = (selectedDaysSheetVal.indexOf(",") !== -1) ? selectedDaysSheetVal.split(", ") : selectedDaysSheetVal.split(" ");
selectedDays.map((selectedDay) => {
let tempDate = new Date((Date.now() + (selectedDay * 1000 * 60 * 60 * 24)));
tempDate = Utilities.formatDate(tempDate, 'GMT', 'MM/dd/yyyy');
if ((expirationDate === tempDate) && MailApp.getRemainingDailyQuota() > 0) {
// MailApp.sendEmail(email, WELCOME_EMAIL_SUBJECT, WELCOME_EMAIL_BODY);
const EMAIL_SUBJECT = `Reminder: Document Expiry in ${selectedDay} Days`;
const EMAIL_BODY = `<p>Hi there,</p>
<p>This is an automated reminder from our system to inform you that the document ${docName} is set to expire in ${selectedDay} days.</p>
<p>Please take the necessary action to renew or update the document if required. If you have already updated your response, kindly update the submitted form response as well.</p>
<p>Thank you for your attention to this matter.</p>
<p>Best regards,<br>
Makeinfo Team</p>`
MailApp.sendEmail({
to: email,
subject: EMAIL_SUBJECT,
htmlBody: EMAIL_BODY,
});
}
})
}
}