Working in the SAP Analytics Cloud Script Editor

Objective

After completing this lesson, you will be able to Access and perform basic tasks in the Script Editor in Advanced Mode.

Introduction to the Script Editor

Scripting Language

SAP Analytics Cloud uses a subset of JavaScript for enhancing stories using scripting.

Why is JavaScript used?

  • It can be executed on browser and server
  • It’s relatively easy to learn
  • It’s sufficiently powerful

Why does SAP Analytics Cloud use a subset of JavaScript?

  • Full JavaScript is quite complex
  • JavaScript does not offer type support
  • Some constructs in JavaScript can be dangerous (for example endless loops)
  • SAP Analytics Cloud can validate all scripts

How does scripting in SAP Analytics Cloud differ from using JavaScript in general?

  • Script must be strongly typed
  • Automatic type casting is not offered

Due to the rich code completion capabilities available in SAP Analytics Cloud, the JavaScript subset must be strongly typed. If you want to use an integer as a string, you must explicitly cast it. What this means is, once you have a duck, it remains a duck. A consequence of strong typing is that you can't expect automatic conversions. With strong typing, you can't recycle variable names as new types. So, if you want something else, you need another variable.

In general, using JavaScript offers weak typing and dynamic typing:

  • With weak typing, the script writer can implicitly coerce variables to act like different types. For example, you can have an integer value and treat it as if it's a string: "Dear integer, you are a string". For more information on weak typing, you can go to Code Basics
  • With dynamic typing, the runtime tries to guess the type from the context at that moment. You can even change the type after the variable is already in use. For example, you can change the value of the previously mentioned integer to another type of object: "Dear integer, you're now a duck".

Even though SAP Analytics Cloud uses as subset of JavaScript as its scripting language, it is still JavaScript. You can write valid JavaScript while treating the language as if it's strongly and statically typed.

Knowing that some more advanced JavaScript features are disabled in SAP Analytics Cloud, being a true JavaScript subset still allows for native execution in a browser. Scripts used in SAP Analytics Cloud’s subset of JavaScript are either tied to events or global script objects and are run and validated against "strict mode".

For more information on strict mode, you can go to: Strict mode - JavaScript

Data Types in SAP Analytics Cloud

All programming languages have the concept of Data Types.

In SAP Analytics Cloud, we have:

  1. Primitive data types (like in JavaScript)
    • Integer: 2, 8, 6+4
    • String: "Hello", "I’m" + "here"
    • Boolean: true, false, true && false, "a" == "a", 1 !=2
  2. BI-specific data types:
    • Widget
    • Dimension
    • Member
    • Filter, etc.

There is no automatic type casting.

The Script Editor

The SAP Analytics Cloud Script Editor lets you write scripts for each widget, creating interactive and highly custom-defined stories.

Scripting offers several options for writing code to enhance a story in SAP Analytics Cloud. For example, you can:

  • Place a Button widget in the story and assign a script to the button’s onClick event,
  • Assign script to a chart using its onSelect event,
  • Write scripts based on other system events such as the onInitialization event of the story, or
  • Add scripts that execute whenever data is changed using onResultChanged.

The code completion and value help functionality in SAP Analytics Cloud allows for:

  • Code completion in the traditional sense like completing local or global identifiers
  • Semantic code completion by suggesting member functions or similar
  • Value help in the form of context-aware value proposals like measures of a data source for function parameters

For example, when calling an API method on an SAP Business Warehouse query, the code completion can propose measures as code completion options or values to specify a filter.

To open code completion in the script editor, use the keyboard shortcut Crtl + Space.

Watch this short video to find out more about the script editor, automatic syntax check, code completion, and value help.

Comment Types

You can enhance your script with comments. Comments help you or others to create documentation about the script or single parts of the script. JavaScript syntax offers the following two kinds of comments:

  1. Single-line comments
  2. Multi line comments

Single-line comments start with//.

Any text between // and the end of the code line is interpreted as a comment and isn’t executed as a command. You can either use it to generate a whole line as a comment but you can also add it to an existing line of code.

For example:

var counter = 1; // declare counter and assign value 1

or

var counter = 1;

// declare counter and assign value 1

Multi line comments start with/*and end with*/.

Text between /* and */ is interpreted as a comment and isn't executed as a command.

For example:

var counter = 1;

/* declare counter and

assign value 1 */

Statement Types

Call StatementsConditional Execution StatementsLoopsAssignment Statements
For example, change the Chart Type of a chart or set a filter for a dimensionIF xxx THEN xxx ELSEFOR…Define a new variable or assign a value to an existing one.
Basic syntax: <Widget>.<Method>(<Arguments>);SWITCH CASEWHILE…var <variable> = <expression>;
For/IN...or

<variable> = <expression>;

SAP Analytics Cloud can use delivered APIs and common JavaScript options for different statement types. These statement types are covered in more detail in the following lessons of this course.

Log in to track your progress & complete quizzes