Auto Populate Multiple Choice in google forms from a list on google sheet

。_饼干妹妹 提交于 2020-02-06 07:52:41

问题


So I have a google sheet with a list of names and I want a multiple choice box on a google form to populate with this list. I have already created a script to create a dropdown menu based on this list but can't get the multiple choice to work.

Here is the script that creates the dropdown menu.

function updateForm(){
// call your form and connect to the drop-down item
var form = FormApp.openById("Form ID");

var namesList = form.getItemById("Data-Item-ID").asListItem ();

// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Sheet Name");

// grab the values in the first column of the sheet - use 2 to skip header row 
var namesValues = names.getRange(2, 26, names.getMaxRows() - 1).getValues();

var name = [];
var jjj = -1;

// convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)    
if((namesValues[i][0] != "") & (namesValues[i][0] != "TOTALS") & 
(namesValues[i][0] != "Total Volunteers"))
{
  jjj +=1;
  name[jjj] = namesValues[i][0];
}

// populate the drop-down with the array data
namesList.setChoiceValues(name);

}

回答1:


Check here: https://developers.google.com/apps-script/reference/forms/multiple-choice-item you'll use the addMultipleChoiceItem()




回答2:


Please try with the below-mentioned code.

function updateForm(){
  // call your form and connect to the drop-down item
  var form = FormApp.openById("Your Form ID");

  var namesList = form.getItemById("The Drop-Down List ID").asListItem();

// identify the sheet where the data resides needed to populate the drop-down
  var ss = SpreadsheetApp.getActive();
  var names = ss.getSheetByName("Name of Sheet in Spreadsheet");

  // grab the values in the first column of the sheet - use 2 to skip header row 
  var namesValues = names.getRange(2, 1, names.getMaxRows() - 1).getValues();

  var studentNames = [];

  // convert the array ignoring empty cells
  for(var i = 0; i < namesValues.length; i++)    
    if(namesValues[i][0] != "")
      studentNames[i] = namesValues[i][0];

  // populate the drop-down with the array data
  namesList.setChoiceValues(studentNames);

}


来源:https://stackoverflow.com/questions/50938463/auto-populate-multiple-choice-in-google-forms-from-a-list-on-google-sheet

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