1

I made a Google form and a script that sends a mail with the content of the form.

There are 3 fields: A, B, C that are not mandatory. Example: If we enter 1, 5, and 8 then the script is writing:

A=1, B=5, C=8

The problem is that if B is not given a value (1 for A and 3 for C), then I get the output:

A=1, B=3, C=

There is a shift of the values! What can I do ?

function sendResponses() {
var form = FormApp.getActiveForm();
var responses = form.getResponses();
var lastrow = responses.length - 1;
var itemResponses = responses[lastrow].getItemResponses();
var Total = "A=" + itemResponses[0].getResponse() + ", B=" + itemResponses[1].getResponse() + ", C=" + itemResponses[2].getResponse(),
}

Thank you, Frédéric

cyrnian
  • 11
  • 4
  • There is a comma instead of a semi-colon at the end of the last line. `var Total = "A=" + itemResponses[0].getResponse() + ", B=" + itemResponses[1].getResponse() + ", C=" + itemResponses[2].getResponse(),` Was that a mistake entering the question, or how your code actually is? – Alan Wells Sep 10 '14 at 20:23
  • Thank you Sandy but I found another solution by doing a loop and getting the proper title for each response using itemResponse.getItem().getTitle(). For info, the code was used to send the responses by email in HTML rather than the logger.log with its timestamps. – cyrnian Sep 11 '14 at 08:45
  • So, it's not a Google Form? It's a form in HTML using HTML Service? Can you make corrections to your answer if need be, and post the correct answer? – Alan Wells Sep 11 '14 at 14:59

2 Answers2

0

Get the responses using named values instead of indexes... there won't be any possible confusion.

See documentation here.

You can retrieve the item names with the FormApp method getTitle()

Example :

e.namedValues   {'First Name': ['Jane'], 'Timestamp': ['6/7/2015 20:54:13'], 'Last Name': ['Doe']}  An object containing the question names and values from the form submission

See this other post for a full example.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
0

When I use this code, it's giving me the results that you want:

function sendResponses(e) {
  var thisResponse = e.response;
  Logger.log('thisResponse: ' + thisResponse);

  var itemResponses = thisResponse.getItemResponses();
  Logger.log('itemResponses: ' + itemResponses);

  var Total = "A=" + itemResponses[0].getResponse() + ", B=" + itemResponses[1].getResponse() + ", C=" + itemResponses[2].getResponse();
  Logger.log('Total: ' + Total);
}

Note that you don't need to get all the form responses. Your code retrieves all responses, past and present. You can get just the current response. Note the variable e as an argument in the sendResponses function:

function sendResponses(e) {

I created a form with three text fields, and did not put anything in the second field. The Log prints this:

[14-09-10 16:25:44:265 EDT] thisResponse: FormResponse

[14-09-10 16:25:44:267 EDT] itemResponses: ItemResponse,ItemResponse,ItemResponse

[14-09-10 16:25:44:268 EDT] Total: A=1, B=, C=C

The function is triggered when the form is submitted:

Form Trigger

Alan Wells
  • 30,746
  • 15
  • 104
  • 152