Reputation: 1
I am using a Google Sheet to send an automated pre-filled email whenever someone fills out the required fields in the Google Sheet. How can I make the following script send to different emails based on the location the user enters? For example if user enters London send email to [email protected] or if user enters Paris send email to [email protected]. Also, do I need to add a trigger for Google Sheets to know when to initiate this script?
function fireEmail(e){
var userName = e.values[1];
var timeStamp = e.values[0];
var medalType1 = e.values[2];
var medalType2 = e.values[3];
var medalColor = e.values[4];
var medalRecipient = e.values[5];
var dateNeeded = e.values[6];
var medalLocation = e.values[7];
var emailBodyHTML = [];
emailBodyHTML += '<b>You have received a medal request from ' + userName + '.</b>';
emailBodyHTML += '<p>Onboarding Ops Medal Type, If Applicable: ' + medalType1 + '</p>';
emailBodyHTML += '<p>Service Ops Medal Type, If Applicable: ' + medalType2 + '</p>';
emailBodyHTML += '<p>Medal Color: ' + medalColor + '</p>';
emailBodyHTML += '<p>Medal Recipient: ' + medalRecipient + '</p>';
emailBodyHTML += '<p>Medal Needed by: ' + dateNeeded + '</p>';
emailBodyHTML += '<p>Please visit <a href="place.com">the medaling document</a> to manage this request.</p>';
var emailSubject = 'New Medal Request for ' + medalLocation + '!';
MailApp.sendEmail({
to: "[email protected]",
name: "Medaling Request",
subject: emailSubject,
htmlBody: emailBodyHTML
});
}
Upvotes: 0
Views: 229
Reputation: 27292
To check the location you can use an if-statement (after your htlm body)
var mailTo;
if (medalLocation == 'London') {
mailTo = '[email protected]';
} else if (medalLocation == 'Paris' {
mailTo = '[email protected]';
}
If you only have two locations, you can also use a conditional (ternary) operator:
var mailTo = (medalLocation == 'London') ? '[email protected]' : '[email protected]'
If, on the other hand you have more locations to check, I would advice the use of a switch statement
var mailTo;
switch(medalLocation) {
case 'London':
mailTo = '[email protected]';
break;
case 'Paris':
mailTo = '[email protected]';
break;
case 'Brussels':
mailTo = '[email protected]';
break;
default:
mailTo = '[email protected]';
}
Then, after your 'mailTo' is determined, change the mail part to
MailApp.sendEmail({
to: mailTo,
name: "Medaling Request",
subject: emailSubject
htmlBody: emailBodyHTML
});
The script should have a on Form submit trigger. Hope this helps ?
Upvotes: 1