Skip to main content

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.v24.1.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

void OverrideFunction(
    string name,
    ICustomFunction function
)

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.

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);
            }
        }
    }
See Also