You can save commonly-used scripts as functions in the Script Library. This allows you to easily reuse these scripts in multiple report elements and multiple reports. By saving common script tasks to the script library, you can keep your main scrips short, simple, and easy to maintain.
To understand the advantages of the Script Library, consider the script below, which computes the total of a table column:
var total = 0; for(var r = 1; r < table.length; r++) { total += table[r][col]; }
This is a common task, which you might need to perform on different table columns, on different tables, and in different reports. Rather than writing the same script again and again for each case, you should reuse the original script. For example, you can save this simple script into the Script Library as a function called “tableTotal()”.
function tableTotal() { var total = 0; for(var r = 1; r < table.length; r++) { total += table[r][col]; } return total; }
You can then execute the script by calling the “tableTotal()” function from within any element or report script. The following sections explain how to create and use JavaScript functions and the Script Library.
The Script Library is a collection of functions, which can be called from any report with access to the Library. To add a new function to the library, follow the steps below.
function tableTotal() { }
4. Fill in the function body and input parameters (if required).
function tableTotal(col) { var total = 0; for(var r = 1; r < table.length; r++) { total += table[r][col]; } return total; }
Any parameters (e.g., “col”) should be placed in a comma-separated list inside the parentheses that follow the function name. These parameters can then be used inside the function body. 5. Click the 'Save and Close' button to save the script into the library. Note in the above function that the property “table” is not qualified by an element ID (e.g., “Table1.table”). Functions are scoped dynamically (see Function Scopes); therefore, the property “table” refers to whichever Table element called the tableTotal() function. You can therefore call this tableTotal() function from within any table element to operate on that particular table element.
The Script Library is a collection of functions, which can be called from any report with access to the Library. To edit a function in the library, follow the steps below.
Function scoping is dynamic. This means that the scope in which the function executes is the scope of the function caller. For example, if a function is called from the scope of a script on the element with ID Table1, then all of the properties of element Table1 are available to the function. Properties that are not qualified with an element ID (e.g., “table,” “visible,” as opposed to “TableX.table,” “TableX.visible”) refer to the caller, Table1.
This scope rule means that if a variable is not declared within a function, the script engine will try to find it in the enclosing scopes. Exactly which object will be located depends on where the function is called. To avoid ambiguity, when you use a variable in a function, you should either declare it in the function, or pass it in as a parameter.
Copyright © 2024, InetSoft Technology Corp.