Table of Contents

TFlexCelUserFunction Class

A class used to define a FlexCel user function, that you can call from a report.

Remarks

To Create a User function:

  1. Create a new class derived from TFlexCelUserFunction.
  2. Override the method Evaluate.
  3. Add the new user function to the report using TFlexCelReport.SetUserFunction.

Syntax

Unit: FlexCel.Report

TFlexCelUserFunction = class(TFlexCelObject);

Methods

Name Description
Evaluate Override this method on a derived class to implement your own defined function.

Examples

To define a user function that returns "One" for param = 1; "Two" for param = 2 and "Unknown" on any other case: 1) Define the class:

  TMyUserFunction = class (TFlexCelUserFunction)
  public
    constructor Create;
    function Evaluate(const parameters: TFormulaValueArray): TReportValue; override;
  end;
...
function TMyUserFunction.Evaluate(const parameters: TFormulaValueArray): TReportValue;
var
  d: double;
  p: Int32;
begin
  if (Length(parameters) <> 1) then
    raise Exception.Create('Invalid number of parameters for user defined function "MyUserFunction"');

  if not parameters[0].TryToDouble(d, false) then raise Exception.Create('The first parameter must be a double.');
  if (d < -1000) or (d > 1000) then raise Exception.Create('The value of the parameter must be between -1000 and 1000');
  p := Round(d);
  case p of
    1:
    begin
      exit('One');
    end;
    2:
    begin
      exit('Two');
    end;
  end;
  Result := 'Unknown';
end;

2) Add the function to the report.

  myFuncImpl := TMyUserFunction.Create;  //The MyUserFunction class implements an user defined function.
  //Note that we don't have to free myFuncImpl, it will be freed by the report once it is over.
  flexCelReport.SetUserFunction('MF', myFuncImpl);  //The name used in Excel will be =MF().

3) Now, you can write "<#MF(1)>" on a template, and it will be replaced by "One".