Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting a form through google scripts

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(); 
like image 799
TeachScience Avatar asked May 28 '17 05:05

TeachScience


People also ask

Can I automate a Google Form submission?

Create an Interactive Workflow with Google FormsUsing automation, Google Forms let you automatically connect your form data with a Google spreadsheet. For example, if you conduct employee surveys, set up a workflow so that the form data automatically populates an existing Google spreadsheet.

Can I script a Google Form?

This service allows scripts to create, access, and modify Google Forms. // Create a new form, then add a checkbox question, a multiple choice question, // a page break, then a date question and a grid of questions.


1 Answers

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.

like image 145
Ozan Avatar answered Sep 28 '22 08:09

Ozan