In the previous blogs in this series, we created a simple analytic application and added features such as popups and dynamic visibility. We also leveraged scripting, albeit on a very limited basis. Now let us explore scripting capabilities a bit more in detail.
The Need for Scripting
Scripting capabilities enable IT teams deliver a lot of custom capabilities to end users. While stories can deliver content with standardized interaction & navigation paths, analytic applications need to deliver customized user experiences, interactions and navigation paths. This is primarily enabled through scripting.
For example, look at the visual below that outlines how scripting can be used to customize measures that are displayed on charts & tables. This can be accomplished only in applications, and not in stories.

Stories vs. Applications – Deliver custom scenarios via. Scripting (Source: SAP)
The above is only the starting point for scripting. SAP Analytics Cloud will soon deliver advanced scripting capabilities to perform more complex operations such as inheriting widgets and accessing predictive frameworks, all of which can deliver custom experiences to users.
Basic Scripting
The scripting capabilities delivered by SAP Analytics Cloud is very similar to that of SAP Lumira Designer.
- It leverages typescript, a subset of JavaScript, for coding
- Content Assistance can be enabled using the keyboard combination Ctrl + Space which shows all the available commands (see image below)
- Syntax check & commenting features are available
- You can glance several scripts at once (an improvement from SAP Lumira Designer)

The scripting interface
Here are some of the commonly used script functions by multiple widgets.
- setVisible() – shows or hides a widget. A single Boolean parameter is used. For example, SetVisible(true); will set the visibility of table to true.
- isVisible() – returns whether a widget is visible or not. The returned value is Boolean.
- addDimension() – adds the specified dimension to the chart. It has three parameters dimension name, feed and position (optional). For example, addDimension(“State”, Feed.CategoryAxis); adds State values to the Category axis of the chart.
- addMeasure() – adds the specified measure to the chart. The syntax is similar to that of addDimension(). Both addDimension() and addMeasure() functions are only available for chart widgets.
- addDimensionToRows() – adds the specified dimension to the table row. It has two parameters – dimension name and position.
- addDimensionToColumns() – adds the specified dimension to the table column. It has two parameters – dimension name and position.
- removeDimension() & removeMeasure() – removes the specified dimension or measure from the chart.
- getDataSource() – returns the data source used in the widget.
- getSelection() – returns the selection made in chart or table.

Some basic scripting functions
Commenting
To comment a single line of code we can include “//” at the beginning of the line. To comment multiple lines, we can enclose the code between “/*” and “*/”.

Commenting in scripts
Console Function
Console Function can be used to log a string in the console window of the browser’s developer tools. This will be useful when we try to debug the application.

Logging your messages

The console log
Control Statements
Control statements determine whether specific segments of code need to be executed. SAP Analytics Cloud supports the following control statements as part of analytic applications.
1. If Else Statement
The syntax for the if else statement is as follows:
if (condition) {
Script to be executed if the condition is true
} else if (condition) {
Script to be executed if the condition is true
} else {
Script to be executed if all conditions fail
}
To have multiple conditions inside if statement we can use “||” for OR and “&&” for AND. See the example below that displays the appropriate chart based on the region.

The If..Else statement
2. Loop Functions
Unlike SAP Lumira Designer which only supports For loop, analytic application of SAP Analytics Cloud supports For, While and Do While loops.
2.1. For Loop
For loop can be used to access and manipulate an array. In the following example, the list of members from a data source is added to a dropdown.

The For Loop function
2.2. While Loop
While loop can be used instead of a for loop in the example mentioned earlier.

The While Loop
2.3. Do While Loop
The code snippet above can also be written using the do…while loop.

The Do…While loop
2.4. Break and Continue Statements
In all the loop functions you can break the loop using a break statement and skip an element using continue statement. Here we have used the continue statement to skip any NULL values and break statement to come out of the loop when a member of the array has the string “_Closed”.

Break & Continue Functions
3. Switch Function
The Switch statement can be used to run a specific block of code based on the condition. Here we are displaying specific charts based on appropriate region selected in the dropdown. In addition, there is also a ‘default’ behavior that executes when none of the conditions are met.

The Switch..Case Function
* * *
In the subsequent blog, we will discuss string, math, date, array and conversion functions available in Analytic Applications.
Reach out to us here today if you are interested to evaluate if SAP Analytics Cloud is right for you.
Subscribe to our Newsletter
The post SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting appeared first on Visual BI Solutions.