博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[翻译] Using Custom Functions in a Report 在报表中使用自己义函数
阅读量:5270 次
发布时间:2019-06-14

本文共 4651 字,大约阅读时间需要 15 分钟。

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”组件编写代码或事件来添加这些自定义函数。

转载于:https://www.cnblogs.com/moon25/p/5531634.html

你可能感兴趣的文章
项目管理之路(1):初步踏入项目管理
查看>>
Java 中 静态方法与非静态方法的区别
查看>>
echarts饼图显示百分比
查看>>
JMS消息
查看>>
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
php上传文件及头像预览
查看>>
大四java实习生的一些经历
查看>>
线程池的概念
查看>>
Oracle_Statspack性能诊断工具
查看>>
转获取sql维护的表关系
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>