基于脚本的依赖 Drop-Down 列表不起作用(工作表)
Script based Dependent Drop-Down List not working (Sheets)
我已将此脚本设置为在单元格 Y4 上提供下拉列表,具体取决于在单元格 S4 上选择的值。这两个值来自的数据基于另一个名为 DW 的 sheet,从第 2 行开始,因为它有 headers.
function onEdit(){
var tabLists = "DW";
var tabValidation = "EditItem";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 19 && activeCell.getRow() == 4 && ss.getSheetName() == tabValidation){
activeCell.offset(0, 6).clearContent().clearDataValidations();
var makes = datass.getRange("A2:C").getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()); //This gives me row2 to check below, but I need the column data, instead. How to change this one then?
if(makeIndex != 0){
var validationRange = datass.getRange("C2:C");
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 6).setDataValidation(validationRule);
}
}
}
我从 Y4 上列出的 DW("C2:C") 获得列表,但根据 S4 上选择的项目名称,它没有出现 "filtered"。
关于我遗漏了什么的任何线索?
谢谢!
如果我理解正确 - 问题出在你的陈述中 if(makeIndex != 0)
请记住,如果未在数组中找到条目,则方法 indexOf returns -1
而不是 0
。
因此,如果您只想在 activeCell.getValue()
包含在 "C2:C"
.
中执行数据验证,则应该查询 if(makeIndex != -1)
更新
- 您想知道
B
列中的哪一行包含 S4
中的值
- 您想将该行的第
C
列中的值用于 Y4
中的数据验证
在这种情况下你需要
- 将
B
列中的条目转换为可搜索数组 - 这可以通过 map() 完成
- 在列
C
中找到相应的值 - 这可以通过使用检索到的索引手动构建单元格的 A1 符号来完成
样本:
var makes = datass.getRange("A2:C").getValues();
var searchArray=makes.map(function(e){return e[1];});
var makeIndex = searchArray.indexOf(activeCell.getValue());
if(makeIndex != -1){
var validationRange = datass.getRange("C" + (2+makeIndex));
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 6).setDataValidation(validationRule);
}
Notes:
- It seems that you are not using column A, so there is no need to
include it in range
makes
- Notaions like
A2:C
are not very efficient becasue they include many empty rows. Instead it is better to use as a margin the last
row with
contents
我已将此脚本设置为在单元格 Y4 上提供下拉列表,具体取决于在单元格 S4 上选择的值。这两个值来自的数据基于另一个名为 DW 的 sheet,从第 2 行开始,因为它有 headers.
function onEdit(){
var tabLists = "DW";
var tabValidation = "EditItem";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 19 && activeCell.getRow() == 4 && ss.getSheetName() == tabValidation){
activeCell.offset(0, 6).clearContent().clearDataValidations();
var makes = datass.getRange("A2:C").getValues();
var makeIndex = makes[0].indexOf(activeCell.getValue()); //This gives me row2 to check below, but I need the column data, instead. How to change this one then?
if(makeIndex != 0){
var validationRange = datass.getRange("C2:C");
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 6).setDataValidation(validationRule);
}
}
}
我从 Y4 上列出的 DW("C2:C") 获得列表,但根据 S4 上选择的项目名称,它没有出现 "filtered"。
关于我遗漏了什么的任何线索?
谢谢!
如果我理解正确 - 问题出在你的陈述中 if(makeIndex != 0)
请记住,如果未在数组中找到条目,则方法 indexOf returns -1
而不是 0
。
因此,如果您只想在 activeCell.getValue()
包含在 "C2:C"
.
if(makeIndex != -1)
更新
- 您想知道
B
列中的哪一行包含S4
中的值
- 您想将该行的第
C
列中的值用于Y4
中的数据验证
在这种情况下你需要
- 将
B
列中的条目转换为可搜索数组 - 这可以通过 map() 完成
- 在列
C
中找到相应的值 - 这可以通过使用检索到的索引手动构建单元格的 A1 符号来完成
样本:
var makes = datass.getRange("A2:C").getValues();
var searchArray=makes.map(function(e){return e[1];});
var makeIndex = searchArray.indexOf(activeCell.getValue());
if(makeIndex != -1){
var validationRange = datass.getRange("C" + (2+makeIndex));
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
activeCell.offset(0, 6).setDataValidation(validationRule);
}
Notes:
- It seems that you are not using column A, so there is no need to include it in range
makes
- Notaions like
A2:C
are not very efficient becasue they include many empty rows. Instead it is better to use as a margin the last row with contents