Discussion:
[Lazarus] lazreport: how to use global report variables
Graeme Geldenhuys
2015-06-30 13:03:19 UTC
Permalink
Hi,

I have a print dialog, shown before the report is generated. The user
can make some selections. Based on that, I want to set the value of a
global variable, then use that variable to decide if a column of data
must be visible or not.

I've used File -> Variable List to define a new variable (just the name).

I then double clicked on a header memo field and enabled the Script
checkbox, and typed in the following code in the Script box:

===================================
if [ HideKlas] = '0' then
Visible := True
else
Visible := False;
===================================

In my *.pas unit I temporarily tried to set a hard-coded value of the
"HideKlas" variable to see what happens in the report. I tried both the
values '0' and '1'.

===================================
If FRPrint.PrepareReport then
begin
FRPrint.Variables.Values['HideKlas'] := '1'; // for testing only
FRPrint.Preview := FAfdrukForm.PrintPanel;
FRPrint.ShowPreparedReport;
end;
===================================


No matter what I do, the header (memo) field is now _always hidden_.
Changing the hard-coded value of the variable doesn't seem to make any
difference.

Before any of the above script changes the report worked, and the header
text was visible. But I can't seem to make that visibility a dynamic option.

Any tips would be much appreciated.


Regards,
- Graeme -
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
Graeme Geldenhuys
2015-06-30 13:08:06 UTC
Permalink
Post by Graeme Geldenhuys
===================================
If FRPrint.PrepareReport then
begin
FRPrint.Variables.Values['HideKlas'] := '1'; // for testing only
FRPrint.Preview := FAfdrukForm.PrintPanel;
FRPrint.ShowPreparedReport;
end;
===================================
I've even tried removing the "HideKlas" variable completely, then do
something like this in the .pas unit.

(FRPrint.Pages.Pages[0].FindObject('memo5') as TfrMemoView).Visible :=
False;
FRPrint.Preview := FAfdrukForm.PrintPanel;
FRPrint.ShowPreparedReport;

But not even that works. In this case the memo5 is always shown, as if I
didn't have that extra line of code.


Regards,
- Graeme -
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
Graeme Geldenhuys
2015-06-30 15:35:54 UTC
Permalink
Post by Graeme Geldenhuys
I've used File -> Variable List to define a new variable (just the name).
Digging deeper into the LazReport code, I finally found the solution.

Even though you setup a variable, you must not refer to it via the
Scripts.Values[] or Variables.Values[] properties. Go figure!

Instead you use the Values.FindVariables() method.

FRPrint.Values.FindVariable('HideKlas').Field := '1';


For more details of what I've done, see the Lazarus Forum post.
http://forum.lazarus.freepascal.org/index.php/topic,28922.0.html


Now this begs the question... What the hell does Scripts.Values[] and
Variables.Values[] actually do?


Regards,
- Graeme -
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
Jesus Reyes A.
2015-07-01 20:47:36 UTC
Permalink
On Tue, 30 Jun 2015 10:35:54 -0500, Graeme Geldenhuys
Post by Graeme Geldenhuys
Post by Graeme Geldenhuys
I've used File -> Variable List to define a new variable (just the name).
Digging deeper into the LazReport code, I finally found the solution.
Even though you setup a variable, you must not refer to it via the
Scripts.Values[] or Variables.Values[] properties. Go figure!
Instead you use the Values.FindVariables() method.
FRPrint.Values.FindVariable('HideKlas').Field := '1';
Those are not global variables. They are report variables.

I think the easier way to modify global variables is through the
frVariables object
ej.

frVariables['HideKlas'] := 1;
Post by Graeme Geldenhuys
For more details of what I've done, see the Lazarus Forum post.
http://forum.lazarus.freepascal.org/index.php/topic,28922.0.html
Now this begs the question... What the hell does Scripts.Values[] and
Variables.Values[] actually do?
They do the same thing they do for any TStrings list, ie. Assign values to
supposedly existing "names" , for they to work you have to have a list
like:

name1=value1
name2=value2
etc.

But in LazReport this TStrings list functionality is not used.

Report.Variables holds the list of variables and variables categories that
you have defined in the Report (usually using the report designer gui).
Variables categories can only be used as distinctive tags for a group of
variables, and I guess that is the reason why there are a Values object
too, which btw has a list of only variables.

I was not able to find a Report.Scripts property or field, maybe you mean
Report.Script? in that case supposedly it holds the script to be run as
"BeforePrint" script.

Jesus Reyes A.

--
Graeme Geldenhuys
2015-07-01 22:52:57 UTC
Permalink
Hi Jesus,

Thanks for taking the time to reply. It is much appreciated.
Post by Jesus Reyes A.
Those are not global variables. They are report variables.
In the context of a report, they are the same thing to me. You define a
variable via the Report GUI Designer (I still don't know how to do it
from code), that variable is then available for use anywhere in your
report. ie: gloabal ;-)
Post by Jesus Reyes A.
I think the easier way to modify global variables is through the
frVariables object
ej.
frVariables['HideKlas'] := 1;
I'll take a look at that, thanks.
Post by Jesus Reyes A.
But in LazReport this TStrings list functionality is not used.
Huh? You lot me there. So if they are not used, why do those properties
still exist in TfrReport?
Post by Jesus Reyes A.
I was not able to find a Report.Scripts property or field, maybe you mean
Report.Script?
Sorry, it was a typo. I meant Report.Script.

As far as I understand, LazReport is based on some open source version
of FastReport. Due to the lack of documentation (or updated
documentation) of LazReport, I looked at the documentation for
FastReport and tried to use some if what it suggests. But it seems much
of it doesn't translate to LazReport.

Here is the URL of FastReport I looked at.

https://www.fast-report.com/documentation/ProgMan/index.html?adding_a_variable.htm

I worked through the whole "Working with a list of variables" section.


Regards,
- Graeme -
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

--
Jesus Reyes A.
2015-07-02 03:36:21 UTC
Permalink
En Wed, 01 Jul 2015 17:52:57 -0500, Graeme Geldenhuys
Post by Graeme Geldenhuys
Post by Jesus Reyes A.
Those are not global variables. They are report variables.
In the context of a report, they are the same thing to me. You define a
variable via the Report GUI Designer (I still don't know how to do it
from code), that variable is then available for use anywhere in your
report. ie: gloabal ;-)
:)
Yes, you can consider them as global if you so desire. However they exists
only in your report, where they were defined. The frVariables list however
can be used by n-number of reports in your application. This is why I
think of them as "global"
Post by Graeme Geldenhuys
Post by Jesus Reyes A.
I think the easier way to modify global variables is through the
frVariables object
ej.
frVariables['HideKlas'] := 1;
I'll take a look at that, thanks.
It seems possible to have report and global variables named the same,
however I have not investigated who takes precedence, I would guess report
variables are found first, but I'm not sure TBH.
Post by Graeme Geldenhuys
Post by Jesus Reyes A.
But in LazReport this TStrings list functionality is not used.
Huh? You lot me there. So if they are not used, why do those properties
still exist in TfrReport?
They are not specific to LazReport, this is basic functionality of every
TStringList, just because they descend from TStrings. Values and Names are
public properties of TStrings, how can one hide those properties in a
instance of TStringList?, you can't, so LazReport also can't. :D
Post by Graeme Geldenhuys
Post by Jesus Reyes A.
I was not able to find a Report.Scripts property or field, maybe you mean
Report.Script?
Sorry, it was a typo. I meant Report.Script.
As far as I understand, LazReport is based on some open source version
of FastReport. Due to the lack of documentation (or updated
documentation) of LazReport, I looked at the documentation for
FastReport and tried to use some if what it suggests. But it seems much
of it doesn't translate to LazReport.
Yes, probably, however one could find that they have also many things on
common.
Post by Graeme Geldenhuys
Here is the URL of FastReport I looked at.
https://www.fast-report.com/documentation/ProgMan/index.html?adding_a_variable.htm
I worked through the whole "Working with a list of variables" section.
frxReport1.Variables['My Variable 1'] := 10; and likewise
Variable := frxReport1.Variables.Add and
Variable := frxReport1.Variables.Insert(1);

Wouldn't work on LazReport as here frxReport1.Variables is just a mere
mortal TStringList which already have a default property: Strings[]
indexed by integer, This part is in LazReport a little mess :), I think
FastReport guys noted this and they fixed the situation in FastReport.
Post by Graeme Geldenhuys
Regards,
- Graeme -
--
Usando el cliente de correo de Opera: http://www.opera.com/mail/

--
Loading...