Issue making a response from a google form showing error “”TypeError: Cannot read property “values” from undefined. (line 2, file “Code”)“”

你。 提交于 2020-08-10 20:21:30

问题


In Google Forms, I am attempting to create an email response that includes a case number that is generated in the form responses (column titled code) but unfortunately, I have come across the error:

typeError: Cannot read property "values" from undefined. (line 2, file "Code")

I am a complete novice regarding this but I have set the trigger function and am given this error in the email that is sent in a reply.

Find a copy of the responses docs below:

https://docs.google.com/spreadsheets/d/1w8cUkErljf6JzQm9q5aFwa-m9jX4o5qrAg5K7ib9Keg/edit?usp=sharing

This is the code I'm using:

function myFunction(e){
var userName = e.namedValues.Name;
var userEmail = e.namedValues.Tag;
var date = e.namedValues.Timestamp;
var subject = e.namedValues.Code;
var message = "Thanks, " + userName + " for submiting your data remember to take note of your Case number "+ subject + "You'll need this to for you or the customer to find the case again" +date;
MailApp.sendEmail (userEmail, subject, message);}

I expect it to generate an email, use the code as a subject and include it the text in the body.


回答1:


Different Google Applications can have triggers with the same name, but different event objects.

To access the latest form response you need the trigger onFormsubmit(), which can be both a Google Sheets and Google Form trigger.

e.namedValues is a Google Sheets onFormsubmit() event object, while a Google Form onFormsubmit() event object would be e.response.

You have two options:

  1. Bind your Apps Script to the Spreadsheet where your form responses are being saved and access the responses with e.namedValues as intended
  2. Bind your Apps Script to the Form itself and access the responses with e.response as following:
var userName = e.response.getItemResponses()[0].getResponse();
var userEmail= e.response.getItemResponses()[1].getResponse();
...

Keep in mind that onFormsubmit() is an installable Trigger.



来源:https://stackoverflow.com/questions/57274957/issue-making-a-response-from-a-google-form-showing-error-typeerror-cannot-rea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!