How to use own function?

Question: How to use own function?

Answer:

Use TfrReport.OnUserFunction event. Here is simple example:

1
2
3
4
5
procedure TForm1.frReport1UserFunction(const Name: String; p1, p2, p3: Variant; var val: Variant);
begin
ifAnsiCompareText('SUMTOSTR', Name) = 0then
val := My_Convertion_Routine(frParser.Calc(p1));
end;

 



After this, you can use SumToStr function in any place of report (in any expression or script). 

(ok, but it works only for one TfrReport component. I want to use my function everywhere (in all TfrReport components).

Make OnUserFunction event handler common for all components. If you can't do this, you should create function library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type
TMyFunctionLibrary = class(TfrFunctionLibrary)
public
constructor Create; override;
procedure DoFunction(FNo: Integer; p1, p2, p3: Variant; var val: Variant); override;
end;
 
constructor MyFunctionLibrary.Create;
begin
inherited Create;
with List do
begin
Add('DATETOSTR');
Add('SUMTOSTR');
end;
end;
 
procedure TMyFunctionLibrary.DoFunction(FNo: Integer; p1, p2, p3: Variant; var val: Variant);
begin
val := 0;
case FNo of
0: val := My_DateConvertion_Routine(frParser.Calc(p1));
1: val := My_SumConvertion_Routine(frParser.Calc(p1));
end;
end;
 
To register function library, call
frRegisterFunctionLibrary(TMyFunctionLibrary);
To unregister library, call
frUnRegisterFunctionLibrary(TMyFunctionLibrary);

 

(how I can add my function to function list (in expression builder)?

1
2
3
Use frAddFunctionDesc procedure (FR_Class unit):
 
frAddFunctionDesc('SUMTOSTR', 'My functions','SUMTOSTR()/Converts number to its verbal presentation.');

 

Note: "/" symbol is required! It separates function syntax from its description. 
FuncLib is reference to your function library (can be nil if you don't use the function library). When function library is unregistered, all its function will be automatically removed from the function list.