使用 Excel-DNA,如何区分 Excel 加载项函数调用中的未指定参数与缺失参数?

With Excel-DNA, how can an unspecified argument in an Excel add-in function call be distinguished from a missing one?

这个问题是关于 Excel-DNA 对 Excel.

中加载项函数调用的解释

在Excel中,未指定缺失参数不相同。这可以通过 IF 函数看到(它有两个必需参数,后跟一个可选参数):

=IF() - 这是无效的

=IF(,) - return 错误

=IF(,,) - returns 0

我正在使用 ExcelIntegration.RegisterDelegates() 方法在 Excel 中注册插件函数,注册函数如下:

Delegate del = new Func<object, object, object, object>((a, b, c) => a.ToString() + " " + b.ToString() + " " + c.ToString());

var fnAttr = new ExcelFunctionAttribute { Name = "TestFunc", Category = "ExcelDnaTest", Description = "Test function", IsHidden = false };

var args = new List<object>();
args.Add(new ExcelArgumentAttribute { Name = "Arg1", Description = "First argument", AllowReference = true });
args.Add(new ExcelArgumentAttribute { Name = "Arg2", Description = "Second argument", AllowReference = true });
args.Add(new ExcelArgumentAttribute { Name = "Arg3", Description = "Third argument", AllowReference = true });

ExcelIntegration.RegisterDelegates(new List<Delegate> { del }, new List<object> { fnAttr }, new List<List<object>> { args });

当提供所有参数时,这会按预期工作:

=TestFunc("A", "B", "C") - returns "A B C"

但是,没有参数(即三个 未指定 参数)的结果与两个或三个 missing 参数相同提供(我不知道如何提供单个 missing 参数):

=TestFunc()

=TestFunc(,)

=TestFunc(,,)

所有这些return:

"ExcelDna.Integration.ExcelMissing ExcelDna.Integration.ExcelMissing ExcelDna.Integration.ExcelMissing"

对于类似于Excel的IF函数的插件函数,这三个公式代表不同的函数调用,但我找不到区分它们的方法 - Excel-DNA 似乎为 unspecifiedmissing 参数提供其 Missing 值。

问:使用Excel-DNA,如何区分上面对TestFunc()的三个调用?

您无法在用户定义的函数中区分这些情况。

我不相信 Excel C API(Excel-DNA 用于实现 Excel -> .NET UDF 接口)允许任何 UDF看到这种区别 - 在 C API 中没有 'unspecified' 参数这样的概念 - 你的两种情况下的 XLOPER 类型都作为 xltypeMissing.

传递