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 = TestFormFirst 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 = TestFormFirst 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.