简体   繁体   English

如何在 Google Sheets App Script 中创建第二个动态相关下拉列表?

[英]How do you create a second dynamic dependant dropdown in Google Sheets App Script?

I have started a Google Sheet which has 4 dropdowns.我已经启动了一个有 4 个下拉列表的 Google 表格。 Dropdowns 2, 3 and 4 all depend on the selection in dropdown 1. I have got dropdown 2 working but can't work out the code in App Script Editor for dropdowns 3 and 4.下拉菜单 2、3 和 4 都取决于下拉菜单 1 中的选择。我已经让下拉菜单 2 工作,但无法在应用脚本编辑器中为下拉菜单 3 和 4 计算代码。

This is my sheet titled 'Dropdown Lists':这是我的标题为“下拉列表”的工作表:

下拉列表

Row 1 is in dropdown 1 (Column C of the 'Event/Incidents' Sheet)第 1 行位于下拉列表 1 中(“事件/事件”表的 C 列)

Rows 4 to 10 are in dropdown 2 (Column D of the 'Event/Incidents' Sheet)第 4 到 10 行在下拉列表 2 中(“事件/事件”表的 D 列)

Rows 15 to the end row are in dropdowns 3 (Column E of the 'Event/Incidents' Sheet) & also in dropdown 4 (Column F of the 'Event/Incidents' Sheet) This is the 'Events/Incidents' Sheet.第 15 行到最后一行位于下拉列表 3(“事件/事件”表的 E 列)和下拉列表 4(“事件/事件”表的 F 列)这是“事件/事件”表。

事件/事件

I have the following code which works for dropdown 2 (Column D of the 'Events/Incidents' Sheet) only:我有以下代码仅适用于下拉列表 2(“事件/事件”表的 D 列):

应用脚本代码

Would really appreciate help with the rest of the code.非常感谢代码 rest 的帮助。

In order to adapt your script and create additional dropdown menus, you need to为了调整您的脚本并创建额外的下拉菜单,您需要

  • create additional validation rules创建额外的验证规则
  • with adjusted validation ranges调整后的验证范围
  • adjust the offset according to the position where the dropdown menu shall be inserted.根据应插入下拉菜单的 position 调整偏移量。

Sample:样本:

      var validationRange2=datass.getRange(15,baseIndex,datass.getLastRow());
      var validationRule2=SpreadsheetApp.newDataValidation().requireValueInRange(validationRange2).build()
      activeCell.offset(0,2).setDataValidation(validationRule2);

The full code would be:完整的代码是:

function onEdit() {
 var tablists="Dropdown Lists";
  var tabValidation="Events/Incidents";
  var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var datass=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tablists);
  var activeCell=ss.getActiveCell();
  if(activeCell.getColumn()==3&&activeCell.getRow()>1&&ss.getSheetName()==tabValidation){
    activeCell.offset(0,1).clearContent().clearDataValidations();
    var base=datass.getRange(2,1,1,5).getValues();
    var baseIndex=base[0].indexOf(activeCell.getValue())+1;
    Logger.log(baseIndex);
    if(baseIndex!=0){
      var validationRange=datass.getRange(3,baseIndex,10);
      var validationRule=SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build()
      activeCell.offset(0,1).setDataValidation(validationRule);

      var validationRange2=datass.getRange(15,baseIndex,datass.getLastRow());
      var validationRule2=SpreadsheetApp.newDataValidation().requireValueInRange(validationRange2).build()
      activeCell.offset(0,2).setDataValidation(validationRule2);
      activeCell.offset(0,3).setDataValidation(validationRule2);

    }
  }
}

To obtain a better understanding of the code, please refer to the Apps Scriptdocumentation and tutorials要更好地理解代码,请参阅 Apps 脚本文档教程

My recommendation is you watch the following video and then download the demo script.我的建议是您观看以下视频,然后下载演示脚本。 Your request is for cascading dynamic drop down menus, right?您的要求是级联动态下拉菜单,对吧?

https://youtu.be/rW9T4XZy-7U https://youtu.be/rW9T4XZy-7U

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用数组或对象中的值在 Google 表格中动态创建下拉列表? - How do you dynamically create a dropdown list in Google sheets using values from an array or perhaps an object? 你如何 select 并使用脚本删除谷歌表格中的图纸? - How do you select and delete drawings in google sheets with a script? 如何创建彼此依赖的两个Google地图 - how do I create two google maps dependant on each other 创建一个带有Google表格链接的下拉菜单? - create a dropdown with links in google sheets? 您如何延迟表单提交(到Google表格),以便可以首先通过脚本进行验证? - How do you delay form submission (to google sheets) so that it can be validated first by a script? 如何在 Google 表格中编写脚本以删除列中的某些值? - How do you make a script in Google Sheets to delete certain values in a column? 如何使用脚本从 Google 表格中“工作表 B”上的数据生成“工作表 A”上的列表 - How Do You Generate List On "Sheet A" from Data on "Sheet B" in Google Sheets Using a Script 使用 Google App Script 以 HTML 形式显示动态下拉列表 - Dynamic Dropdown List in HTML form using Google App Script 谷歌表格中的“脚本应用程序”是否会自动登录网站? - Does "Script App" from google sheets do auto login in websites? 如何在Google App脚本中创建一个弹出框? - How do I create a pop up box in Google App script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM