google-apps-script - Google Spreadsheet Form,根据电子表格填充表单选项

标签 google-apps-script google-forms

我需要找到一种方法,让 Google 表单中的多项选择选项根据 Google 电子表格中不断变化的一组单元格进行更改。有什么想法吗?

最佳答案

这是可能的。在电子表格中编写您自己的脚本来更新您的表单。如果您不熟悉编写脚本,您可能会在 Google 脚本库中找到某人的脚本。让我们看看我的脚本示例。该脚本用于更新两个列表框。

function updateForm(){
  // a way to get the items from the form
  var form = FormApp.openById("<your form id>");
  var agentList = form.getItemById(<your item id>).asListItem();
  var hotelList = form.getItemById(<your item id>).asListItem();


  var ss = SpreadsheetApp.getActive();
  var agents = ss.getSheetByName("Agents");
  var hotels = ss.getSheetByName("Hotels");

  // get the values in the first column accept header row 1
  var agentValues = agents.getRange(2, 1, agents.getMaxRows() - 1).getValues();
  var hotelValues = hotels.getRange(2, 1, hotels.getMaxRows() - 1).getValues();

  var agentNames = [];
  var hotelNames = [];

  // convert 2D to 1D array and ignore empty cells
  for(var i = 0; i < agentValues.length; i++) {  
    if(agentValues[i][0] != "") {
      agentNames[i] = agentValues[i][0];
    }
  }

  for(var i = 0; i < hotelValues.length; i++) {
    if(hotelValues[i][0] != "") {
      hotelNames[i] = hotelValues[i][0];
    }
  }

  // populate the list
  agentList.setChoiceValues(agentNames);
  hotelList.setChoiceValues(hotelNames);
}

您还可以将此功能链接到电子表格编辑/更改事件,以使列表自动更新。

关于google-apps-script - Google Spreadsheet Form,根据电子表格填充表单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8084953/

相关文章:

javascript - 从 Google Apps 脚本中的用户名获取姓名和姓氏

google-apps-script - Google 表单 - 自动填写多项选择调查

google-apps-script - 从 Google 表格中获取指向单个 Google 表单响应的链接

google-forms - 在谷歌表单中将文本右对齐

google-sheets - 如何在 SPSS 中处理(Google 表单 - 电子表格) "Checkboxes"答案

google-apps-script - 如何从 Forms App 中的 VideoItem 获取 VideoID 或 VideoUrl?

javascript - 使用 Google Drive API 上传大文件

javascript - Google Apps 脚本预填写表单 - 无法编辑表单

mysql - 如何创建 View ?

google-apps-script - 如何使用嵌入图表(谷歌表格)中的应用程序脚本更改垂直轴的最小值?