-1

I am attempting to generate a script that opens up a google form and fills it out.

I have been able to read through the documentation and do the following: select a form using it's ID, get a list of questions in an "item" array, get the type of question and it's ID, get the item by id, return it by various types and do all sorts of "selecting" of the elements in the form.

I have not been able to 1. set a response to a specific item in the form, or 2. submit that form with a response. The formReponse object continues to be empty no matter what I do.

formResponse, despite being a class, without the for loop comes up as undefined unless I have that code there.

function myFunction() {
   var form = FormApp.openById('1h_8Gz5GKwLqUdw249rpMWu6-AlrDJgUig-rdMCC7TWU');
   //form.addTextItem();
   var items =  form.getItems();
   Logger.log(items);
  // Access the text item as a generic item.
  var item1 = items[0];
  Logger.log(item1);
  var item1type = item1.getType();
  Logger.log(item1type);
  var item1id = item1.getId();
  var getitem1 = item1.asTextItem();
  var getitem1t = form.getItemById(item1id);
  var response = "tewtw";
  getitem1t.createResponse(response);

  var formResponses = form.getResponses();

  for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
   for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    Logger.log('Response #%s to the question "%s" was "%s"',
         (i + 1).toString(),
        itemResponse.getItem().getTitle(),
        itemResponse.getResponse()); 
 }

formResponse.submit();
}

Thanks!

  • Are you collecting the form responses in a spreadsheet? If so, I suggest taking a different approach. Instead of automatically filling out and submitting a form, use the Spreadsheet Service to directly insert your data. – John Dec 13 '14 at 07:36

1 Answers1

-1

You can "simulate"" form answers using a code similar to the one Mogsdad shows here that would call the onFormSubmit triggered function. This "simulation" function should be in the same script project to be able to call the submission function.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131