问题
I have created a sample google form, and defined a function like below:
function onFormSubmit(e) {
Logger.log('Inside function');
Logger.log('Emailing data row ->');
var itemResponses = e.getItemResponses();
var email = itemResponses[0].getResponse();
Logger.log(itemResponses);
}
In Edit-> Current project's triggers
I have added the function to be called on form submit:
In My Executions
section I see the error below:
TypeError: Cannot call method "getItemResponses" of undefined. at onSubmit(Code:7)
Why is it like that? How should I capture form fields? Is there something wrong with my code, or some permissions?
回答1:
You need to call the getItemResponses() method on the response property of the e object, not directly on the e object.
Replace:
var itemResponses = e.getItemResponses();
With:
var itemResponses = e.response.getItemResponses();
回答2:
It seems that there was a bug in Google Forms
which is being happened only on newly created forms:
- Google forms script on form submit - event does not have FormResponse
2 workarounds are suggested:
1) Include a call to FormApp somewhere in the code, even in a comment. For example:
// FormApp.getActiveForm();
2) Add the scope to your Apps Script manifest file, as per these instructions:
https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
The scope to add is "https://www.googleapis.com/auth/forms"
Once you've completed either of those two options, re-authorize your script. You can do this be running any function in the script editor.
来源:https://stackoverflow.com/questions/53633060/why-google-forms-does-not-return-user-response-on-formsubmit