Google script get current onSubmit response

Is there a way to find out the current answer of a Google form (programmatically)? In the onSubmit function.

Looked at my documentation , but I have to provide responseId. How to find the answer?

The cropped code from them about it:

// Open a form by ID and log the responses to each question. var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); var formResponses = form.getResponses(); for (var i = 0; i < formResponses.length; i++) { var formResponse = formResponses[i]; var itemResponses = formResponse.getItemResponses(); for (var j = 0; j < itemResponses.length; j++) { var itemResponse = itemResponses[j]; Logger.log('Response #%s to the question "%s" was "%s"', (i + 1).toString(), itemResponse.getItem().getTitle(), itemResponse.getResponse()); } } 

UPDATE:

 function onSubmit(e) { /* Get values entered by filling the form */ var itemResponses = e.response.getItemResponses(); var myWantedValue = itemResponses[COLUMN_NUMBER].getResponse(); } 

Note: By passing the e (vent) parameter as a parameter, you can access the values ​​by sending a live form, not a script editor!

+6
source share
4 answers

This code gives you values ​​for the last form response, is that what you are looking for?

 function myFunction() { var formResponses = FormApp.getActiveForm().getResponses(); Logger.log(formResponses.length); var formResponse = formResponses[formResponses.length-1]; var itemResponses = formResponse.getItemResponses(); for (var j = 0; j < itemResponses.length; j++) { var itemResponse = itemResponses[j]; Logger.log('Last response to the question "%s" was "%s"', itemResponse.getItem().getTitle(), itemResponse.getResponse()); } } 
+9
source

@Serge, I used your code to display the respondent's answers when he submits a form using form.setConfirmationMessage (), followed by form.getConfirmationMessage (). I used the onsubmit trigger form. However, it always returns the answers of the previous respondent. I also tried using e.response, but again I get the answers of the previous respondent. I also did Logger.log (form.getConfirmationMessage ()) to check and find that this displays the answers of the current respondent. However, the actual page the subject responds to is the previous respondent. Any clues?

Update: I noted that other people also posted this issue: see this https://productforums.google.com/forum/#!topic/docs/cygTFXtyy5I and this http://code.google.com/p/google -apps-script-issues / issues / detail? can = 2 & start = 0 & num = 100 & q = & colspec = Stars% 20Opened% 20ID% 20Type% 20Status% 20Summary% 20Component% 20Owner & groupby = & sort = & id = 3507

0
source

You are trying to read the exact answer that onSubmit () executed to trigger another process based on specific data, such as sending confirmation email.

If this is true:

Instead of using the form as a data source for further processing, try reading the Google Datasheet, which stores responses. It may seem that the problem is the same: reading either the data source (the form itself or the sheet storing the data) does not indicate the exact record that raised the onSubmit () event.

The difference is that in the worksheet you can create a "reply_sent" column to set the time when each form response was processed, for example, by sending an automatic response based on its contents.

Then, when onSubmit () is executed, it goes through all the responses and processes not only received last, or the one that called the onSubmit () function, but also any other answer that can be left without a link to all Reason (reply_sent field "empty). The last advantage of this approach is that your script registers how it behaves, since the date is written along with each response, as it calls obSubmit ().

0
source

All Articles