0

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.

1

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 to formResponse.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 interest var itemtosearch = 158271976;) and to list only the responses for this item number. Those responses are identified by the IF method if (itemResponse.getItem().getId() == itemtosearch).

NB; these scripts have been drafted on the basis of the project being bound to the relevant Google Form.


function GetitemDetails() {
  
  var form = FormApp.getActiveForm();
  
  // get the items
  var items = form.getItems();
  Logger.log("the number of items = "+items.length)

  //Loop through the itejms and list them
  for (var i = 0;i<items.length;i++){
    var item = items[i];
    var itemID = item.getId();
    var itemtype = item.getType();
    var itemtitle = item.getTitle();
    var itemindex = item.getIndex();
    Logger.log("DEBUG: Item # "+item.getId()+", type = "+itemtype+", Index# "+itemindex+", Title: "+itemtitle)
  }
}

function getAllResponses(){

  var form = FormApp.getActiveForm();
  
  // get the responses
  var formResponses = form.getResponses();

  // loop through the form responses and list them
  for (var i = 0; i < formResponses.length; i++) {
    var formResponse = formResponses[i];
    // get the item responses and list them
    var itemResponses = formResponse.getItemResponses();
    for (var j = 0; j < itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      Logger.log("Item ID = "+itemResponse.getItem().getId()+", Title: "+itemResponse.getItem().getTitle()+", Response: "+itemResponse.getResponse())    
    }
  }

}

function getspecificResponses(){

  var form = FormApp.getActiveForm();
  
  // get the responses
  var formResponses = form.getResponses();
  
  // item number of interest
  var itemtosearch = 158271976;
  
  // loop through the form responses
  for (var i = 0; i < formResponses.length; i++) {
    var formResponse = formResponses[i];
  
    // get the item responses
    var itemResponses = formResponse.getItemResponses();
    for (var j = 0; j < itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      
      // test for the item number = searchterm
      if (itemResponse.getItem().getId() == itemtosearch){
        
        Logger.log("Item#: "+itemtosearch+" - Response: "+itemResponse.getResponse())     
      }
      else{
       //Logger.log("j="+j+", this response is NOT for item number "+itemNum)
    
      }
    
    }
  }

}
1
  • 1
    Hello Tedinoz, thanks for your extensive answer. It looks like what I need. I am going to test it in the coming hours and will let you know they outcome.
    – Renaat
    Nov 17 '20 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.