Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing user entered data upon submit in google forms

I have a google-form that has the following two fields:

  • Email address: - A text box
  • Tool: - A radio button
    • Tool 1
    • Tool 2
    • Tool 3

The user would enter his email address and select a tool and click submit. I would like the following message to appear:

Thanks for responding. An email has been sent to you to at entered email address to download selected tool.

I have the following piece of code in the script editor

    function emailFormSubmission() {
        var form = FormApp.getActiveForm();//the current form
        var dest_id = form.getDestinationId(); //the destination spreadsheet where form responses are stored
        var ss = SpreadsheetApp.openById(dest_id);//open that spreadsheet
        var theFormSheet = ss.getSheets()[0]; //read the first sheet in that spreadsheet
        var row = theFormSheet.getLastRow(); //get the last row
        var emailid = theFormSheet.getRange(row,2,1,1).getValue();//get column 2 corresponding to the email id. column 1 is timestamp. so, skip that.
        var tool = theFormSheet.getRange(row,3,1,1).getValue();//get column 3 corresponding to the selected tool.
        form.setConfirmationMessage('Thanks for responding. An email has been sent to you '+ emailid + ' to download' + tool);
    }

I have also set the triggers to be Run -> emailFormSubmission, Events -> from Form , onFormSubmit.

What happens is: Suppose the first user ('A') enters his information and clicks submit. His entered information gets displayed correctly. When second user ('B') enters his information and clicks submit, A's information is displayed. When third user ('C') enters his information and clicks submit, then B's information is displayed. I found that the issue is with "getlastrow()" since the spreadsheet is updated after emailFormSubmission is processed.

Whats wrong with the above code? How do I fix this?

UPDATE

Based on @wchiquito's comments, I changed the code to following to make it work.

function emailFormSubmission(e) {
    var form = FormApp.getActiveForm();
    //Check this link on how to access form response:
    //https://developers.google.com/apps-script/understanding_events?hl=en

    var responses = e.response;//e is of type formresponse.
    var emailid = responses.getItemResponses()[0].getResponse();
    var tool = responses.getItemResponses()[1].getResponse();
    Logger.log(emailid);
    Logger.log(tool);
    form.setConfirmationMessage('Thanks for responding. An email has been sent to  '+ emailid + '   with instructions to download ' + tool +'. If you do not find our email in your inbox, please check your spam folder');  
    Logger.log(form.getConfirmationMessage());
}
like image 677
Bhushan Avatar asked Nov 18 '13 00:11

Bhushan


People also ask

Can we retrieve the data of Google Forms?

Google Forms does not have version history like Google Docs or Sheets. When you delete responses in Google Forms, it will be deleted permanently. The deleted form responses cannot be restored later.

How do you see who submitted what responses in Google Forms?

After you've sent your form, it's only a matter of time before people start responding. To access these responses, simply open your form and click the Responses tab. Here, you'll find the results from everyone who's responded so far, along with several options.

Where does Google Forms store the data that is collected?

Google forms is a free Google application that allows you to quickly create and distribute a form to gather information. Form responses are saved in a Google spreadsheet in Google drive.

How do I see results from Google Forms not owner?

You can take the form's ID from the original form, which looks like https://docs.google.com/forms/d/e/[form's ID]/viewform . Save this answer.


1 Answers

Remember that the event On form submit (Understanding Events) receives a parameter that has the following structure:

  • values​​
  • range
  • namedValues​​

and you can do something like:

function emailFormSubmission(e) {
    ...
    var row = e.range.getRow();
    ...
}

Try the following code to observe the structure of the parameter e:

function emailFormSubmission(e) {
    ...
    Logger.log(e);
    ...
}

UPDATE

First, excuse my confusion, I showed you the structure of a Spreadsheet form submit event when you really are using a Form submit event.

Sure enough, a Form submit event has the following structure:

  • response

Returning an object of type FormResponse.

Therefore, defining the event: On submit form (Form submit event), you can do something like the following:

function emailFormSubmission(e) {
  var itemResponses = e.response.getItemResponses();
  for (var i = 0, len = itemResponses.length; i < len; ++i) {
    Logger.log('Response #%s to the question "%s" was "%s"',
               (i + 1).toString(),
               itemResponses[i].getItem().getTitle(),
               itemResponses[i].getResponse());
  }
}

However, the confirmation message set according to the data sent as responses of the form, does not seem very clear, you can set the message, but will not display for the active response, if not for the next.

like image 181
wchiquito Avatar answered Nov 16 '22 02:11

wchiquito