为什么 ExcelDNA 将一些我没有要求的函数注册为 UDF?

问题描述 投票:0回答:1

我有一个用 C# 编写的基于 ExcelDNA 的插件 (XLL),它可以执行许多不同的操作,包括向 Excel 注册一些 UDF 以在单元格公式中使用。

奇怪的是,我有以下 UDF 代码,它成功地将 Excel 中的“ConvertUnit”函数注册为 UDF,但也最终也注册了来自另一个类的被调用函数“ConvertUnits”函数:

// This is in a public static class called MyFunctions in the global namespace
[ExcelFunction(Description = "Convert value", IsMacroType = false)]
public static object ConvertUnit(
    [ExcelArgument(Name = "Value", Description = "Value to be converted")] string value,
    [ExcelArgument(Name = "Property Group", Description = "Type of property to be converted e.g. \"temperature\", \"diff_temperature\"")] string unitGroup,
    [ExcelArgument(Name = "From Units", Description = "Units of measure to convert from")] string fromUom,
    [ExcelArgument(Name = "To Units", Description = "Units of measure to convert to")] string toUom)
{ // UDF to convert a between UoM

    double dblValue;
    bool isValue = double.TryParse(value, out dblValue);
    if (!isValue) return Common.errValue;
    { return UnitConversion.ConvertUnits(dblValue, unitGroup, fromUom, toUom); }
}

UnitConversion.ConvertUnits 函数位于不同的命名空间中,称为 DataTool:

public static object ConvertUnits(double value, string unitGroup, string fromUom, string toUom)
{ // Use this to expose via a UDF for users to convert between UoM for a specified unitGroup
    string defaultUnit;
    double toJM_Multiplier; double fromJM_Multiplier;

    if (unitGroup == "temperature")
    {
        double tempVal;
        switch (fromUom)
        {
            case "C":
                tempVal = value;
                break;
            case "F":
                tempVal = (value - 32.0) / 1.8;
                break;
            case "K":
                tempVal = value - 273.15;
                break;
            case "R":
                tempVal = (value - 491.67) / 1.8;
                break;
            default:
                return Common.errUOM;
        }
        switch (toUom)
        {
            case "C":
                return tempVal;
            case "F":
                return tempVal * 1.8 + 32.0;
            case "K":
                return tempVal + 273.15;
            case "R":
                return tempVal * 1.8 + 491.67;
            default:
                return Common.errUOM;
        }
    }

    if (defaultUoM.ContainsKey(unitGroup))
    { // Get the default (JM) UoM for the unit group - should always exist but handle missing config file
        defaultUnit = defaultUoM[unitGroup];
    }
    else
    { return Common.errConfigFile; }

    if (!conversions.ContainsKey(unitGroup)) return Common.errConfigFile;

    if (conversions[unitGroup].ContainsKey(fromUom))
    { // Get the conversion factor if it is mapped in the config file
        toJM_Multiplier = conversions[unitGroup][fromUom];
    }
    else
    { return Common.errUOM; }

    if (conversions[unitGroup].ContainsKey(toUom))
    { // Get the conversion factor if it is mapped in the config file
        fromJM_Multiplier = conversions[unitGroup][toUom];
    }
    else
    { return Common.errUOM; }

    if (toJM_Multiplier != 0.0)
    { return value / toJM_Multiplier * fromJM_Multiplier; }
    else
    { return null; }
}

然后在构建的 XLL 中,我最终看到这两个函数在 Excel 中显示为 UDF,尽管我的 UnitConversion 类中没有 ExcelDNA 内容。为什么!?

c# excel-dna
1个回答
0
投票

默认情况下,所有公共静态方法都注册在 AddIn 中。

看看在此处输入链接描述

我看到这两个参数都可以帮助你:

<!-- Prevents every static public function from becomming a UDF, they will need an explicit [ExcelFunction] annotation. -->
<!-- Default value: false -->
<ExcelAddInExplicitExports></ExcelAddInExplicitExports>

<!-- Prevents automatic registration of functions and commands. -->
<!-- Default value: false -->
<ExcelAddInExplicitRegistration></ExcelAddInExplicitRegistration>
© www.soinside.com 2019 - 2024. All rights reserved.