问题
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 SheetsonFormsubmit()
event object, while a Google FormonFormsubmit()
event object would bee.response
.
You have two options:
- Bind your Apps Script to the Spreadsheet where your form responses are being saved and access the responses with
e.namedValues
as intended - 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