Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form values in the submit event handler?

Tags:

I'm trying to get started with a very simple Google Form containing just a couple of questions (a multiple choice with just 2 options and a short text). After creating it, I opened the script editor and typed in

function onSubmit(e) {   Logger.log("onSubmit(%s)", JSON.stringify(e)); } 

and configured onSubmit as the handler for "form submit" trigger using the "Current project's triggers" from the "Edit" menu.

Filling in the form and submitting it now does result in the handler being called, but I only see this in the log:

[17-04-15 18:56:23:584 CEST] onSubmit({"response":{},"source":{},"authMode":{},"triggerUid":1870249629}) 

i.e. the response field is empty. I've also tried using FormApp.getActiveForm().getResponses(), but it returns an array of several empty objects too (OTOH, FormApp.getActiveForm().getTitle() does return the title I gave the form).

I suspect I need to give the script some extra permissions to access the form data, but I have no idea how to do it, nor even if this is really the problem.

Does anybody know why am I not getting the form values and what should I do to get them? Thanks in advance!

like image 207
VZ. Avatar asked Apr 15 '17 17:04

VZ.


People also ask

How do I get form values on submit?

To get form values on submit, we can pass in an event handler function into the onSubmit prop to get the inputted form values. We use the useState hook so that we can use the phone state as the value of the value prop.

How do I get form data from an event?

Grabbing data from a FormData object If you want to snitch into a FormData object visit the example HTML form in a browser and place a breakpoint on console. log(event. formData) . Fill and submit the form with the browser's console opened and save the object as a global variable.

How do I get form data on submit in Reactjs?

To get input values on form submit in React: Store the values of the input fields in state variables. Set the onSubmit prop on the form element. Access the values of the input fields in your handleSubmit function.

How do I submit a form with an event?

Event: submit. There are two main ways to submit a form: The first – to click <input type="submit"> or <input type="image">. The second – press Enter on an input field. Both actions lead to submit event on the form. The handler can check the data, and if there are errors, show them and call event.preventDefault(),...

What is the use of submit event in JavaScript?

The submit event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript. The method form.submit () allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.

How to get form values on submit in jQuery?

To get form values on submit, we can pass in an event handler function into the onSubmit prop to get the inputted form values.

How to get form fields from onsubmit event object?

You also have access to the form fields when using onSubmit through the form itself passed with the event object, you can use event.target.elements to get an array of the form controls (inputs, button, selects, etc.)


1 Answers

There are 2 patterns for retrieving submitted values. For both patterns, the function for retrieving the values from form submission has to be installed as a trigger. The detail information of Installable Triggers is https://developers.google.com/apps-script/guides/triggers/installable.

1. Script is opened on spreadsheet.

In this case, by installing a trigger, you can retrieve the submitted values by your script. The detail information of Event Objects is https://developers.google.com/apps-script/guides/triggers/events#form-submit.

Script :

function onSubmit(e){   Logger.log("%s", JSON.stringify(e)); } 

Result :

{   "values": [     "date and time",     "test"   ],   "namedValues": {     "fromtestform": [       "test"     ],     "timeStamp": [       "date and time"     ]   },   "range": {     "columnStart": 1,     "rowStart": 2,     "rowEnd": 2,     "columnEnd": 2   },   "source": {},   "authMode": {},   "triggerUid": ##### } 

2. Script is opened on form.

In this case, the submitted values can be retrieved by following script. The detail information is https://developers.google.com/apps-script/reference/forms/form-response.

Script :

function onSubmit(e){   Logger.log("authMode=%s, source.getId()=%s", e.authMode, e.source.getId());   var items = e.response.getItemResponses();   for (i in items){     Logger.log("getItem().getTitle()=%s, getResponse()=%s", items[i].getItem().getTitle(), items[i].getResponse());   } } 

Result :

authMode=FULL, source.getId()=### form ID ### getItem().getTitle()=## item's title ##, getResponse()=test 

If I misunderstand your question, I'm sorry.

like image 192
Tanaike Avatar answered Oct 05 '22 22:10

Tanaike