簡體   English   中英

從谷歌表格上的列表中自動填充谷歌表單中的多項選擇

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

所以我有一個帶有姓名列表的 google 表格,我想要一個 google 表單上的多選框來填充這個列表。 我已經創建了一個腳本來根據此列表創建下拉菜單,但無法使多項選擇工作。

這是創建下拉菜單的腳本。

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);

}

在此處查看: https : //developers.google.com/apps-script/reference/forms/multiple-choice-item您將使用addMultipleChoiceItem()

請嘗試使用下面提到的代碼。

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);

}

這是一個使用電子表格中的值填充多項選擇項的版本,同時還根據多項選擇項設置導航:

function GoToPage() {
var form = FormApp.openById('MY FORM');  //Identifies the form from where the multiplechoice item resides
var spreadsheet = SpreadsheetApp.openById("MY SPREADSHEET"); //Identifies the spreadsheet from where to find the sheet with values for the multiplechoice item
var sheet = spreadsheet.getSheetByName("MY SHEET");  //Identifies the sheet with values for the multiplechoice item
var multiplechoice = form.getItemById("MY MULTIPLE CHOICE ID").asMultipleChoiceItem() //identifies the multiplechoice item by its Id  
var pagebreak01 = form.getItemById("PAGE BREAK ID 1").asPageBreakItem(); //Identifies the section where to navigate to upon chosing the first choice from the multiplechoice item
var pagebreak02 = form.getItemById("PAGE BREAK ID 2").asPageBreakItem();//Identifies the section where to navigate to upon chosing the second choice from the multiplechoice item
var pagebreak03 = form.getItemById("PAGE BREAK ID 3").asPageBreakItem(); //Identifies the section where to navigate to upon chosing the third choice from the multiplechoice item, and so on
var choice1 = sheet.getRange("RANGE 1 FROM MY SHEET").getValues(); //Identifies the value for the first choice of the multiplechoice item
var choice2 = sheet.getRange("RANGE 2 FROM MY SHEET").getValues(); //Identifies the value for the second choice of the multiplechoice item
var choice3 = sheet.getRange("RANGE 3 FROM MY SHEET").getValues();  //Identifies the value for the third choice of the multiplechoice item, and so on
multiplechoice.setChoices([ //sets the values, individually, and the pagebreaks for each value
  multiplechoice.createChoice(choice1, pagebreak01),
  multiplechoice.createChoice(choice2, pagebreak02),
  multiplechoice.createChoice(choice3, pagebreak03)]); 
}

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM