Why Google Forms does not return user response on formSubmit?

為{幸葍}努か 提交于 2019-12-25 03:38:17

问题


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

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