Reputation: 1
Trying to create a google script that issues an email to different people depending on values enter using a google form. I.e. if you say you're in group AAA then an email is sent to person A... if you say you're in group BBB an email is sent to person B. It's complicated as some groups, say group CCCC don't want to be notified, so the script should just end.
Feeling a bit lost and confused on how to do this and concerned it will just loop round as if someone says they are in Group CCC I'm unsure how to just end the script.
I have sent this up and it is working fine with a simple email. It is the act of multiple options with different responses that have me out of my comfort zone.
function email2group(e) {
var name = e.values[1];
var surname = e.values[2];
var group = e.values[4];
var email = "end";
if (group == "AAA") {
email = "[email protected]";
} else if (team == "BBBB") {
email = "[email protected]";
}
var subject = + group+" Registration completed";
var message = "2019 Registration has been completed for " + name " " + surname ".\n"+"\n"+"If you think a mistake has been made as to what group they are in then please reply";
if (email = "end") {
MailApp.sendEmail (email,subject,message);
}
}
Upvotes: 0
Views: 188
Reputation: 1
Thanks for the assistance but I now get an error saying Missing; before statement. at the 'Mailapp' line.
Any thoughts - I may just leave it as for waht seems to be a simple error it is not very clear
Upvotes: 0
Reputation: 6877
... I'm unsure how to just end the script.
To end the execution of a function, you use a return
statement.
I would approach this by defining the mapping between groups and their email addresses using an object literal and then referencing the mapping in your function.
The benefit of this approach is that once you have the logic in place, you can leave the function alone and simply maintain the mapping definition over time.
Here's an example implementation:
var GROUP_TO_EMAIL = {
'AAAA': '[email protected]',
'BBBB': '[email protected]',
'CCCC': undefined,
}
function emailToGroup(e) {
var name = e.values[1];
var surname = e.values[2];
var group = e.values[4];
// Check to see if the group exists in the mapping.
if (!GROUP_TO_EMAIL.hasOwnProperty(group)) {
Logger.log('No email address to send to for group ' + group);
return;
}
// Check to see if a group is in the mapping, but does not want to be
// emailed.
if (GROUP_TO_EMAIL[group] == undefined) {
Logger.log('Not sending email for group ' + group);
return;
}
var subject = group + 'registration completed';
var message = '2019 registration has been compelted for ' +
name + " " + surname + "\n\nIf you think a mistake has " +
"been made as to what group they are in then please reply";
Logger.log('Sending email to ' + GROUP_TO_EMAIL[group] + ' for group ' + group);
MailApp.sendEmail(GROUP_TO_EMAIL[group]), subject, message);
}
And here's a test function to demonstrate the different scenarios:
function testEmailToGroup() {
emailToGroup({
values: ['', 'Jane', 'Doe', '', 'AAAA'],
});
emailToGroup({
values: ['', 'Jane', 'Doe', '', 'BBBB'],
});
emailToGroup({
values: ['', 'Jane', 'Doe', '', 'CCCC'],
});
emailToGroup({
values: ['', 'Jane', 'Doe', '', 'DDDD'],
});
}
And here's the logging output from executing the above test function (FYI, I had the sendEmail()
call commented out while testing):
[19-09-08 00:15:44:550 PDT] Sending email to [email protected] for group AAAA
[19-09-08 00:15:44:550 PDT] Sending email to [email protected] for group BBBB
[19-09-08 00:15:44:551 PDT] Not sending email for group CCCC
[19-09-08 00:15:44:551 PDT] No email address to send to for group DDDD
Upvotes: 1