In the recent release v2013.1 of the DevExpress WinForms Subscription, we introduced a brand new Spreadsheet Control. We are hard at work adding features to it and in v13.1.5 we’ve added support for Custom Functions
To register a Custom Function, we simply need to implement a CustomFunction interface and add the object to workbook’s CustomFunctions collection.
Here is a sample implementation a custom function SPHEREMASS(radius, density). The function calculates the mass of a sphere given it’s radius and density. If parameter density is not specified, the density of water (1000 kg/m3) is assumed.
if (!workbook.CustomFunctions.Contains(customFunction.Name))
workbook.CustomFunctions.Add(customFunction);
publicclassSphereMassFunction : ICustomFunction
{
conststring functionName = "SPHEREMASS";
readonlyParameterInfo[] functionParameters;
public SphereMassFunction()
{
// Missing optional parameters do not result in error message.
this.functionParameters = newParameterInfo[] { newParameterInfo(ParameterType.Value, ParameterAttributes.Required),
newParameterInfo(ParameterType.Value, ParameterAttributes.Optional)};
}
publicstring Name { get { return functionName; } }
ParameterInfo[] CustomFunction.Parameters { get { return functionParameters; } }
ParameterTypeCustomFunction.ReturnType { get { returnParameterType.Value; } }
// Reevaluate cells on every recalculation.
boolCustomFunction.Volatile { get { returnfalse; } }
ParameterValueCustomFunction.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;
}
stringCustomFunction.GetName(CultureInfo culture)
{
return functionName;
}
}Build v13.1.5 is available for immediate download from the Download Manager.
Cheers,
Azret