在谷歌表单中的条件下拉菜单

huangapple go评论58阅读模式
英文:

Conditional Dropdown in Google form

问题

我有一个Google表格,其中有两列,一列对应部门,第二列对应项目。我想创建一个Google表单,受访者必须从下拉菜单中选择一个部门,然后在第二个问题中选择一个项目。但我只希望那些与他们选择的部门相对应的项目出现在第二个问题中。我尝试使用Apps Script,并成功将第一列导入到第一个问题的下拉菜单中。有人可以帮助创建第二个条件下拉菜单吗?如果不可能,我认为Google表单有一个选项,可以根据答案将受访者重定向到一个新的部分。这个路线可以通过编码自动实现吗?提前感谢!

以下是我用来创建第一个下拉菜单的代码。

function updateDropdown() {
  // 获取Google表单
  var form = FormApp.getActiveForm();

  // 通过它们的索引获取目标下拉菜单项(根据需要进行调整)
  var firstDropdownIndex = 0; // 表单中第一个下拉菜单项的索引
  var secondDropdownIndex = 1; // 表单中第二个下拉菜单项的索引
  var firstDropdownItem = form.getItems()[firstDropdownIndex];
  var secondDropdownItem = form.getItems()[secondDropdownIndex];

  // 从Google表格获取数据
  var sheetId = '103-vF2U3OEG6_k25IU8bn0oODXxS3XiyuJxokXfqJMU';
  var sheetName = 'Sheet1'; // 替换为实际的工作表名称
  var sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
  var data = sheet.getRange('A1:B91').getValues(); // 根据需要调整范围

  // 从第一列中提取第一个下拉菜单选项的唯一值
  var firstDropdownChoices = [...new Set(data.map(function(row) {
    return row[0]; // 假设第一列包含第一个下拉菜单的选项
  }))];

  // 设置第一个下拉菜单项的选项
  var firstDropdown = firstDropdownItem.asListItem();
  firstDropdown.setChoiceValues(firstDropdownChoices);
英文:

I have a google sheet with two columns, one corresponding to department and the second corresponding to projects. I would like to create a google form in which respondents have to select a department from the dropdown and then in the second question select a project. But I want only those projects that correspond to their selection if department to appear in the second question. I tried using Apps Script and I was able to import the first column into the dropdown for the first question. Could someone help with creating the second conditional dropdown. If that's not possible, I think google forms has an option to redirect respondents to a new section based on a response. Can this route be made automatic by coding? Thanks in advance!

Below is the code I used to create the first dropdown.

function updateDropdown() {
  // Get the Google Form
  var form = FormApp.getActiveForm();

  // Get the target dropdown items by their indices (adjust as needed)
  var firstDropdownIndex = 0; // Index of the first dropdown item in the form
  var secondDropdownIndex = 1; // Index of the second dropdown item in the form
  var firstDropdownItem = form.getItems()[firstDropdownIndex];
  var secondDropdownItem = form.getItems()[secondDropdownIndex];

  // Get the data from the Google Sheet
  var sheetId = '103-vF2U3OEG6_k25IU8bn0oODXxS3XiyuJxokXfqJMU';
  var sheetName = 'Sheet1'; // Replace with the actual sheet name
  var sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
  var data = sheet.getRange('A1:B91').getValues(); // Adjust the range as needed

  // Extract the unique values from the first column for the first dropdown choices
  var firstDropdownChoices = [...new Set(data.map(function(row) {
    return row[0]; // Assuming the first column contains the options for the first dropdown
  }))];

  // Set the choices for the first dropdown item
  var firstDropdown = firstDropdownItem.asListItem();
  firstDropdown.setChoiceValues(firstDropdownChoices);

答案1

得分: 1

在这种情况下,没有选项可以仅基于之前问题的选择回答来显示下一个问题中的特定选项,但您可以使用基于答案显示问题功能,基本上您需要根据可用的部门创建多个部分。

关于您关于使用编码实现基于答案显示问题功能的问题,我一直在官方文档中搜索,但我找到的唯一一个用于添加新部分的类是SectionHeaderItem类,在我端上进行了多次测试后,没有可用的方法来在Google表单中添加一个部分,您只能向现有部分添加标题。

尽管如此,通过Google Apps脚本在Google表单中创建新部分的能力将是一个不错的功能。如果您有时间,可以提出一个功能请求

参考资料:

英文:

In this case there are no options to only show some specific options in the next question based on the answer selected before that question, however the option you have is to use the feature Show questions based on answers basically you'll need to create multiple sections based on the departments available.

In regards to your question about using coding to implement the Show questions based on answers feature, I've been searching through the official documentation but the only class I found to add a new section is the class SectionHeaderItem, after multiple testing on my end there's no method available to Add a section in a Google Form as you need it, you can only add a header to the existing section.

Nevertheless, having the ability to create a new section in Google Forms through Google Apps Scripts would be a nice feature to have. If you have the time, you can open a Feature request

References:

huangapple
  • 本文由 发表于 2023年6月15日 17:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定