Using Custom Functions in a FastReport
























unit AwfrxUserDefFunc;

interface

uses System.SysUtils, System.Classes, fs_iinterpreter;

const
  MonthTH: array [1 .. 12] of string = ('มกราคม ', 'กุมภาพันธ์',
    'มีนาคม', 'เมษายน',

    'พฤษภาคม', 'มิถุนายน', 'กรกฎาคม', 'สิงหาคม',

    'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม');

  ShortMonthTH: array [1 .. 12] of string = ('ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.',

    'พ.ค. ', 'มิ.ย.', 'ก.ค.', 'ส.ค.',

    'ก.ย.', 'ต.ค.', 'พ.ย. ', 'ธ.ค.');
  MonthEN: array [1 .. 12] of string = ('January', 'February', 'March', 'April',

    'May', 'June', 'July', 'August',

    'September', 'October', 'November', 'December');

  ShortMonthEN: array [1 .. 12] of string = ('Jan.', 'Feb.', 'Mar.', 'Apr.',

    'May', 'Jun.', 'Jul.', 'Aug.',

    'Sep.', 'Oct.', 'Nov.', 'Dec.');

type
  TFrxUserDefFunction = class(TfsRTTIModule)
  private
    function CallMethod(Instance: TObject; ClassType: TClass;
      const MethodName: String; var Params: Variant): Variant;
  public
    constructor Create(AScript: TfsScript); override;
  end;

function GenderThai(const Gender: String): String;
function GenderEnglish(const Gender: String): String;
function DisplayShortDateFormatTH(const ADate: TDate): String;
function DisplayLongDateFormatTH(const ADate: TDate): String;
function DisplayShortDateFormatEN(const ADate: TDate): String;
function DisplayLongDateFormatEN(const ADate: TDate): String;

implementation

function DisplayLongDateFormatEN(const ADate: TDate): String;
var
  fYear, fMonth, fDay: Word;
begin
  DecodeDate(ADate, fYear, fMonth, fDay);
  result := IntToStr(fDay) + ' ' + MonthEN[fMonth] + ' ' + IntToStr(fYear);
end;

function DisplayLongDateFormatTH(const ADate: TDate): String;
var
  fYear, fMonth, fDay: Word;
begin
  DecodeDate(ADate, fYear, fMonth, fDay);
  result := IntToStr(fDay) + ' ' + MonthTH[fMonth] + ' ' +
    IntToStr(fYear + 543);
end;

function DisplayShortDateFormatEN(const ADate: TDate): String;
var
  fYear, fMonth, fDay: Word;
begin
  DecodeDate(ADate, fYear, fMonth, fDay);
  result := IntToStr(fDay) + ' ' + ShortMonthEN[fMonth] + ' ' + IntToStr(fYear);
end;

function DisplayShortDateFormatTH(const ADate: TDate): String;
var
  fYear, fMonth, fDay: Word;
begin
  DecodeDate(ADate, fYear, fMonth, fDay);
  result := IntToStr(fDay) + ' ' + ShortMonthTH[fMonth] + ' ' +
    IntToStr(fYear + 543);
end;

function GenderEnglish(const Gender: String): String;
begin
  if Gender = '1' then
    result := 'Male'
  else
    result := 'Female';
end;

function GenderThai(const Gender: String): String;
begin
  if Gender = '1' then
    result := 'ชาย'
  else
    result := 'หญิง';
end;

{ TFrxUserDefFunction }
constructor TFrxUserDefFunction.Create;
const
  g = 'User Functions/Procedures';
begin
  inherited Create(AScript);
  with AScript do
  begin
    AddMethod('function GenderThai(const Gender: String): String;', CallMethod,
      g, 'Convert Gender(str:1=ชาย,2=หญิง) to Male or Female in thai format.');
    AddMethod('function GenderEnglish(const Gender: String): String;',
      CallMethod, g,
      'Convert Gender(str:1=Male,2=Female) to Male or Female in English format.');
    AddMethod(' function DisplayShortDateFormatTH(const ADate: TDate): String;',
      CallMethod, g, 'Convert Date to Short date (Thai format.)');
    AddMethod(' function DisplayLongDateFormatTH(const ADate: TDate): String;',
      CallMethod, g, 'Convert Date to Long date (Thai format.)');
    AddMethod(' function DisplayShortDateFormatEN(const ADate: TDate): String;',
      CallMethod, g, 'Convert Date to Short date (English format.)');
    AddMethod(' function DisplayLongDateFormatEN(const ADate: TDate): String;',
      CallMethod, g, 'Convert Date to Long date (English format.)');
  end;
end;

function TFrxUserDefFunction.CallMethod(Instance: TObject; ClassType: TClass;
  const MethodName: String; var Params: Variant): Variant;
begin
  if MethodName = 'GENDERTHAI' then
    result := GenderThai(Params[0]);

  if MethodName = 'GENDERENGLISH' then
    result := GenderEnglish(Params[0]);

  if MethodName = 'DISPLAYSHORTDATEFORMATTH' then
    result := DisplayShortDateFormatTH(Params[0]);

  if MethodName = 'DISPLAYLONGDATEFORMATTH' then
    result := DisplayLongDateFormatTH(Params[0]);

  if MethodName = 'DISPLAYSHORTDATEFORMATEN' then
    result := DisplayShortDateFormatEN(Params[0]);

  if MethodName = 'DISPLAYLONGDATEFORMATEN' then
    result := DisplayLongDateFormatEN(Params[0]);
end;

initialization

fsRTTIModules.Add(TFrxUserDefFunction);

end.

//////////////
How to use.

uses AwfrxUserDefFunc; //use

procedure TMain.Btn_DesignrepClick(Sender: TObject);
begin
  frxReport1.LoadFromFile('..\..\rep_Demo.fr3');
  frxReport1.DesignReport();
end;

procedure TMain.Btn_ShowRepClick(Sender: TObject);
begin
  frxReport1.LoadFromFile('..\..\rep_Demo.fr3');
  frxReport1.ShowReport();
end;

Call user function.

Comments