如何根据 Google 表格中的范围自动更新 Google 表单中的下拉列表?

How to automatically update dropdown in a Google Form based on range in Google Sheets?

我有一个带有下拉列表的 google 表单(见下文)

我在 google sheet 上有一个专栏,每天都会更新。

有什么方法可以自动 link 从 google sheet 到 google 表单下拉列表问题 1 的名称,这样每次 sheet 会更新为其他名称 - google 表单会自动更新为下拉列表中的名称。我想我们需要使用 Google AppScript。任何指导我正确方向的指导将不胜感激。

一个非常通用的脚本,但您应该可以根据需要修改它

function updateForm(){

  var ss = SpreadsheetApp.openById('----------'); // ID of spreadsheet with names
  var sheet = ss.getSheetByName('Names'); // Name of sheet with range of names
  var nameValues = sheet.getRange('A2:A10').getValues(); // Get name values

  var form = FormApp.openById('---------');  // ID of form
  var formItems = form.getItems();
  var question = formItems[2].asListItem(); // Get the second item on the from 

  var names = []

  for(var x = 1; x < nameValues.length; x++){

    if(nameValues[x][0] != ""){ // Ignores blank cells
     names.push(question.createChoice(nameValues[x][0])) // Create an array of choice objects
   } 
  }
  var setQuestion1 = question.setChoices(names); // Update the question

}

要在编辑 sheet 时更新表单,您可以使用已安装的 onEdit 触发器。通过添加逻辑,您可以将表单的更新限制为仅在编辑特定范围时发生。

在此示例中,仅当对 sheet 'Names'

的 A 列进行编辑时,表单才会更新
function updateForm(e){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var sheetName = sheet.getSheetName();
  var getCol = e.range.getColumn(); 

  if(sheetName == 'Names' && 1){

  var nameValues = sheet.getRange('A2:A10').getValues(); // Get name values

  var form = FormApp.openById('---------');  // ID of form
  var formItems = form.getItems();
  var question = formItems[2].asListItem(); // Get the second item on the from 

  var names = []

  for(var x = 1; x < nameValues.length; x++){

    if(nameValues[x][0] != ""){ // Ignores blank cells
     names.push(question.createChoice(nameValues[x][0])) // Create an array of choice objects
   } 
  }
  var setQuestion1 = question.setChoices(names); // Update the question
  }
}