FastReport 允许编写和使用自定义函数。
如何做到这一点您可以在:FastReport DeveloperManual-en.pdf中找到。
在第 37、38、39 页的“在报表中使用自定义函数”一章中
我希望这会有所帮助。
更新
在报告中使用自定义函数
FastReport 内置大量标准函数供使用
在报告设计中。 FastReport 还允许自定义函数
编写和使用。使用“FastScript”库添加功能
接口,包含在 FastReport 中(了解更多关于
FastScript 参考它的库手册)。
让我们看看程序是怎样的
和/或函数可以添加到 FastReport。数量和种类
参数因功能而异。 “设置”的参数和
FastScript 不支持“记录”类型,因此它们必须是
使用更简单的类型实现,例如 TRect 可以作为
四个整数:X0、Y0、X1、Y1。还有更多关于使用
FastScript 文档中带有各种参数的函数。
在 Delphi 表单中声明函数或过程及其代码。
function TForm1.MyFunc(s: String; i: Integer): Boolean;
begin
// required logic
end;
procedure TForm1.MyProc(s: String);
begin
// required logic
end;
为报表组件创建“onUser”函数处理程序。
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;
使用报表组件的add方法将其添加到函数列表中
(通常在 Delphi 表单的“onCreate”或“onShow”事件中)。
frxReport1.AddFunction('function MyFunc(s: String; i: Integer):Boolean');
frxReport1.AddFunction('procedure MyProc(s: String)');
添加的功能现在可以在报告脚本中使用,并且可以
由“TfrxMemoView”类型的对象引用。功能也是
显示在“数据树”功能选项卡上。在此选项卡上,功能是
分为类别和选择时有关功能的提示
出现在选项卡的底部窗格中。将上面的代码示例修改为
在单独的类别中注册功能,并显示描述性
提示:
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');
添加的功能将出现在“我的功能”类别下。到
现有类别中的注册功能使用以下之一
类别名称:
- 'ctString' 字符串函数
- 'ctDate' 日期/时间函数
- 'ctConv' 转换函数
- 'ctFormat' 格式化
- 'ctMath' 数学函数
- 'ctOther' 其他函数
如果类别名称留空,则函数位于
函数树根。要添加大量功能,它是
建议将所有逻辑放在单独的库单元中。这里
是一个例子:
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.
使用 .pas 扩展名保存文件,然后在
Delphiproject 表单的“uses”子句。您所有的自定义功能
然后将可用于任何报告组件,而无需
需要编写代码将这些函数添加到每个“TfrxReport”和
无需为每个报表组件编写额外的代码
“onUser”函数处理程序。
更新2
创建自定义函数FileExists在Delphi中声明函数例如:
function TForm1.FrFileExists(FileName : string):boolean;
begin
// required logic
Result := FileExists(FileName);
end;
使用报表组件的add方法将其添加到函数列表中(通常在Delphi表单的“onCreate”或“onShow”事件中)。
procedure TForm1.FormCreate(Sender: TObject);
begin
frxReport1.AddFunction('function FrFileExists(FileName:String):Boolean','My functions',
'This function returns True if file exists');
frxReport1.DesignReport; //<-- THIS SHOW REPORT DESIGNER RUNTIME
end;
为报表组件创建“onUser”函数处理程序。
function TForm1.frxReport1UserFunction(const MethodName: string;var Params: Variant): Variant;
begin
if MethodName = 'FrFileExists' then
Result := FrFileExists(Params[0])
end;
您不能期望在快速报告 IDE 设计时间中看到这些功能。
要在 IDE 运行时查看函数,请执行以下操作:
在uses子句中包含frxDesgn;
使用此代码显示设计器:
frxReport1.DesignReport; //see code On create above
运行项目,将看到 Fast Report Ide 和我们全新的功能FrFileExists