Accessing user entered data upon submit in google forms

笑着哭i 提交于 2019-11-29 02:18:26

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.

My first guess is these two lines right here:

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.

When you call getLastRow() on a sheet - you're getting the last row. Sure, but considering the order of events and how these values are processed, you need a +1, to get the most recent submission. Currently you're one row behind when your code runs to update the Form confirmation message.

So just change your code to the following:

var emailid = theFormSheet.getRange(row+1,2,1,1).getValue();
var tool = theFormSheet.getRange(row+1,3,1,1).getValue();

Spreadsheets are the most confusing of Google services, in my opinion. When you get values in the Spreadsheet, they're returned as an [] or [][] depending on what your Range is when you call getValues(). But getRange() on a sheet starts at index 1 (to make it easier to read in code I suppose). Often times I find that I have an off-by-one error because of the way data is passed around. Just keep that in mind as you work with Spreadsheets :)

Short answer: want you want can't be done with Google forms.

Explanation: form.setConfirmationMessage() sets the confirmation message for the form as stored on the server, not for the current active form. Same applies for example for form.setTitle(). The active form will not be modified. One would expect different behaviour for the confirmation message, but alas, this is not the case.

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