Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property "1" from undefined

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 commented 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_}); 
    }
like image 818
user3726045 Avatar asked Jun 10 '14 14:06

user3726045


2 Answers

There 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.

like image 134
dthree Avatar answered Sep 24 '22 03:09

dthree


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:

  1. The Google App Script must be created (as a blank script) from responses spreadsheet and NOT out of the form itself!
  2. 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.
  3. 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.
  4. 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 compulsory with the comment 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 compulsory). 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)

like image 41
user3726045 Avatar answered Sep 21 '22 03:09

user3726045