Question

I have the triggers working, and I can add items to the form. But, if I try to change the choices of the item in any way I receive "Failed to edit the form" error at the .setChoices line.

var form = FormApp.openById("<your form id>");
var mci = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
  var item = mci[0].asMultipleChoiceItem();
  Logger.log(item.getChoices()[0].getValue());
  item.setChoices([
     item.createChoice('Cats'),
     item.createChoice('Dogs')
 ]);

I have checked, the multiple choice item exists, and I can log the correct current choice values. I have also tried

item.setChoiceValues(['Cats', 'Dogs']);

and

var choices = [];
choices.push(item.createChoice('Cats'));
choices.push(item.createChoice('Dogs'));
item.setChoices(choices);

with same results.

The goal: If a respondent enters an "other" choice in a multiple choice item, add that choice to the list of choices for the next respondent.

Was it helpful?

Solution

I am convinced that this can not be done using Google Forms.

It can still be done using Google App Scripts to create your own user interfaces and interact with your own data repositories.

OTHER TIPS

I was able to make the following code work, albeit having an independent rather than container-bound script. This also had to be run manually rather than via trigger.

A hack like this could be easily modified and triggered so that it runs every minute, adding choices from a known spreadsheet to a known form.

This code requires a previous form containing a multiple choice object, with its ID inserted into the variable id.

function AddNewChoiceBug() {
  //choice variable needs input before running.

  try{
    //get form via ID
    var id = '';  //insert form ID here
    var form = FormApp.openById(id);
    Logger.log( 'Opening form: %s', id );

    //get all multiple choice objects in given form
    var MultChoice = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);

    //input choice to all multiple choice objects
    var choice = 'new choice'; //insert new choice here
    Logger.log( 'New choice: %s', choice );

    //Iterate through Multiple Choice objects in opening form
    for( i=0; i< MultChoice.length; i++ ){

      Logger.log( 'Multiple Choice Question titled, %s', MultChoice[i].getTitle() );

      var knownChoices = MultChoice[i].asMultipleChoiceItem().getChoices();
      //Iterate through Multiple Choice Options for each object

      if( catchMultChoices( knownChoices, choice ) != null ){
        knownChoices.push( MultChoice[i].asMultipleChoiceItem().createChoice(choice) );

//The following creates an error:
        MultChoice[i].asMultipleChoiceItem().setChoices( knownChoices );
      }
    }
  }
  catch (e){
    Logger.log( e );
  }
}

function catchMultChoices(kc, c) {
  for( j=0; j< kc.length; j++ ){

    //catch choices already in MultChoice[i]
    if( kc[j].getValue() === c ){
      Logger.log( '  choice %s already a choice', c);
      return null;
    }
  }

  //choice not in MultChoice[i]!
  Logger.log( '  choice %s not in question!', c);
  return c;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top