officefileapi-devexpress-dot-spreadsheet-dot-functions-99963263.md
Defines properties and methods required to implement your own worksheet function.
Namespace : DevExpress.Spreadsheet.Functions
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public interface ICustomFunction :
IFunction
Public Interface ICustomFunction
Inherits IFunction
The following members return ICustomFunction objects:
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Functions;
using DevExpress.XtraSpreadsheet;
// Create a custom function and add it to the global scope.
SphereMassFunction customFunction = new SphereMassFunction();
if (!workbook.Functions.GlobalCustomFunctions.Contains(customFunction.Name))
workbook.Functions.GlobalCustomFunctions.Add(customFunction);
public class SphereMassFunction : ICustomFunction
{
const string functionName = "SPHEREMASS";
readonly ParameterInfo[] functionParameters;
public SphereMassFunction()
{
// Missing optional parameters do not result in an error message.
this.functionParameters = new ParameterInfo[] { new ParameterInfo(ParameterType.Value, ParameterAttributes.Required),
new ParameterInfo(ParameterType.Value, ParameterAttributes.Optional)};
}
public string Name { get { return functionName; } }
ParameterInfo[] IFunction.Parameters { get { return functionParameters; } }
ParameterType IFunction.ReturnType { get { return ParameterType.Value; } }
bool IFunction.Volatile { get { return false; } }
ParameterValue IFunction.Evaluate(IList<ParameterValue> parameters, EvaluationContext context)
{
double radius;
double density = 1000;
ParameterValue radiusParameter;
ParameterValue densityParameter;
if (parameters.Count == 2)
{
densityParameter = parameters[1];
if (densityParameter.IsError)
return densityParameter;
else
density = densityParameter.NumericValue;
}
radiusParameter = parameters[0];
if (radiusParameter.IsError)
return radiusParameter;
else
radius = radiusParameter.NumericValue;
return (4 * Math.PI) / 3 * Math.Pow(radius,3) * density;
}
string IFunction.GetName(CultureInfo culture)
{
return functionName;
}
}
Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Functions
Imports DevExpress.XtraSpreadsheet
' Create a custom function and add it to the global scope.
Dim customFunction As New SphereMassFunction()
If Not workbook.Functions.GlobalCustomFunctions.Contains(customFunction.Name) Then
workbook.Functions.GlobalCustomFunctions.Add(customFunction)
End If
' Inheritance from Object is required for automatic VB.NET conversion
Public Class SphereMassFunction
Implements ICustomFunction
Private Const functionName As String = "SPHEREMASS"
Private ReadOnly functionParameters() As ParameterInfo
Public Sub New()
' Missing optional parameters do not result in an error message.
Me.functionParameters = New ParameterInfo() {
New ParameterInfo(ParameterType.Value, ParameterAttributes.Required),
New ParameterInfo(ParameterType.Value, ParameterAttributes.Optional)
}
End Sub
Public ReadOnly Property Name() As String Implements IFunction.Name
Get
Return functionName
End Get
End Property
Private ReadOnly Property IFunction_Parameters() As ParameterInfo() Implements IFunction.Parameters
Get
Return functionParameters
End Get
End Property
Private ReadOnly Property IFunction_ReturnType() As ParameterType Implements IFunction.ReturnType
Get
Return ParameterType.Value
End Get
End Property
Private ReadOnly Property IFunction_Volatile() As Boolean Implements IFunction.Volatile
Get
Return False
End Get
End Property
Private Function IFunction_Evaluate(ByVal parameters As IList(Of ParameterValue), ByVal context As EvaluationContext) As ParameterValue Implements IFunction.Evaluate
Dim radius As Double
Dim density As Double = 1000
Dim radiusParameter As ParameterValue
Dim densityParameter As ParameterValue
If parameters.Count = 2 Then
densityParameter = parameters(1)
If densityParameter.IsError Then
Return densityParameter
Else
density = densityParameter.NumericValue
End If
End If
radiusParameter = parameters(0)
If radiusParameter.IsError Then
Return radiusParameter
Else
radius = radiusParameter.NumericValue
End If
Return (4 * Math.PI) / 3 * Math.Pow(radius,3) * density
End Function
Private Function IFunction_GetName(ByVal culture As CultureInfo) As String Implements IFunction.GetName
Return functionName
End Function
End Class
See Also