Using Custom Functions in a Report

Top  Previous  Next

 

FastReport has a large number of built-in standard functions for use in report designs. FastReport also allows custom functions to be written and used. Functions are added using the “FastScript” library interface, which is included in FastReport (to learn more about FastScript refer to it’s library manual).

 

Let's look at how procedures and/or functions can be added to FastReport. The number and types of parameters vary from function to function. Parameters of “Set” and “Record" type are not supported by FastScript, so they must be implemented using simpler types, for instance a TRect can be passed as  four integers : X0, Y0, X1, Y1. There is more about the use of functions with various parameters in the FastScript documentation.

 

In the Delphi form declare the function or procedure and its code.

 

function TForm1.MyFunc(s: String; i: Integer): Boolean;

begin

// required logic

end;

 

procedure TForm1.MyProc(s: String);

begin

// required logic

end;

 

Create the “onUser” function handler for the report component.

 

function TForm1.frxReport1UserFunction(const MethodName: String;

                                      var Params: Variant): Variant;

begin

if MethodName = 'MYFUNC' then

   Result := MyFunc(Params[0], Params[1])

else if MethodName = 'MYPROC' then

   MyProc(Params[0]);

end;

 

Use the report component’s add method to add it to the function list (usually in the “onCreate” or “onShow” event of the Delphi form).

 

frxReport1.AddFunction('function MyFunc(s: String; i: Integer):Boolean');

frxReport1.AddFunction('procedure MyProc(s: String)');

 

The added function can now be used in a report script and can be referenced by objects of the “TfrxMemoView” type. The function is also displayed on the "Data tree" functions tab. On this tab functions are divided into categories and when selected a hint about the function appears in the bottom pane of the tab.

 

Modify the code sample above to register functions in separate categories, and to display descriptive hints:

 

frxReport1.AddFunction('function MyFunc(s: String; i: Integer): Boolean',

                      'My functions',

                      ' MyFunc function always returns True');

frxReport1.AddFunction('procedure MyProc(s: String)',

                      'My functions',

                      ' MyProc procedure does not do anything');

 

The added functions will appear under the category “My functions”.

 

To register functions in an existing categories use one of the following category names:

 

- 'ctString'string function
- 'ctDate'date/time functions
- 'ctConv'conversion functions
- 'ctFormat'formatting
- 'ctMath'mathematical functions
- 'ctOther'other functions

 

If the category name is left blank the function is placed under the functions tree root. To add a large number of functions it is recommended that all logic is placed in a separate library unit. Here is an example:

 

unit myfunctions;

 

interface

 

implementation

 

uses SysUtils, Classes, fs_iinterpreter;

          // you can also add a reference to any other external library here

 

type

 TFunctions = class(TfsRTTIModule)

private

  function CallMethod(Instance: TObject; ClassType: TClass;

                      const MethodName: String; var Params: Variant): Variant;

public

  constructor Create(AScript: TfsScript); override;

end;

 

 

function MyFunc(s: String; i: Integer): Boolean;

begin

// required logic

end;

 

procedure MyProc(s: String);

begin

// required logic

end;

 

 

{ TFunctions }

 

constructor TFunctions.Create;

begin

inherited Create(AScript);

with AScript do

   AddMethod('function MyFunc(s: String; i: Integer): Boolean', CallMethod,

            'My functions', ' MyFunc function always returns True');

   AddMethod('procedure MyProc(s: String)', CallMethod,

            'My functions', ' MyProc procedure does not do anything'');

end;

end;

 

function TFunctions.CallMethod(Instance: TObject; ClassType: TClass;

                              const MethodName: String;

                              var Params: Variant): Variant;

begin

if MethodName = 'MYFUNC' then

   Result := MyFunc(Params[0], Params[1])

else if MethodName = 'MYPROC' then

   MyProc(Params[0]);

end;

 

initialization

 fsRTTIModules.Add(TFunctions);

 

end.

 

Save the file with a .pas extension then add a reference to it in the “uses” clause of your Delphi project’s form. All your custom functions will then be available for use in any report component, without the need to write code to add these functions to each “TfrxReport” and without the need to write additional code for each report component’s “onUser” function handler.