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

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

我有一个带下拉菜单的Google表单(请参见下文)enter image description here

我在Google工作表上有一列,每天都会更新。

enter image description here

有什么方法可以自动将google工作表中的名称链接到google form下拉列表问题1,以便每次工作表使用其他名称进行更新时-google表单都会自动使用下拉列表中的名称进行更新。我想我们需要使用Google AppScript。向我指出正确方向的任何指导将不胜感激。

javascript google-apps-script google-sheets google-form
1个回答
1
投票

一个非常通用的脚本,但是您应该可以根据需要对其进行修改

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

}

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

在此示例中,仅当对工作表“名称”的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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.