Set Choice from Spreadsheet List and "go to section based on answer" with Google Apps Script

88 Views Asked by At

I have a Google Form, which I will edit using Google Apps Script. One of the questions is multiple choice, with several answer choices that I provide in a list on a spreadsheet.

I'm having trouble to set "go to section based on answer" for each answer choice, to another page/section in the form. For reference, I use this script.

This is my modification script :

 function editForm() {

    var form          = FormApp.openById('MY FORM ID'); // Form to Edit
    var multipleChoiceQstn  = form.getItemById(1288364781).asMultipleChoiceItem();   // Item Multiple Choice, where Next Question/ Section based from the choice in this Item. 
    var spreadsheet   = SpreadsheetApp.openById("MY SPREADSHEET ID");
    var sheet         = spreadsheet.getSheetByName("MY SHEET NAME");
    var choices       = sheet.getRange(7,4,17,1).getValues(); // Range the Choice for Multiple Choice Answer. 
    var questionsId   = sheet.getRange(7,6,17,1).getValues() ; // Range the Question ID, where we want to "go to section based on answer".
       

    for (var k = 0; k < choices.length; k++) { 

    var choice        = choices[k][0];
    var questionId    = questionsId[k][0];
    var sectionQuest  = form.getItemById(questionId).asPageBreakItem(); 

      multipleChoiceQstn.setChoices([multipleChoiceQstn.createChoice(choice,  sectionQuest)])

      } 

      };

As a result of Logger, all answer choices and target questions appear, but the final result is only the last answer choice and last target question/ section on the form.

Any suggestions for improvement? Your help would be greatly appreciated.

1

There are 1 best solutions below

3
Saddles On BEST ANSWER

Google Sheets: Set Choices and Sections in Google Forms

You can achieve what you'd like by changing the logic of the code to create the choices first and then setting the choices afterwards.

function editForm() {
  var form = FormApp.openById("MY FORM ID");
  var multipleChoiceQstn = form.getItemById(1288364781).asMultipleChoiceItem();
  var spreadsheet = SpreadsheetApp.openById("MY SPREADSHEET ID");
  var sheet = spreadsheet.getSheetByName("MY SHEET NAME");
  var choices = sheet.getRange(7, 4, 17, 1).getValues();
  var questionsId = sheet.getRange(7, 6, 17, 1).getValues();
  var formChoices = [];

  for (var k = 0; k < choices.length; k++) {
    var choice = choices[k][0];
    var questionId = questionsId[k][0];
    var sectionQuest = form.getItemById(questionId).asPageBreakItem();
    formChoices.push(multipleChoiceQstn.createChoice(choice, sectionQuest))
  }
  multipleChoiceQstn.setChoices(formChoices);
};

I created an empty array called formChoices to be the placeholder for the choices, combined with the .push() method to add them, and set the choices afterwards for them to reflect in the form.

The sample sheet looks like this:

enter image description here

The sample form looks like this:

image

OUTPUT

enter image description here

REFERENCE