从单张表格填充多个表单

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

不是编码员。我已经从其他来源复制/粘贴了代码以获得基本基础,但我需要帮助添加功能。

我有一个 Google 表格,其中的“事件”选项卡中包含数据,我想用它来自动填充多个不同 Google 表单中的下拉菜单。

我使用的代码仅针对单个表单进行设置,我需要设置代码的指导,以便我将来可以添加更多表单以自动填充。

第一个表单 ID 是

1jDopSo1ofeZpy_5rhL--bg3DpInidz2TtBmWfgKLfiw

第二个表单ID(需要添加到代码中)是

1DhnRIqvFt0z4LXjKB0JI3Yl7zeUaHyQchBKTjDvMvj0

function updateDropdown() {
  // Get the form and the spreadsheet
  var form = FormApp.openById('1jDopSo1ofeZpy_5rhL--bg3DpInidz2TtBmWfgKLfiw');
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('events');  

  // Get the data from the spreadsheet
  var data = sheet.getRange('A1:A').getValues();
  var items = data.flat().filter(String);  

  // Put the dropdown question in the form
  var formItems = form.getItems(FormApp.ItemType.LIST);
  var listItem = formItems[0].asListItem();  

  // Update the dropdown options
  listItem.setChoiceValues(items);

}

我发现了类似的问题

但我没有足够的经验来将这两种解决方案转换为我的特定情况。

google-sheets google-apps-script google-forms
1个回答
0
投票

您需要将每个表单的 URL 放入工作表的列表中。接下来将此列表获取为一个数组,并循环使用 FormApp.openByURL(URL) 打开每个表单,更新每个表单上的下拉列表。

function updateDropdown() {
 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('events');  

  // Get the data from the spreadsheet
  var data = sheet.getRange('A1:A').getValues();
  var items = data.flat().filter(String);  
  
  //Get the list of form URLs from the spreadsheet in Column D for example
  var formLinks = sheet.getRange('D1:D').getDisplayValues()
  var formURLs = formlinks.flat().filter(string);
  
  for (var x = 0; x < formURLs.length; x++){
    //Open the form 
    var formToUpdate = FormApp.openByUrl(formURLs[x]);
    
    // Put the dropdown question in the form
    var formItems = formToUpdate.getItems(FormApp.ItemType.LIST);
    var listItem = formItems[0].asListItem(); 
     
    // Update the dropdown options
    listItem.setChoiceValues(items);
    
  }


}

© www.soinside.com 2019 - 2024. All rights reserved.