Back to Devexpress

WorkbookFunctions.OverrideFunction(String, ICustomFunction) Method

officefileapi-devexpress-dot-spreadsheet-dot-functions-dot-workbookfunctions-dot-overridefunction-x28-system-dot-string-devexpress-dot-spreadsheet-dot-functions-dot-icustomfunction-x29.md

latest5.7 KB
Original Source

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.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void OverrideFunction(
    string name,
    ICustomFunction function
)
vb
Sub OverrideFunction(
    name As String,
    function As ICustomFunction
)

Parameters

NameTypeDescription
nameString

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.

csharp
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);
            }
        }
    }
vb
Imports DevExpress.Spreadsheet.Functions
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
        Private Sub ReplaceSinFunction()
            Dim functions As WorkbookFunctions = spreadsheetControl1.Document.Functions
            ' Obtain the built-in function to override.
            Dim sinFunction As IFunction = functions.Math.Sin
            ' Create a new function descending from the FunctionWrapper
            ' to replace the built-in function.
            Dim [function] As New OverridenFunction(sinFunction)
            ' Substitute the built-in function with the custom function.
            functions.OverrideFunction([function].Name, [function])
        End Sub

        ' A custom function that calculates the sine function for the angle measured in degerees.
        Public Class OverridenFunction
            Inherits FunctionWrapper

            Public Sub New(ByVal [function] As IFunction)
                MyBase.New([function])
            End Sub

            Public Overrides Function Evaluate(ByVal parameters As IList(Of ParameterValue), ByVal context As EvaluationContext) As ParameterValue
                Dim value As ParameterValue = parameters(0)
                If value.IsError Then
                    Return value
                End If

                Dim angle As Double = parameters(0).NumericValue * Math.PI / 180
                Dim sin As Double = Math.Sin(angle)
                Return Math.Round(sin, 5)
            End Function
        End Class

See Also

WorkbookFunctions Interface

WorkbookFunctions Members

DevExpress.Spreadsheet.Functions Namespace