Using Custom Functions in a Report 在報表中使用本身義函數app
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).ide
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.函數
注意:自定義函數的參數不支持Set和Record類型,TRect 類型轉換成4個參數傳遞。ui
In the Delphi form declare the function or procedure and its code.this
在Delphi窗體中定義函數或過程:code
function TForm1.MyFunc(s: String; i: Integer): Boolean; component
beginorm
// required logic事件
end; ip
procedure TForm1.MyProc(s: String);
begin
// required logic
end;
Create the 「onUser」 function handler for the report component. //編寫報表組件的OnUserFunction 事件
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).
或者調用報表組件的AddFunction方法註冊函數或過程:
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.
保存成文件,而後在項目中引用這個文件。全部的自定義函數就能夠在任何報表組件中使用,而不須要每個「TfrxReport」組件編寫代碼或事件來添加這些自定義函數。