<< Click to display table of contents >> ReportWorkshop Overview | Scripts |
Script is a text string containing commands to execute.
Scripts are executed on report generation, when certain events occur.
The following scripts are available:
•Script_OnStart, Script_BeforeRecord, Script_AfterRecord, Script_OnEnd associated with a whole report template
•Script_OnStart, Script_BeforeRecord, Script_AfterRecord, Script_OnEnd associated with a table row generation rule.
The main purpose of scripts is calculation of values of variables.
Each script line is one command.
Empty lines and lines that start from "//" are ignored.
The script supports the commands: $if, $else, $endif, $set. Each line must include exactly one command.
The syntax of commands is the same as the syntax of commands that can be inserted as a field in text.
In scripts, the initial "$" character can be omitted, so you can write both
set %myvar = 1
and
$set %myvar = 1
Let we have the following variables:
•%count - the count of items;
•%sum_price - the sum of prices of all items.
We need to calculate an average price and assign it to %avg_price variable.
The script is:
if +%count=0
set %avg_price = 0
else
set %avg_price = +%sum_price/+%count
endif
Note 1: since all variables are text variables, we need to convert them to numbers before calculation. We do it using "+" operator. See details in the topic about expressions.
Note 2: we cannot use this code:
// wrong code
set %avg_price = If(+%count=0,0,+%sum_price/+%count)
It's because If() calculates all its parameters, and a division by zero would occur if %count = '0'.