How to assign Go_To_Page in Form Service on Multip

2019-08-07 10:07发布

问题:

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?

回答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); 
  }  
}