ClosedXML: 依赖下拉菜单移除空选项

huangapple go评论78阅读模式
英文:

ClosedXML: Dependent dropdown remove empty options

问题

You can modify your code to dynamically update the options in the F1 dropdown menu based on the selection in E1. Here's an updated version of your code in Chinese:

ws.Cell("A1").InsertData(new []{ 1,2,3});
ws.Cell("B1").InsertData(new []{ 4,5}, true); // 这会转置数据,使其成为行而不是列
ws.Cell("B2").InsertData(new []{ 6}, true);
ws.Cell("B3").InsertData(new []{ 7,8,9}, true);
ws.Range("A1:A3").AddToNamed("Parents");
ws.Range("B1:D3").AddToNamed("Children");
var parentCell = ws.Cell("E1");
parentCell.CreateDataValidation().List(ws.Range("A1:A3"), true);
var formula = "=INDEX(Children,MATCH(E1,Parents,0),0)";
var childCell = ws.Cell("F1");
childCell.CreateDataValidation().List(formula, true);

By using the formula "=INDEX(Children,MATCH(E1,Parents,0),0)" for the F1 dropdown, it will dynamically update the options in F1 based on the selection in E1, removing empty options.

英文:

I have this table in excel :

A B C D
1 4 5
2 6
3 7 8 9

Where 4 and 5 are the children of 1, 6 - of 2, and 7, 8 and 9 - of 3.
I want a dependent dropdown list where in cell E1 i have a dropdown with the options 1,2 and 3, and in cell F1 I will have the options 4 and 5 if 1 is chosen in E1 and so on.

my code looks like this:

ws.Cell("A1").InsertData(new []{ 1,2,3});
ws.Cell("B1").InsertData(new []{ 4,5}, true); // this transposes the data, making it a row instead of a column
ws.Cell("B2").InsertData(new []{ 6}, true);
ws.Cell("B3").InsertData(new []{ 7,8,9}, true);
ws.Range("A1:A3").AddToNamed("Parents");
ws.Range("B1:D3").AddToNamed("Children");
var parentCell = ws.Cell("E1");
parentCell.CreateDataValidation().List(ws.Range("A1:A3"), true);
var childCell = ws.Cell("F1");
childCell.CreateDataValidation().List("=INDEX(Children,MATCH(E1,Parents,0),0)", true);

This works fine but the problem is that when I select 1 or 2 in E1, there are empty options in the F1 dropdown menu. How do I remove these?

答案1

得分: 0

我在Excel中为每个子列表分别命名了一个范围,并在F1单元格中使用了INDIRECT来引用E1中的值。我在范围的名称前添加了下划线,因为范围名称不能以数字开头,并且在比较E1的值与范围名称时添加了下划线。代码现在如下所示:

ws.Cell("A1").InsertData(new []{ 1,2,3});
ws.Cell("B1").InsertData(new []{ 4,5}, true); // this transposes the data, making it a row instead of a column
ws.Cell("B2").InsertData(new []{ 6}, true);
ws.Cell("B3").InsertData(new []{ 7,8,9}, true);
ws.Range("B1:C1").AddToNamed("_1");
ws.Range("B2:B2").AddToNamed("_2");
ws.Range("B3:D3").AddToNamed("_3");
var parentCell = ws.Cell("E1");
parentCell.CreateDataValidation().List(ws.Range("A1:A3"), true);
var childCell = ws.Cell("F1");
childCell.CreateDataValidation().List("=INDIRECT(CONCATENATE(\"_\", $E$1))", true);
英文:

I named each of the child lists as a separate range in excel and in the F1 cell i used INDIRECT to reference the value in E1. i added _ to the names of the ranges because a range name cannot start with a number, and added an _ when comparing E1's value to the range names. Code now looks like this

ws.Cell("A1").InsertData(new []{ 1,2,3});
ws.Cell("B1").InsertData(new []{ 4,5}, true); // this transposes the data, making it a row instead of a column
ws.Cell("B2").InsertData(new []{ 6}, true);
ws.Cell("B3").InsertData(new []{ 7,8,9}, true);
ws.Range("B1:C1").AddToNamed("_1");
ws.Range("B2:B2").AddToNamed("_2");
ws.Range("B3:D3").AddToNamed("_3");
var parentCell = ws.Cell("E1");
parentCell.CreateDataValidation().List(ws.Range("A1:A3"), true);
var childCell = ws.Cell("F1");
childCell.CreateDataValidation().List("=INDIRECT(CONCATENATE(\"_\", $E$1))", true);


</details>



huangapple
  • 本文由 发表于 2023年6月29日 15:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定