Get the current response onSubmit
Asked Answered
Y

4

10

Is there any way to find out the current response of the Google Form (programmatically)?

In the onSubmit function.

Looked over their documentation but I have to provide the respondId.

How do I find the respondId?

A snipped code from them about this is:

// 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) as parameter the values can be accessed by submitting the live form, NOT by the script editor!

Ypsilanti answered 8/2, 2014 at 15:21 Comment(0)
A
18

This code gives you the values for the last form response, is that what you're 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());
  }
}
Amentia answered 8/2, 2014 at 16:45 Comment(1)
Doesn't this solution induce a race condition if someone else happens to submit the form at the same time?Keitel
F
1

You are trying to read the exact response that triggered the execution of onSubmit(), to run another process on particular data, such as sending a confirmation email.

If the above is correct:

Instead of using the form as a data source for your later processing, try to read the Google Sheet storing the responses. It may seem the problem is the same: reading either data source (the form itself, or the sheet storing the data), doesn't point you to the exact entry that has triggered the onSubmit() event.

The difference is that in a sheet you can create a "reply_sent" column, to timestamp the instant each form response has been postprocessed, for example, having sent an automated reply based on its contents.

Then, when onSubmit() is run, it goes through all responses and processes not only the last one received, or the one having triggered the onSubmit() funtion, but also any other response that may have been left unreplied for whatever reason ("reply_sent" field blank). A last benefit from this approach is that your script is logging how it is behaving, since a date is recorded along with each response as it triggers obSubmit().

Fikes answered 21/8, 2017 at 20:12 Comment(0)
C
-1

You cannot set up a custom confirmation message for each user.

California answered 1/1, 2016 at 12:36 Comment(0)
K
-1
function onSubmit(e) {
  /* Get values entered by filling the form */
  var itemResponses = e.response.getItemResponses();
  var myWantedValue = itemResponses[COLUMN_NUMBER].getResponse();
}
Kenric answered 22/6 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.