简体   繁体   English

三个相互依赖的下拉列表

[英]Three drop down lists depending on each other

I need to get three depending drop-down lists, which depend on each other.我需要获得三个相互依赖的下拉列表。 Here is an editable sheet with the example.这是带有示例的可编辑工作 Cells in the yellow column contain a drop-down list which is based on usual data validation (Options sheet).黄色列中的单元格包含一个基于常规数据验证(选项表)的下拉列表。 Cells in the green column give a corresponding drop-down list, which is based on the script.绿色列中的单元格给出了相应的下拉列表,该列表基于脚本。 And in blue column I need to get a corresponding list from the sheet Options with names corresponding with the data in the middle green column.在蓝色列中,我需要从工作表选项中获取相应的列表,其名称与中间绿色列中的数据相对应。

I will appreciate any help.我将不胜感激任何帮助。

var mainWsName = "master";
var optionsWsName = "options";
var firstLevelColumn = 12;
var secondLevelColumn = 13;
var thirdLevelColumn = 14;

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 = options.filter(function(o){ return o[0] === val });
    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 =options.filter(function(o){ return o[0] === firstLevelColValue && o[1] === val });
    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

  • You want to show the dropdown list to the column "N" by the column "M".您希望通过“M”列向“N”列显示下拉列表。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • options is not declared for the function of applyFirstLevelValidation and applySecondLevelValidation .没有为applyFirstLevelValidationapplySecondLevelValidation的函数声明options
  • r.thirdLevelColumn of ws.getRange(r.thirdLevelColumn) occurs an error. r.thirdLevelColumn ws.getRange(r.thirdLevelColumn)ws.getRange(r.thirdLevelColumn)发生错误。 Because r is the number.因为r是数字。
  • .requireValueInlist(list) is .requireValueInList(list) . .requireValueInlist(list).requireValueInList(list) It's a spelling mistake.这是一个拼写错误。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From:从:

var filteredOptions = options.filter(function(o){ return o[0] === val });

To:到:

var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] === val });

and

From:从:

var filteredOptions =options.filter(function(o){ return o[0] === firstLevelColValue && o[1] === val });

To:到:

var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] === firstLevelColValue && o[1] === val });

and

From:从:

var listToApply = filteredOptions.map(function(o){ return o[2] });
var cell = ws.getRange(r.thirdLevelColumn);
applyValidationToCell(listToApply,cell);

To:到:

var listToApply = filteredOptions.map(function(o){ return o[2] });
var cell = ws.getRange(r, thirdLevelColumn);  // <--- Modified
applyValidationToCell(listToApply,cell);

and

From:从:

var rule = SpreadsheetApp
.newDataValidation()
.requireValueInlist(list)
.setAllowInvalid(false)
.build();

To:到:

var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(list)  // <--- Modified
.setAllowInvalid(false)
.build();

Whole modified script:整个修改后的脚本:

 var mainWsName = "master"; var optionsWsName = "options"; var firstLevelColumn = 12; var secondLevelColumn = 13; var thirdLevelColumn = 14; 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); // <--- Modified applyValidationToCell(listToApply,cell); } } function applyValidationToCell(list,cell){ var rule = SpreadsheetApp .newDataValidation() .requireValueInList(list) // <--- Modified .setAllowInvalid(false) .build(); cell.setDataValidation(rule) }

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM