[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.Workbook.AddCustomFunction

AddCustomFunction Method

AddCustomFunction(CustomFunction, bool)

Adds a custom function to the function set.

The added function can be used in worksheet formulas by the name defined in the specified CustomFunction instance. If a function with the same name already exists in the function set, this method throws an exception.

Declaration
public static void AddCustomFunction(CustomFunction func, bool canOverride = false)
Public Shared Sub AddCustomFunction(func As CustomFunction, Optional canOverride As Boolean = False)
Parameters
Type Name Description
CustomFunction func

The custom function instance to add. Must not be null.

bool canOverride

Whether an existing function with the same name can be replaced.

Examples
class MyAddFunction : CustomFunction
{
    public MyAddFunction()
        : base("WORKBOOK_API_MYADD", FunctionValueType.Number, new Parameter[]
        {
            new Parameter(FunctionValueType.Number, 0d),
            new Parameter(FunctionValueType.Number, 0d)
        })
    {
    }

    public override object Evaluate(object[] arguments, ICalcContext context)
    {
        return (double)arguments[0] + (double)arguments[1];
    }
}

Workbook.AddCustomFunction(new MyAddFunction());
worksheet.Range["A1"].Formula = "WORKBOOK_API_MYADD(1,2)";
Exceptions
Type Condition
ArgumentException

Thrown when a function with the same name already exists and canOverride is false.