如何在 ExcelDNA XLL UDF 中处理 Excel 数组公式
How to handle Excel array formula in ExcelDNA XLL UDF
我真的很难理解如何允许将我的 UDF 作为数组公式输入的可能性。我翻阅了各种帖子寻找答案,但我认为我的理解一定存在差距。
以ExcelDNA tutorial double MultiplyThem(double x, double y)为例进行演示。
如果我 select 一系列单元格并输入 {=MultiplyThem({1;2;3;4},5)} 作为数组公式 I期望看到 selected 列范围充满
5
10
15
20
然而,我得到的全是5。此外,如果我的目标范围大于可用值的数量,我希望看到 #N/A 值,但我再次只看到值 5.
如何处理数组公式?我是否需要 returns double[] 的 UDF 重载,或者是否有一些内置的 Excel 功能会使用适当的数组值重复调用我的 UDF。
Excel UDF 可以将数组作为输入,并将 return 数组作为结果,但这必须明确地完成。不支持自动扩展标量函数以处理数组。
如果您将 MultiplyThem
函数的签名更改为
public static object MuliplyThem(object x, object y) {…}
您的函数将在调用时接收完整数组 {=MultiplyThem({1,2,3,4},5)}
。
然后您需要在函数中添加类型检查以确保正确处理不同的选项。 (当你声明参数为'double'时Excel会尝试转换输入,如果不兼容return #VALUE
。这里你必须处理类型检查和转换。)
您可以获得的“对象”参数的所有值的详尽示例如下所示:
[ExcelFunction(Description="Describes the value passed to the function.")]
public static string Describe(object arg)
{
if (arg is double)
return "Double: " + (double)arg;
else if (arg is string)
return "String: " + (string)arg;
else if (arg is bool)
return "Boolean: " + (bool)arg;
else if (arg is ExcelError)
return "ExcelError: " + arg.ToString();
else if (arg is object[,])
// The object array returned here may contain a mixture of different types,
// reflecting the different cell contents.
return string.Format("Array[{0},{1}]", ((object[,])arg).GetLength(0), ((object[,])arg).GetLength(1));
else if (arg is ExcelMissing)
return "<<Missing>>"; // Would have been System.Reflection.Missing in previous versions of ExcelDna
else if (arg is ExcelEmpty)
return "<<Empty>>"; // Would have been null
else
return "!? Unheard Of ?!";
}
在您的情况下,您将以特殊方式处理 object[,]
数组,并在适当的时候 return 一个 double[,]
数组或 object[,]
数组。如果输入不是您可以处理的类型,您可能 return 一个错误,很可能 ExcelError.ExcelErrorValue
.
我真的很难理解如何允许将我的 UDF 作为数组公式输入的可能性。我翻阅了各种帖子寻找答案,但我认为我的理解一定存在差距。
以ExcelDNA tutorial double MultiplyThem(double x, double y)为例进行演示。
如果我 select 一系列单元格并输入 {=MultiplyThem({1;2;3;4},5)} 作为数组公式 I期望看到 selected 列范围充满
5 10 15 20
然而,我得到的全是5。此外,如果我的目标范围大于可用值的数量,我希望看到 #N/A 值,但我再次只看到值 5.
如何处理数组公式?我是否需要 returns double[] 的 UDF 重载,或者是否有一些内置的 Excel 功能会使用适当的数组值重复调用我的 UDF。
Excel UDF 可以将数组作为输入,并将 return 数组作为结果,但这必须明确地完成。不支持自动扩展标量函数以处理数组。
如果您将 MultiplyThem
函数的签名更改为
public static object MuliplyThem(object x, object y) {…}
您的函数将在调用时接收完整数组 {=MultiplyThem({1,2,3,4},5)}
。
然后您需要在函数中添加类型检查以确保正确处理不同的选项。 (当你声明参数为'double'时Excel会尝试转换输入,如果不兼容return #VALUE
。这里你必须处理类型检查和转换。)
您可以获得的“对象”参数的所有值的详尽示例如下所示:
[ExcelFunction(Description="Describes the value passed to the function.")]
public static string Describe(object arg)
{
if (arg is double)
return "Double: " + (double)arg;
else if (arg is string)
return "String: " + (string)arg;
else if (arg is bool)
return "Boolean: " + (bool)arg;
else if (arg is ExcelError)
return "ExcelError: " + arg.ToString();
else if (arg is object[,])
// The object array returned here may contain a mixture of different types,
// reflecting the different cell contents.
return string.Format("Array[{0},{1}]", ((object[,])arg).GetLength(0), ((object[,])arg).GetLength(1));
else if (arg is ExcelMissing)
return "<<Missing>>"; // Would have been System.Reflection.Missing in previous versions of ExcelDna
else if (arg is ExcelEmpty)
return "<<Empty>>"; // Would have been null
else
return "!? Unheard Of ?!";
}
在您的情况下,您将以特殊方式处理 object[,]
数组,并在适当的时候 return 一个 double[,]
数组或 object[,]
数组。如果输入不是您可以处理的类型,您可能 return 一个错误,很可能 ExcelError.ExcelErrorValue
.