Search Results for

    Show / Hide Table of Contents

    Custom Functions Connection to Report

    FastReport has a large number of built in standard functions, for use in report designing. There is also the ability to add your own custom functions. Function connection is performed using the “FastScript” script library interface, which is included in FastReport (to learn more about FastScript, refer to it’s library manual).

    An example of how procedures and/or functions can be connected. Function’s parameter quantity and type can vary. One cannot transfer parameters of “Set” and “Record" type, as they are not supported by FastScript. You must transfer such parameters as simpler types, for example, to transfer TRect as X0, Y0, X1, Y1: Integer. See more about process of adding functions with different parameters in FastScript documentation.

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

    function TForm1.MyFunc(s: String; i: Integer): Boolean; 
    begin 
    // necessary logic
    end; 
    
    procedure TForm1.MyProc(s: String); 
    begin 
    // necessary 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 to the function list (usually in on create or on show event of the Delphi form).

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

    Connected function can now be used in report script; furthermore, one can refer to it from objects of TfrxMemoView type. This function is also displayed in "Data tree" window’s function page tab. In this window functions are split into categories, and when you select any function, a hint about this function appears in the bottom pane of the window.

    Modify code sample above to register functions in separate category, and display function description hint:

    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'); 
    

    Your added items will now appear under the category “My Functions”.

    If you want to register functions in one of the 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 blank category name is specified, the function is placed in the functions tree root.

    If you wish to add a large number of functions, it is recommended to placet all logic 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 
    // necessary logic
    end; 
    
    procedure MyProc(s: String); 
    begin 
    // necessary 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 and all your custom functions will be available for use in any report component. No need to write code to add these functions to each TfrxReport, and no need to write additional code for each report component’s “onuserfunction “ handler.

    Back to top © 1998-2022 Copyright Fast Reports Inc.