[]
Represents the base type for a custom function.
Derive from this abstract class to define spreadsheet functions that behave like built-in
formula functions. A custom function supplies its function name, return type, and
optional parameter metadata through one of the CustomFunction constructors, and must
implement Evaluate(object[], ICalcContext) to calculate the result.
After a derived function is created, register it by using AddCustomFunction(CustomFunction, bool) before using it in formulas.
Custom functions can be configured to participate in calculation caching and error
propagation. Use IsVolatile to control whether repeated calls with the
same arguments can reuse cached results, and use AcceptErrors to control
whether error values such as #N/A or #VALUE! are passed to the function for
handling. By default, custom functions are not volatile and do not accept error arguments.
This type is the synchronous custom-function base class. For asynchronous evaluation scenarios, use AsyncCustomFunction.
public abstract class CustomFunction
Public MustInherit Class CustomFunction
CustomFunction function = new Add2Function();
Workbook.AddCustomFunction(function, true);
worksheet.Range["A1"].Formula = "=ADD2(10,20)";
object sum = worksheet.Range["A1"].Value;
class Add2Function : CustomFunction
{
public Add2Function()
: base("ADD2", FunctionValueType.Number, new Parameter[] {
new Parameter(FunctionValueType.Number),
new Parameter(FunctionValueType.Number)
})
{
}
public override object Evaluate(object[] arguments, ICalcContext context)
{
double left = Convert.ToDouble(arguments[0]);
double right = Convert.ToDouble(arguments[1]);
return left + right;
}
}
| Name | Description |
|---|---|
| CustomFunction(string, FunctionValueType) | Initializes an instance of a custom function. |
| CustomFunction(string, FunctionValueType, Parameter[]) | Initializes an instance of a custom function.
This constructor passes |
| CustomFunction(string, string, FunctionValueType, Parameter[]) | Initializes an instance of a custom function. |
| Name | Description |
|---|---|
| AcceptErrors | Gets or sets whether the function accepts error values as arguments.
If this property returns |
| IsVolatile | Gets or sets whether this custom function is volatile.
If this property is |
| Name | Description |
|---|---|
| Evaluate(object[], ICalcContext) | Calculates the result of the custom function.
This method is called by the calculation engine when the custom function is evaluated.
The
If AcceptErrors returns |