[英]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为了调整您的脚本并创建额外的下拉菜单,您需要
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?
您的要求是级联动态下拉菜单,对吧?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.