I have some very simple code that is triggered by a google form (see ) that asks for 2 email addresses. The code works fine when the email address is hardcoded (see mented email below) but when I try and obtain the email using e.values (when the form is submitted) is get the following error
Execution failed: TypeError: Cannot read property "1" from undefined. (line 3, file "Code").
I've searched this forum but can't find a solution to this problem. Can anyone please help.
function onFormSubmit (e) {
// var email_address_ = "[email protected]";
var email_address_ = e.values[1];
// Send the email
var subject_ = "Andy's Report";
var body_ = "Here is Andy's report...";
MailApp.sendEmail(email_address_, subject_, body_, {htmlBody: body_});
}
I have some very simple code that is triggered by a google form (see http://goo.gl/lFHi3j) that asks for 2 email addresses. The code works fine when the email address is hardcoded (see mented email below) but when I try and obtain the email using e.values (when the form is submitted) is get the following error
Execution failed: TypeError: Cannot read property "1" from undefined. (line 3, file "Code").
I've searched this forum but can't find a solution to this problem. Can anyone please help.
function onFormSubmit (e) {
// var email_address_ = "[email protected]";
var email_address_ = e.values[1];
// Send the email
var subject_ = "Andy's Report";
var body_ = "Here is Andy's report...";
MailApp.sendEmail(email_address_, subject_, body_, {htmlBody: body_});
}
Share
Improve this question
edited Jun 10, 2014 at 21:58
user3726045
asked Jun 10, 2014 at 14:46
user3726045user3726045
1161 gold badge1 silver badge6 bronze badges
4
-
1
What part of the error don't you understand? Why do you expect
e.values
to exist? – SLaks Commented Jun 10, 2014 at 14:48 -
im assuming (e) is an event... that means that e.target will contain the form... but it's still not just gonna give you 'values'... If you are referencing specific fields then use
var email_address_ = document.getElementById('unique_id_for_email_address_field').value;
– xxcezz Commented Jun 10, 2014 at 14:56 - 1 Thanks for your ments - I thought that e.values would exist because the email addresses I'm looking for are supplied by the user in the form. I thought that when "submit" is pressed, this script would be triggered and e.values would contain all the inputs provided by the user in the form. Isn't this how it worked with the previous version of Google forms? – user3726045 Commented Jun 10, 2014 at 21:33
-
Try putting a
console.dir(e)
as the first statement inonFormSubmit
to find out what sort of form it takes. – CanSpice Commented Jun 10, 2014 at 21:35
2 Answers
Reset to default 4There is no e.values[1]
.
That is the answer to the question as to why you are getting that error.
Do a console.log(e)
and familiarize yourself with what e
consists of by looking at the log results in a browser debugger.
From there you can determine for yourself if e.values even exists as an array. Then go from there.
Thanks for the responses which all helped me find a solution to the problems I was having. Here's what I've discovered as I've worked on a solution:
- The Google App Script must be created (as a blank script) from responses spreadsheet and NOT out of the form itself!
- In Google Forms, the developer has the option of unlinking their responses spreadsheet and sending responses to new results spreadsheet. If this option is selected then a new script needs to be created from the new results spreadsheet and the code must be copied/moved into the new script. Of course this means that a new trigger must be created and the script needs to be authorized to run.
- Once a user submits a form, they can be given the option to edit/correct their submission. Unless EVERY field is edited, the script thinks the unedited fields are blank and writes "undefined" into the resulting Google Doc. I've therefore disabled the "Edit" option in my form to get around this.
- If the user skips over irrelevant fields (leaves them blank) then the e.values numbering order is thrown out. I have therefore made every field pulsory with the ment that the user enter 'n/a' if a field doesn't apply.
Having worked through these issues my form now works perfectly (albeit a little clumsily with all questions being pulsory). I would be very grateful if anyone has any suggestions on how to get around the Editing and and Blank Field problems.
(Thanks to TJ Houston for his original script)