如何使用Excel-DNA检测到对空单元格的引用?

问题描述 投票:0回答:1
     //when the following line encounters a reference to
     //an empty cell, the method returns #VALUE

''''double val = dataRange [0];返回值''''

[ExcelFunction(IsMacroType = true)]
public static object getVal([ExcelArgument(AllowReference = true)] double[] dataRange)
excel-dna
1个回答
0
投票

通过将函数的参数声明为double(或double[]),只允许您使用有效的double值。

如果需要检查特殊值,例如ExcelEmpty,则需要将函数的参数声明为object(或object[])。

例如

[ExcelFunction(IsMacroType = true)]
public static object getVal([ExcelArgument(AllowReference = true)] object[] dataRange)
{
    if (dataRange[0] == ExcelEmpty.Value)
    {
        // ...
    }

    // ...
}

[如果您希望通过ExcelReference获取值,则将输入参数声明为object,并检查它是否为ExcelReference

例如

[ExcelFunction(IsMacroType = true)]
public static object getVal([ExcelArgument(AllowReference = true)] object input)
{
    if (input is ExcelReference reference)
    {
        // ...

        var value = reference.GetValue();

        // ...
    }

    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.