从Google工作表中的列表自动填充Google表单中的多项选择

时间:2018-06-20 00:05:09

标签: google-apps-script google-sheets google-form

因此,我有一个带有名称列表的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);

}

3 个答案:

答案 0 :(得分:0)

在此处检查:https://developers.google.com/apps-script/reference/forms/multiple-choice-item,您将使用addMultipleChoiceItem()

答案 1 :(得分:0)

请尝试使用下面提到的代码。

{
sub foo { bar(); }
sub bar { 1; }
foo();
}

答案 2 :(得分:0)

此版本可使用电子表格中的值填充多项选择项,同时还根据多项选择项设置导航:

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

希望有帮助