How to use Google Apps Script to set the “values” and “go to section based on answer” for a Google Form dropdown?

限于喜欢 提交于 2021-01-29 07:57:33

问题


I using Google Sheets to store the "values" and "go to section based on answer" for a Google Form dropdown. How can I use the Google Apps Script to set both "values" and "go to section based on answer" for the dropdown?

I am able to set values from Google Sheet using the script, but I am not able to set the "go to section based on answer" for the drop-down using the sheet and script.

I already have the listItem of the dropdown in the script which I am using to set values. I tried using createChoice(value, navigationItem) and with it to set the "go to section" and then used setChoiceValues(values) but I everything gets set as "Choice" on the form.


回答1:


If you just want to create a dropdown item that takes you to a different section depending on the choice using Apps Script:

function createDropdownExample() {
  var form = FormApp.openById('your_form_id');
  var item = form.addMultipleChoiceItem();
  item.setTitle('Do you prefer cats or dogs?');  
  var dogsPage = form.addPageBreakItem().setTitle('Dogs');
  var catsPage = form.addPageBreakItem().setTitle('Cats');
  var dogsChoice = item.createChoice('Dogs', dogsPage);
  var catsChoice = item.createChoice('Cats', catsPage);
  item.setChoices([dogsChoice, catsChoice]);
}



回答2:


This a version of the above with values from a spreadsheet:

function GoToPage() {
var form = FormApp.openById('MY FORM');
var spreadsheet = SpreadsheetApp.openById("MY SPREADSHEET");
var sheet = spreadsheet.getSheetByName("MY SHEET");
var list = form.getItemById("MY MULTIPLE CHOICE ID").asListItem()  
var pagebreak01 = form.getItemById("PAGE BREAK ID 1").asPageBreakItem();
var pagebreak02 = form.getItemById("PAGE BREAK ID 2").asPageBreakItem();
var pagebreak03 = form.getItemById("PAGE BREAK ID 3").asPageBreakItem();
var choice1 = sheet.getRange("RANGE 1 FROM MY SHEET").getValues();
var choice2 = sheet.getRange("RANGE 2 FROM MY SHEET").getValues();
var choice3 = sheet.getRange("RANGE 3 FROM MY SHEET").getValues();  
list.setChoices([
  list.createChoice(choice1, pagebreak01),
  list.createChoice(choice2, pagebreak02),
  list.createChoice(choice3, pagebreak03)]); 
}



回答3:


I was using setChoiceValues(values) instead of using setChoices(choices). When I made this correction, I was able to set the "go to section" dynamically.



来源:https://stackoverflow.com/questions/58423352/how-to-use-google-apps-script-to-set-the-values-and-go-to-section-based-on-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!