Using Custom Functions in a Report 在报表中使用自己义函数
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.
注意:自定义函数的参数不支持Set和Record类型,TRect 类型转换成4个参数传递。
In the Delphi form declare the function or procedure and its code.
在Delphi窗体中定义函数或过程:
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. //编写报表组件的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”组件编写代码或事件来添加这些自定义函数。