我有一个工作脚本,该脚本在一个名为“一个”的选项卡上创建了依赖的下拉列表。现在,我需要它为另一个名为“ middle”的相似选项卡和名为“ mix”的第三个选项卡工作。这些选项卡的结构彼此相似。如何使用以下脚本进行管理?
var mainWsName = "One";
var optionsWsName = "STOK";
var firstLevelColumn = 5;
var secondLevelColumn = 6;
var thirdLevelColumn = 7;
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainWsName);
var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(optionsWsName);
function onEdit(e){
var activeCell = e.range;
var val = activeCell.getValue();
var r = activeCell.getRow();
var c = activeCell.getColumn();
var wsName = activeCell.getSheet().getName();
if(wsName === mainWsName && c === firstLevelColumn && r > 4){
applyFirstLevelValidation(val,r);
} else if(wsName === mainWsName && c === secondLevelColumn && r > 4){
applySecondLevelValidation(val,r);
}
} //end onEdit
function applyFirstLevelValidation(val,r){
if(val === ""){
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, secondLevelColumn).clearDataValidations();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
} else {
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, secondLevelColumn).clearDataValidations();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] ===
val }); // <--- Modified
var listToApply = filteredOptions.map(function(o){ return o[1] });
var cell = ws.getRange(r, secondLevelColumn);
applyValidationToCell(listToApply,cell);
}
}
function applySecondLevelValidation(val,r){
if(val === ""){
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
} else {
ws.getRange(r, thirdLevelColumn).clearContent();
var firstLevelColValue = ws.getRange(r, firstLevelColumn).getValue();
var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] ===
firstLevelColValue && o[1] === val }); // <--- Modified
var listToApply = filteredOptions.map(function(o){ return o[2] });
var cell = ws.getRange(r, thirdLevelColumn);
applyValidationToCell(listToApply,cell);
}
}
function applyValidationToCell(list,cell){
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(list)
.setAllowInvalid(false)
.build();
cell.setDataValidation(rule)
}
答案 0 :(得分:0)
您的行
if(wsName === mainWsName && c === firstLevelColumn && r > 4){
包括一个if
语句,该语句仅在编辑的工作表名称等于applyFirstLevelValidation(val,r)
的情况下运行功能mainWsName
,
var mainWsName = "One";
现在,如果您要运行此功能多于一张纸:
您需要为所有感兴趣的工作表名称创建其他变量,并使用||
运算符将编辑后的工作表名称与所有这些变量进行比较。
您需要将已编辑的工作表名称wsName
传递给函数applyFirstLevelValidation
,以确保将代码应用于正确的工作表。
示例:
var mainWsName1 = "One";
var mainWsName2 = "Two";
var optionsWsName = "STOK";
var firstLevelColumn = 5;
var secondLevelColumn = 6;
var thirdLevelColumn = 7;
var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(optionsWsName);
function onEdit(e){
var activeCell = e.range;
var val = activeCell.getValue();
var r = activeCell.getRow();
var c = activeCell.getColumn();
var wsName = activeCell.getSheet().getName();
// compare the edited sheet name against multiple allowed sheet anmes with ||
if(wsName == mainWsName1 || wsName == mainWsName2 && r > 4){
if(c == firstLevelColumn){
applyFirstLevelValidation(val,r, wsName);
} else if(c == secondLevelColumn){
applySecondLevelValidation(val,r, wsName);
}
}
}
function applyFirstLevelValidation(val,r,wsName){
// obtain the sheet name dynamically from the onEdit() function
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wsName);
if(val == ""){
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, secondLevelColumn).clearDataValidations();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
} else {
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, secondLevelColumn).clearDataValidations();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] ==
val }); // <--- Modified
var listToApply = filteredOptions.map(function(o){ return o[1] });
var cell = ws.getRange(r, secondLevelColumn);
applyValidationToCell(listToApply,cell);
}
}
function applySecondLevelValidation(val,r, wsName){
//change in the same way as function applyFirstLevelValidation
}