6
votes

I need to submit a form in a google script but get this error:

TypeError: Cannot call method "withItemResponse" of undefined

According to the link below, this is how it should be set up https://developers.google.com/apps-script/reference/forms/form#createResponse()

Code:

 //Submit form

  var formID = row[24]; 

  var form = FormApp.openById(formID);
  Logger.log(form.getId());  //returns correct ID
  form.createResponse() ;
  form.FormResponse.withItemResponse('Core Teachers', logSummary);  
  //form has only two questions, a short text and a paragraph text
    form.FormResponse.submit(); 
1
You didn't define logSummary in your codeJosan Iracheta

1 Answers

7
votes

form.createResponse() returns a FormResponse, which you need to assign to a variable.

also, withItemResponse() expects an object of type ItemResponse. I am not familiar with google forms, but maybe this gets you in the right direction:

var formID = row[24]; 
var form = FormApp.openById(formID);
var formResponse = form.createResponse();
// get items of form and loop through
var items = form.getItems();
for (index = 0; index < a.length; ++index) {
  var item = items[index]
  // Cast the generic item to the text-item class. You will likely have to adjust this part. You can find the item classes in the documentation. https://developers.google.com/apps-script/reference/forms/item-type.
  if (item.getType() == 'TEXT') {
    var textItem = item.asTextItem();
    var itemresponse = textItem.createResponse('Core Teachers');
    formResponse.withItemResponse(itemresponse);  
  }
}
formResponse.submit();

Generally, when the documentation of a method lists as parameter type something else than primitive types like String or Boolean you need to create or aquire an object of that type, like I did with createResponse. You need to familiarize yourself with these and other principles because the GoogleAppsScript documentation assumes knowledge of them.