2

Just starting to use the Forms Service in Google Apps Script. Need to direct the form to take the user to a specific page depending on the answer that is given. Here's my current code

form.addMultipleChoiceItem()
    .setTitle('What would you like to do?')
    .setRequired(true)
    .setChoiceValues(['Request a new reservation.','Change the date or number of tickets for an existing reservation.']) 

Now, I've found this section in the documentation: Enum PageNavicationType

But they don't example the use of Go_To_Page. Also the creation of the ChoiceValues is wonky to me.

Anyone out there worked this out?

MartinK
  • 412
  • 1
  • 4
  • 20

1 Answers1

-1

Instead of .setChoiceValues you want to use .setChoices([arrayOfChoices]), and use the .createChoice(value, page) to create the choice.

Edit: Updated code to fix errors

function createAMCQuestion(){
  var af = FormApp.getActiveForm();
  var pRed = af.getItemById("1300443051").asPageBreakItem();  //use findPageIds to get the page id of a pre-created page
  var pGreen = af.getItemById("902629766").asPageBreakItem(); 

  var item = af.addMultipleChoiceItem().setTitle('Pic a color?');  // creates the Multiple Choice Question
  af.moveItem(item.getIndex(), 1); // Makes the question show up as the second question in the form (starts at 0)

  //create choices as an array
  var choices = [];
  choices.push(item.createChoice('Red', pRed));
  choices.push(item.createChoice('Green', pGreen));

  // assignes the choices for the question
  item.setChoices(choices);
}

function findPageIds(){
  var af = FormApp.getActiveForm();
  var pages = af.getItems(FormApp.ItemType.PAGE_BREAK);

  for (i in pages){
   var pName = pages[i].getTitle();
   var pId = pages[i].getId(); 
    Logger.log(pName+":"+pId); 
  }  
}
Bjorn Behrendt
  • 1,204
  • 17
  • 35
  • Thanks, Bjorn, but I am running into an issue. Error "Cannot convert Array to Choice" with a reference to: item.setChoices(choices);. Also what is the getItemById? Do the forms automatically assign pages id's as default? – MartinK Feb 15 '15 at 04:20
  • Sorry that is what I get from typing code from memory and not tested in production. I updated the code. – Bjorn Behrendt Feb 17 '15 at 00:27