InetSoft Product Instructions: Reusing Scripts in Multiple Reports

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;
}
visual analysis report example

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.

dashboard demo
View a 2-minute demonstration of InetSoft's easy, agile, and robust BI software.

Adding a Function to 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.

  1. Select Edit → Script Library from the Report Designer menu. This opens the 'Script Library' dialog box.
  2. Click the 'New' button. This prompts you to enter the function name.
  3. Enter a name for the new function, and click 'OK'. This opens the Script Editor, and displays a function skeleton with empty parameter list, e.g.,
 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.

Editing a Function in the Script Library

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.

  1. Select Edit → Script Library from the Report Designer menu. This opens the 'Script Library' dialog box.
  2. Click the 'Edit' button. This opens the Script Editor, and displays the function script.
  3. Make the desired edits.
  4. Click the 'Save and Close' button to save the script into the library.

Function Scopes

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.