与电子表格相关的谷歌表单上的多个下拉问题

问题描述 投票:0回答:1

我实在是太菜鸟了,所以请原谅。 我正在尝试创建一个谷歌表单,引用谷歌表上的多个下拉选项。 我使用下面的脚本将下拉菜单添加到第一个问题。 我为第二个问题创建了第二个脚本,更改列位置,并添加触发器以在更改/编辑工作表时进行更新。 执行更改时,第二个下拉列表会更新,但第一个下拉列表不会更新。 我希望两个下拉源位于同一张纸上,但我认为这是导致问题的原因,因此我将它们制成单独的纸(并在代码中),但没有任何改进。 如果您能指出我完成此任务的方法,谢谢。

function updateForm(){
  // call your form and connect to the drop-down item 
  // This script if for the special donaations block. 
  var form = FormApp.openById("1Y6gm6Di2E3bmH4mlABu6ub5OnCODVHLRsavpSn-V6IA");
  
  //To find this with the form open right click and hit inspect
  //search for data-item-id untill the dropdown box is highlighted
  var namesList = form.getItemById("640761496").asListItem();

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

  // 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);
google-sheets dropdown google-forms
1个回答
0
投票

目标是

to create a google-form that references more than one dropdown selection on a googlesheet
。如果我的理解是正确的,您想要做的是将电子表格的值设置为问题选项。

之所以有

changes
the second dropdown
the first will not
,是因为脚本的逻辑是针对单个问题。

这是脚本的修改版本,应该可以实现您想要的效果:

function updateForm() {
  var ss = SpreadsheetApp.getActive();
  var names = ss.getSheetByName("Key");
  var namesValues = names.getRange(2, 1, names.getMaxRows() - 1).getValues().filter(vl => vl != "");
  var form = FormApp.openById("1Y6gm6Di2E3bmH4mlABu6ub5OnCODVHLRsavpSn-V6IA");
  var id = form.getItems().map(itm => itm.getId());
  id.forEach(itms => {
    var namesList = form.getItemById(itms).asListItem();
    namesList.setChoiceValues(namesValues);
  });
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.