Basically, if there when the user edits column B, I want to send an email message to the address found in column D of the row that was edited. How can I do it with the Apps Script on Google Sheets?
CodePudding user response:
Use MailApp.sendEmail()
with an installable trigger, like this:
function sendEmailNotification(e) {
// version 1.0, written by --Hyde, 14 December 2022
// - see https://stackoverflow.com/q/74789336/13045193
if (!e) {
throw new Error('Please do not run the script in the script editor window but set up a trigger. See https://developers.google.com/apps-script/guides/triggers/installable.');
}
if (!e.range || e.range.columnStart !== 2) {
return;
}
const emailAddress = e.range.offset(0, 2).getDisplayValue();
if (!emailAddress.match(/\w @\w \.\w /i)) {
return;
}
const sheet = e.range.getSheet();
if (!sheet.getName().match(/^(Sheet1|Sheet2|Sheet3)$/i)) {
return;
}
const emailSubject = 'I have trouble formulating a good question.';
const emailContents = 'Cannot really tell what my real-world goal is either.';
MailApp.sendEmail(emailAddress, emailSubject, emailContents);
}