I have created a Google form and I am trying to retrieve a response after the user submits the form. As some of my questions are grids and may be left blank by the user, the indexes of the responses may change since empty responses to grids are ommitted in the repsonse array. Therefore I cannot retrieve the response via the index of the array.
Instead I am trying to use 'form.getItemById(710215182).getResponse' to retrieve the response. But, it keeps returning 'undefined'. I have checked, double checked and triple checked the ID I am using and it is absolutely correct.
Can somebody tell me what I am doing wrong? And if this is a stupid question, please note that I am a newbie to using Google script and forgive my ignorance. Your help is still very much appreciated.
For the sake of the test I have created a new form with only 1 question (Type TEXT) , called Comment1. Even if I use that one I do not get the answer. Here is my code:
function GetCommentbyID() {
var form = FormApp.getActiveForm();
var Comment1 = form.getItemById(710215182).getResponse
Logger.log ('Title = ' + form.getTitle())
Logger.log ('First comment = ' + Comment1)
}
This code returns (in the logger)
Title = TestForm
First comment = undefined
To be sure my form is not the problem I have also used the getResponse via the array.
function GetComment() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var latestFR = formResponses[form.getResponses().length-1];
var itemResponses = latestFR.getItemResponses()
var Comment1 = itemResponses[0].getResponse()
Logger.log ('Title = ' + form.getTitle())
Logger.log ('First comment = ' + Comment1)
}
This code returns (in the logger)
Title = TestForm
First comment = This is my comment
as expected.
To be sure the problem is not in the ID I am using I have used following code
function CollectID() {
var Vform = FormApp.getActiveForm();
var items = Vform.getItems();
for (var i = 0; i < items.length; i++)
Logger.log(items[i].getId() + ' - ' + items[i].getType() + ' - ' + items[i].getTitle() );
}
And this code returns (in the logger)
710215182 – TEXT – Comment1
Can anybody tell me what it am doing (stupidely) wrong? I have been reading every help file I can find scrolling through Google, but no luck.
Thanks in advance for your help, much appreciated.
Best Answer
You are trying to identify data submitted for a given Google Form question. Your debugging code is not displaying the information that you expect.
You are confusing the pathway to list items with the pathway to list item responses. Though
form.getItemById(id)
returns a given item, it is a path to getting more information about item properties. It is NOT a path to item responses.In order to analyse responses, you need to start with
form.getResponses()
, and drill down toformResponse.getItemResponses();
.This answer consists of three scripts:
GetitemDetails()
- The purpose of this script is to list ALL items so that you can unequivovcally identify the item number that of interest to you.getAllResponses()
- The purpose of this script is to list ALL responses for ALL items. It is NOT essential to run this script; though it might be useful for debugging. The relevance of this script is that it provides the information required to understand how to retrieve form and item responses; it is the parent of the next script.getspecificResponses()
- The purpose of this script is to enable you to enter a specific item number as a variable (// item number of interestvar itemtosearch = 158271976;
) and to list only the responses for this item number. Those responses are identified by the IF methodif (itemResponse.getItem().getId() == itemtosearch)
.NB; these scripts have been drafted on the basis of the project being bound to the relevant Google Form.