This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formSubmitReply(e) { | |
/* ----- CREATE NEW OBJECT FROM SUBMISSION ----- */ | |
var NewSubmission = { | |
"Timestamp" : e.values[0], | |
"Favourite Colour" : e.values[1], | |
"Explanation" : e.values[2], | |
} | |
MailApp.sendEmail("gassnippets@gmail.com", "Your form was submitted!", | |
"Hey you!" + | |
"\n\n Someone submitted your form. Here's what they had to say:" + | |
"\n\n Favourite Colour: " + NewSubmission["Favourite Colour"] + | |
"\n Explanation: " + NewSubmission["Explanation"]) | |
} |
I have borrowed the test email code from before to show that the code is easier to interpret when it's more obvious which values are being referred to. Here, I have referenced properties of the NewSubmission object in the body of the notification email using square bracket notation which can be provide greater flexibility than dot notation.
Now imagine that our form is collecting a large amount of data? A quick way of creating the same object as we have above is to create an empty object and then add properties to it. We already have an array of values so we just need to create an array of property names and a loop which combines the two and adds them to our submission object. It may seem trivial in this case as we only have tree properties but perhaps when we look at larger forms taking more data, this will seem more worthwhile.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function OnSubmit(e){ | |
var NewSubmission = {}; | |
var Properties = ["Timestamp","Favourite Colour","Explanation"]; | |
var i = 0; | |
for(i = 0; i < Properties.length; i++){ | |
NewSubmission[Properties[i]] = e.values[i]; | |
} | |
MailApp.sendEmail("gassnippets@gmail.com", "Your form was submitted!", | |
"Hey you!" + | |
"\n\n Someone submitted your form. Here's what they had to say:" + | |
"\n\n Favourite Colour: " + NewSubmission["Favourite Colour"] + | |
"\n Explanation: " + NewSubmission["Explanation"]) | |
} |
No comments:
Post a Comment