WorkbookFunctions.OverrideFunction(String, ICustomFunction) Method
Replaces the built-in function specified by its name with a custom function.
Namespace: DevExpress.Spreadsheet.Functions
Assembly: DevExpress.Spreadsheet.v19.1.Core.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
name | String | A name of the function to replace. |
function | ICustomFunction | An ICustomFunction descendant that is the custom function. |
Remarks
Implement a custom function by creating a descendant of the FunctionWrapper class:
This example creates a function that calculates the sine of an angle measured in degrees and uses that function to replace the built-in SIN worksheet function.
The collection of worksheet functions is available by using the IWorkbook.Functions property. The WorkbookFunctions.OverrideFunction method is used to override a worksheet built-in function. The method requires the function to replace and a custom function with which the built-in function will be replaced.
To create a custom function, implement the FunctionWrapper descendant and override the FunctionWrapper.Evaluate method.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/udf-custom-function-how-to-replace-the-built-in-worksheet-function-t124453
using DevExpress.Spreadsheet.Functions;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
private void ReplaceSinFunction()
{
WorkbookFunctions functions = spreadsheetControl1.Document.Functions;
// Obtain the built-in function to override.
IFunction sinFunction = functions.Math.Sin;
// Create a new function descending from the FunctionWrapper
// to replace the built-in function.
OverridenFunction function = new OverridenFunction(sinFunction);
// Substitute the built-in function with the custom function.
functions.OverrideFunction(function.Name, function);
}
// A custom function that calculates the sine function for the angle measured in degerees.
public class OverridenFunction : FunctionWrapper
{
public OverridenFunction(IFunction function)
: base(function)
{
}
public override ParameterValue Evaluate(IList<ParameterValue> parameters, EvaluationContext context)
{
ParameterValue value = parameters[0];
if (value.IsError)
return value;
double angle = parameters[0].NumericValue * Math.PI / 180;
double sin = Math.Sin(angle);
return Math.Round(sin, 5);
}
}
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the OverrideFunction(String, ICustomFunction) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.