This is possible by creating Installable Trigger with event type On form submit.
Installable triggers let Apps Script run a function automatically when a certain event such as submitting form.
To create Installable Trigger:
- Open your Apps Script project.
- At the left, click Triggers alarm.
- At the bottom right, click Add Trigger.
- Select and configure the type of trigger you want to create.
- Click Save.
From there we fetch the data submitted by user by using Event Object.
When a trigger fires, Apps Script passes the function an event object
as an argument, typically called e. The event object contains
information about the context that caused the trigger to fire.
There are two ways to get the data from an event on form submit, e.values and e.namedValues.
- namedValues - An object containing the question names and values from
the form submission.
- values - Array with values in the same order as they appear in the spreadsheet.
Example:
Code:
function onFormSubmit(e) {
//get data from question 'Type'
var type = e.namedValues['Type'][0];
var email = '';
var subject = '';
var message = '';
if(type == 'Information'){
email = 'inputmarketingemailhere';
subject = 'For Marketing';
message = 'Message for Marketing';
}else if(type == 'Purchasing'){
email = 'inputpurchasingemailhere';
subject = 'For Purchasing';
message = 'Message for Purchasing';
}
MailApp.sendEmail(email, subject, message);
}
Submitted Answers:
Output:
Note: Run the script first to the Editor, This will prompt an authorization to gain access rights to the MailApp Service. See Authorization for Google Services
References:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…