从电子表格列自动填充表单选项
Auto populate form options from spreadsheet column
我创建了一个带有下拉问题的 google 表单,我想使用附加传播中的特定列自动填充下拉列表sheet。
我在网上搜索了解决方案,找到了两种有效的方法:
一个名为 "Form Ranger" 的插件和一个要添加到 sheet 脚本编辑器的代码
我首先尝试了附加组件,以便更轻松、更快速地安装,它运行良好,但有一个主要问题是更新表单中的列表需要很长时间,这是不可接受的,所以我决定将以下代码添加到sheet 脚本编辑器:
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);
}
上面的代码工作得更好,因为它在提交时立即更新表单,只有两个问题:
- 不是完全删除选项,而是用 "NOT_FOUND"
替换选项
- 它根本不删除最后一个选项;想象一下我有 7 个选项,所以它只用六个选项就可以完美地工作,但根本不会删除整个列表选项
所以如果有人能帮助我以更好的方式完成工作,我将非常感激
感谢评论区的解释,试试下面的代码:
function onEdit() {
var ws = SpreadsheetApp.getActiveSpreadsheet();
var ss = ws.getSheetByName("sheet1");
var name = ss.getRange("A6:A8").getValues();
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(name)
.build();
ws.getRange("B3").setDataValidation(rule);
}
此代码将在您的 sheet 更新时执行,它将从 A6:A8 读取 "options"(在我的示例中)并使用它们填充 B3 中的下拉菜单。 B3 中的菜单将删除已删除的选项并创建新的选项。您可以将它与您的代码结合起来,从您拥有它们的地方删除选项。
Here 您可以找到一些关于我使用的方法的文档以及有关处理数据验证的更多信息
我创建了一个带有下拉问题的 google 表单,我想使用附加传播中的特定列自动填充下拉列表sheet。
我在网上搜索了解决方案,找到了两种有效的方法: 一个名为 "Form Ranger" 的插件和一个要添加到 sheet 脚本编辑器的代码
我首先尝试了附加组件,以便更轻松、更快速地安装,它运行良好,但有一个主要问题是更新表单中的列表需要很长时间,这是不可接受的,所以我决定将以下代码添加到sheet 脚本编辑器:
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);
}
上面的代码工作得更好,因为它在提交时立即更新表单,只有两个问题:
- 不是完全删除选项,而是用 "NOT_FOUND" 替换选项
- 它根本不删除最后一个选项;想象一下我有 7 个选项,所以它只用六个选项就可以完美地工作,但根本不会删除整个列表选项
所以如果有人能帮助我以更好的方式完成工作,我将非常感激
感谢评论区的解释,试试下面的代码:
function onEdit() {
var ws = SpreadsheetApp.getActiveSpreadsheet();
var ss = ws.getSheetByName("sheet1");
var name = ss.getRange("A6:A8").getValues();
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(name)
.build();
ws.getRange("B3").setDataValidation(rule);
}
此代码将在您的 sheet 更新时执行,它将从 A6:A8 读取 "options"(在我的示例中)并使用它们填充 B3 中的下拉菜单。 B3 中的菜单将删除已删除的选项并创建新的选项。您可以将它与您的代码结合起来,从您拥有它们的地方删除选项。
Here 您可以找到一些关于我使用的方法的文档以及有关处理数据验证的更多信息