Siemens SIMATIC User Manual

SIMATIC WinCC Unified
Tips and Tricks for Scripting (JavaScript)

SIMATIC WinCC Unified (Engineering), SIMATIC WinCC Unified (Runtime) SIMATIC HMI Unified Comfort Panels
https://support.industry.siemens.com/cs/ww/en/view/109758536
Siemens Industry Online Support

Legal information

Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
2
© Siemens AG
2020 All rights reserved
Legal information
Use of application examples
Application examples illustrate the solution of automation tasks through an interaction of several components in the form of text, graphics and/or software modules. The application examples are a free service by Siemens AG and/or a subsidiary of Siemens AG (“Siemens”). They are non­binding and make no claim to completeness or functionality regarding configuration and equipment. The application examples merely offer help with typical tasks; they do not constitute customer-specific solutions. You yourself are responsible for the proper and safe operation of the products in accordance with applicable regulations and must also check the function of the respective application example and customize it for your system. Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the application examples used by technically trained personnel. Any change to the application examples is your responsibility. Sharing the application examples with third parties or copying the application examples or excerpts thereof is permitted only in combination with your own products. The application examples are not required to undergo the customary tests and quality inspections of a chargeable product; they may have functional and performance defects as well as errors. It is your responsibility to use them in such a manner that any malfunctions that may occur do not result in property damage or injury to persons.
Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without limitation, liability for the usability, availability, completeness and freedom from defects of the application examples as well as for related information, configuration and performance data and any damage caused thereby. This shall not apply in cases of mandatory liability, for example under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for damages arising from a breach of material contractual obligations shall however be limited to the foreseeable damage typical of the type of agreement, unless liability arises from intent or gross negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens against existing or future claims of third parties in this connection except where Siemens is mandatorily liable. By using the application examples you acknowledge that Siemens cannot be held liable for any damage beyond the liability provisions described.
Other information
Siemens reserves the right to make changes to the application examples at any time without notice. In case of discrepancies between the suggestions in the application examples and other Siemens publications such as catalogs, the content of the other documentation shall have precedence. The Siemens terms of use (https://support.industry.siemens.com) shall also apply.
Security information
Siemens provides products and solutions with industrial security functions that support the secure operation of plants, systems, machines and networks. In order to protect plants, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept. Siemens’ products and solutions constitute one element of such a concept. Customers are responsible for preventing unauthorized access to their plants, systems, machines and networks. Such systems, machines and components should only be connected to an enterprise network or the Internet if and to the extent such a connection is necessary and only when appropriate security measures (e.g. firewalls and/or network segmentation) are in place. For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure. Siemens strongly recommends that product updates are applied as soon as they are available and that the latest product versions are used. Use of product versions that are no longer supported, and failure to apply the latest updates may increase customer’s exposure to cyber threats. To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed at: https://www.siemens.com/industrialsecurity.
Table of contents
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
3
© Siemens AG
2020 All rights reserved
Table of contents
Legal information ..................................................................................................... 2
1 Introduction .................................................................................................... 5
1.1 Overview ........................................................................................... 5
1.2 Components used ............................................................................. 5
2 General............................................................................................................ 6
2.1 System function or script (a decision-making aid) ............................... 6
2.2 Trigger types ..................................................................................... 6
2.3 Access properties of a screen object .................................................. 8
2.4 Difference between synchronous and asynchronous script calls......... 8
2.5 Script threads .................................................................................. 10
3 Configuring scripts ...................................................................................... 11
3.1 Configuring local scripts ................................................................... 11
3.1.1 Dynamizing object properties via scripts .......................................... 12
3.1.2 Calling up scripts via events ............................................................. 13
3.1.3 Calling scripts via Scheduled tasks .................................................. 13
3.2 Configuring global script modules .................................................... 14
3.2.1 Add global module ........................................................................... 15
3.2.2 Creating a Global Definition Area ..................................................... 16
3.2.3 Edit global functions......................................................................... 17
3.2.4 Import and use content from global modules .................................... 19
4 Tips and tricks for creating scripts (JavaScript in general) ....................... 24
4.1 Strings in JavaScript ........................................................................ 24
4.1.1 Linking strings by script.................................................................... 24
4.1.2 Adding spaces to linked strings ........................................................ 24
4.1.3 Determining the length of a string .................................................... 25
4.1.4 Finding a sub-section of a string ...................................................... 25
4.1.5 Turning a string into an array ........................................................... 25
4.2 Arrays in JavaScript ......................................................................... 26
4.2.1 Creating arrays and accessing array elements ................................. 26
4.2.2 Extending and truncating arrays ....................................................... 27
4.2.3 Sorting arrays .................................................................................. 27
4.2.4 Turn arrays into strings .................................................................... 28
4.3 Math object in JavaScript ................................................................ . 28
4.3.1 Add constants .................................................................................. 29
4.3.2 Round off tag values ........................................................................ 29
4.3.3 Find square root .............................................................................. 29
4.3.4 Use exponent function ..................................................................... 29
4.3.5 Generate random number ................................................................ 30
4.3.6 Find minimum/maximum values ....................................................... 30
5 Tips and tricks for scripting (WinCC Unified specific)............................... 31
5.1 Script snippets ................................................................................. 31
5.2 Description of the "HMI Runtime" snippets ....................................... 32
5.3 Performance-optimized configuration ............................................... 34
5.3.1 Prefer system dialogs ...................................................................... 34
5.3.2 Read multiple tags with TagSet........................................................ 34
5.3.3 Avoid cyclic scripts .......................................................................... 35
5.3.4 Establish database connections once .............................................. 35
5.4 Screens and screen objects ............................................................. 36
5.4.1 Finding objects in screen windows with object paths ........................ 36
5.4.2 Screen change across multiple screen windows .............................. 39
5.4.3 Displaying screens as pop-ups ........................................................ 40
Table of contents
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
4
© Siemens AG
2020 All rights reserved
5.4.4 Determining the screen name .......................................................... 43
5.4.5 Change colors ................................................................................. 43
5.4.6 Counting screen objects and finding screen object names ............... 44
5.4.7 Read out touch area direction .......................................................... 44
5.5 Interconnect faceplate via script ....................................................... 47
5.5.1 Open faceplate as a pop-up ............................................................. 48
5.5.2 Modifying faceplate interconnection in the screen ............................ 51
5.5.3 Opening a faceplate from a faceplate ............................................... 53
5.5.4 Closing a faceplate ................................................................ .......... 53
5.6 Tags and UDTs ............................................................................... 58
5.6.1 Access to HMI UDT elements .......................................................... 58
5.6.2 Loop breakers ................................................................................. 58
5.6.3 Using client-internal tags via data set ............................................... 59
5.7 Starting programs from the runtime .................................................. 61
5.7.1 StartProgram in the Unified PC runtime ........................................... 61
5.7.2 StartProgram in the Unified Comfort Panel ....................................... 62
5.8 File handling ................................ .................................................... 63
5.8.1 Create folder.................................................................................... 63
5.8.2 Write values to a file and create file .................................................. 64
5.8.3 Read values from a file .................................................................... 65
5.9 Configuring time delays in a script.................................................... 67
5.10 Configuring access to databases ..................................................... 69
5.11 Configuring access to internet resources ......................................... 69
5.12 Filtering alarms and messages ........................................................ 70
5.13 Switching runtime language ............................................................. 71
5.14 "Math" object ................................................................................... 72
5.15 Configuring date and time ................................................................ 72
5.15.1 Working with local date/time ............................................................ 72
5.15.2 Editing user-defined date/time.......................................................... 73
5.15.3 Working with time stamps on a nanosecond basis ........................... 73
5.16 Script diagnostics ............................................................................ 75
5.16.1 "Alert()" notification window .............................................................. 75
5.16.2 Diagnostics via RTIL TraceViewer ................................................... 75
5.16.3 Debugging scripts in Chrome ........................................................... 77
5.16.4 Plan for responses in case of error................................................... 77
6 Useful information ................................................................ ........................ 78
7 Appendix ....................................................................................................... 79
7.1 Service and support ......................................................................... 79
7.2 Links and literature .......................................................................... 80
7.3 Change documentation .................................................................... 80

1 Introduction

Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
5
© Siemens AG
2020 All rights reserved
1 Introduction

1.1 Overview

SIMATIC WinCC Unified uses JavaScript as a script language and therefore provides a modern script environment, which you can typically use to automate screens and objects.
The script environment maps individual elements of the system components via an object model, e.g. screen of the graphic runtime system. You reference this object model in your script languages, this allowing you to access different functions in an object-oriented approach.
The application example will show you how to use scripts in SIMATIC WinCC Unified. Selected examples will also serve to show you tips and tricks for manual scripting, which you can use in your application.
Figure 1-1

1.2 Components used

The following hardware and software components were used to create this application example:
Table 1-1
Components
Quantity
Item number
Comment
SIMATIC WinCC Unified V16 (Engineering)
1
6AV2153-....1-6...
-
SIMATIC WinCC Unified V16 (Runtime)
1
6AV2154-....1-6...
-
SIMATIC HMI Unified Comfort Panel MTP700
1
6AV2128-3GB36-0AX0
Alternatively, you can use any other SIMATIC HMI Unified Comfort Panel.

2 General

Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
6
© Siemens AG
2020 All rights reserved
2 General
This chapter will familiarize you with general information surrounding the topic of scripting in WinCC Unified.

2.1 System function or script (a decision-making aid)

The chart below is a decision-making tool to help you determine when to use a system function, a global module or local scripts.
Figure 2-1
Realizable in
AS?
Create local script
END
System function
available?
Recurring
functionality
needed
Yes
Yes
Yes
No
No
No
Create global module
Use system function
Modify PLC program

2.2 Trigger types

General information
There are various triggers to run a script in the runtime. Triggers are conditions. There are three different types of trigger in WinCC Unified:
Cyclic triggers
Tag trigger
Event-driven triggers
If no trigger is defined (e.g. in the task scheduler), the script is not run.
Cyclic triggers
Cyclical triggers are time-driven and are run repeatedly after a certain time, for example every 10 seconds.
Figure 2-2
2 General
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
7
© Siemens AG
2020 All rights reserved
Note
Please note that the cycle time heavily influences the performance of the project. All actions from a screen must be completed within their cycle time. Apart from
the runtimes of the actions, the times required for requesting the tag values and the reaction times of the automation systems must also be taken into consideration. You should only set trigger events with a cycle time of less than one second when rapidly changing variables must be queried.
Tag trigger
For a tag trigger, one or more tags must be specified, also known as the trigger tag.
Once the value of the trigger tag changes, the script is triggered and the function inside it is executed.
Figure 2-3
Event-driven triggers
For event-driven triggers, the script is always run when this event occurs. Events can be, for example, mouse clicks, keyboard operations or changes in focus.
Figure 2-4
2 General
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
8
© Siemens AG
2020 All rights reserved

2.3 Access properties of a screen object

Using JavaScript, you can address each screen object and modify its properties.
Example
This example changes the background color of the object rectangle with the object name "Rectangle1" to yellow.
Screen.FindItem('Rectangle1').BackColor = 0xFFFFFF00;
Find name of the property
You can find the name of the property by right-clicking on the property (1) and then clicking "Copy property name" (2).
Figure 2-5
You can then paste the property name into the desired location with the keyboard command "CTRL + V".
Note
Alternatively, you can find the properties listed in the manual "SIMATIC WinCC Engineering V16 - Runtime Unified" in the "Objects" chapter under the respective object:
https://support.industry.siemens.com/cs/ww/en/view/109773780/118272265099

2.4 Difference between synchronous and asynchronous script calls

Synchronous/asynchronous script call is a distinction that applied for JavaScript in general.
Synchronous
In synchronous script calls, the functions in the script are executed in order. The next function only begins when the one before it is complete.
2 General
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
9
© Siemens AG
2020 All rights reserved
Figure 2-6, simplified representation of synchronous script sequence
function A
function B
function C
function D
Asynchronous
In contrast to the above, there are also asynchronous script calls. In this case, functions can be executed in parallel, thus allowing them to be processed more rapidly.
Figure 2-7, simplified representation of asynchronous script sequence
function A
function B function C
function D
Typically, asynchronous script calls are used in the context of timers (e.g. "HMIRuntime.Timers.SetTimeout()"), access to network files, or time-intensive database queries.
When using asynchronous script calls, there are however other differences which affect how the script runs. For example, you can use the "await" operator to wait for the result of a function.
If you use multiple complex and therefore more time-intensive functions in a script (for example if you want to read two network files and establish a database connection), then it is recommended to use the Promise.all method.
Note
You can find more information about "await" and the Promise object in the "Mozilla Developer Network".
https://developer.mozilla.org/en­US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
2 General
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
10
© Siemens AG
2020 All rights reserved
Note
You can find additional information about "Promise.all" in the "Mozilla Developer Network":
https://developer.mozilla.org/en­US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

2.5 Script threads

SIMATIC WinCC Unified processes the scripts in Node.js processes. In this context, a Node.js process is always single-threaded. This means that only one CPU core is available to run the code to process the script, meaning that only one script per process can be handled at a time.
In WinCC Unified itself, only two processes are available for script handling:
one process for all scripts which are run by the user (all scripts in screens)
and one process for all scripts which are running in the "Task Scheduler".
Both processes run on two separate CPU cores, thereby can working through scripts in parallel.

3 Configuring scripts

Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
11
© Siemens AG
2020 All rights reserved
3 Configuring scripts

3.1 Configuring local scripts

Supported objects
You can configure scripts to the following points of use in SIMATIC WinCC Unified:
Screens
Screen objects
Tasks
Depending on the object for which you configure the scripts, you can execute different functions.
Table 3-1
Execution context
of the editor
Script context and referencing
"Scripts" Editor in the "Screens" Editor
Each process image has two independent scrip contexts:
Context for dynamizing properties (chapter 3.1.1)
Context for evaluating events (chapter 3.1.2)
Both script contexts of a process image reference the same global modules.
However, each context receives its own copy of the tags defined there.
"Scripts" Editor in the task planner
All tasks are assigned to a script context. Different jobs can access common global tags.
All tasks reference all global modules of a target system (chapter
3.1.3).
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
12
© Siemens AG
2020 All rights reserved

3.1.1 Dynamizing object properties via scripts

You can dynamize the relevant properties of screen and screen objects via script, i.e. change the property for the runtime, e.g. change font color, show/hide visibility.
The following steps are required for dynamization by script:
1. Highlight the screen object on the screen.
2. Open the "Properties > Properties" tab of the screen object.
3. Change the property to be dynamized in the "Dynamization" column to "Script".
The Script Editor then opens in the Inspector window next to the object properties.
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
13
© Siemens AG
2020 All rights reserved

3.1.2 Calling up scripts via events

To execute certain functions in operation, e.g. invert a tag with a button or toggle the language, SIMATIC WinCC Unified provides the option of calling up scripts via events.
The following configuration steps are necessary to call up a script via an event at a screen object (e.g. a button):
1. Highlight the screen object on the screen.
2. Open the "Properties > Events" tab of the screen object.
3. Select the event that calls the script in the local navigation.
4. Now create a new script with the "Convert to script" button.

3.1.3 Calling scripts via Scheduled tasks

In addition to the "Events" at screen objects, you can also call up scripts in the "Scheduled tasks".
The following steps are required for this:
1. In the project tree, open the "Scheduled tasks".
2. Add a new task or highlight the existing task.
3. Open "Properties > Events".
4. Now create a new script with the "Convert to script" button.
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
14
© Siemens AG
2020 All rights reserved

3.2 Configuring global script modules

With the version SIMATIC WinCC Unified V16.0 you have the possibility to use global modules in the script context. These are appropriate for:
Creating functions that are not directly linked to an image object, an image, or
a task and can therefore be used more than once.
Triggering scripts cyclically in the background (e.g. task scheduler).
Term overview
Table 3-2
Designation
Description
Image
Global module
Global modules are stored in the project tree under "Scripts".
Each global module contains its own definition area and one or more functions.
Global modules are very well suited for grouping functions.
Global definition area
In the definition area of a global module, you define local tags that you can access in all functions of the global module.
You can also use these tags in local scripts (such as image properties) using the Export/Import command.
Global function
Each global module can contain several functions.
You can define transfer parameters in a function, which you can then process in the script.
Each function has a return value.
You can also use the Import
command to use functions in local scripts (e.g. image properties).
Configuration
If you want to use global modules, the general configuration procedure is as follows:
1. Add a global module (chapter 3.2.1).
2. Create global definition area (chapter 3.2.2).
3. Edit global function (chapter 3.2.3).
4. Import and use content from global modules (chapter 3.2.4).
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
15
© Siemens AG
2020 All rights reserved

3.2.1 Add global module

To add a global module:
1. Open the "Scripts" folder in the project tree.
2. Click on "Add new global module".
A new folder "Global module" appears in the folder "Scripts". This already contains a "Global definition area".
3. If necessary, rename the folder "Global modules", e.g. to "MyGlobalModules".
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
16
© Siemens AG
2020 All rights reserved

3.2.2 Creating a Global Definition Area

The definition area of a global module is used to access the same tag values in several functions of the same global module.
Configuration
1. Open the "Global definition area" folder by double-clicking on it.
2. Add the code below to the working area.
export let myValue1 = 35;
Note
The value of tags within a global module can only be changed via its functions. Other places of use (e.g. image properties) can only read these tags.
If you want to change the value of tags of a global module (here: "myValue1") from a local usage location, you have to call a script of the same global module (here: "SetValue") and enter the desired value via a parameter (here: "newValue").
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
17
© Siemens AG
2020 All rights reserved

3.2.3 Edit global functions

You edit global functions like the global definition area within a global module.
Example
In this example, three values (a value from the global definition area and two transfer parameters) are to be added together in a function. The return value of the function should be the sum ("result") of the three values.
Configuration
1. Click on "Add new function" in the project tree in your Global Module "MyGlobalModules".
2. If necessary, change the name of the function (1) and the number of transfer parameters ("Parameters") in the properties (2).
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
18
© Siemens AG
2020 All rights reserved
3. Enter the following code in the function:
let result;
result = myValue1 + parameter1 + parameter2;
return result;
Note
The tag "myValue1" has been defined in the global definition area (see chapter 3.2.2).
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
19
© Siemens AG
2020 All rights reserved

3.2.4 Import and use content from global modules

To be able to use the tags and functions of the global modules locally, you must first declare them with the import command in the global definition of the local script.
Requirement
In order to describe the import of the global module contents, the following elements were planned in advance:
an image ("Screen_Script")
a button "Button_1" and an I/O field in the image ("Screen_Script")
an internal tag "ScriptResult" of the data type "Int".
the I/O field is connected with the process tag "ScriptResult".
Example 1
In this example, the value of the tag "myValue1" (as defined in chapter 3.2.2), must be written from the global definition area of the global module "MyGlobalModul" into the internal tag "ScriptResult" when the button "Button_1" is pressed.
The value of the internal tag must then be output to the I/O field.
Configuration 1
1. Creating a script
Mark the button (1) in the "Screen_Script" image. – In the properties, open the "Events" tab (2). – In the navigation area, select the trigger "Click left mouse button" (3) and
then click the button "Convert to script" (4).
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
20
© Siemens AG
2020 All rights reserved
2. In the local script editor menu, click Global definition.
3. Import content from global definition area: – Insert the following code to import the tag "myValue1" from the global
definition area of the global module (1).
import {myValue1} from 'MyGlobalModules';
Then click on "Script function" in the menu of the local script editor (2).
Note
Once you have imported the tag in the global definition area (for example, in the image properties), it is available in the entire image properties area of the same image.
To use the tag in the Events pane of the image, you must re-import the tag. You therefore have to declare the tag in the global definition area of the events.
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
21
© Siemens AG
2020 All rights reserved
4. Insert the following code in the script editor of the event:
let tag1 = Tags('ScriptResult');
tag1.Write(myValue1); //write value 'myValue1' to tag
'ScriptResult' shown in IO field
Note
Optionally, you can also use the script snippet "HMI Runtime > Tag > Write tag" to generate the code in the script editor.
Further information can be found in chapter 5.1.
5. Save your project.
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
22
© Siemens AG
2020 All rights reserved
Example 2
In the second example, the function "Function" of the global module "MyGlobalModul" is to be executed by pressing the button "Button_1".
The values 10 and 12 are to be passed to the function as transfer parameters.
The return value of the function is then to be output as a trace message.
Configuration 2
1. Open the global definition area of the button "Button_1", as in step 1-3 with "Configuration 1" (page 19).
2. Import function from global module: – To additionally import the "Function" function, add the following code (2):
import {Function} from 'MyGlobalModules';
Then click on "Script function" in the menu of the local script editor (2).
Note
If you do not want to pass values to a function of a global module, call the function "Function" with "()" in the workspace of the script editor.
Note
You can also import tags from the global definition area and functions from the same global module in a single command:
import {myValue1; Function} from 'MyGlobalModules';
3 Configuring scripts
Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
23
© Siemens AG
2020 All rights reserved
3. Transfer values – Define three additional tags in the local script of the button event as shown
in the figure (1).
let myValueLocal1 = 10;
let myValueLocal2 = 12;
let resultFunction;
Assign the return value of the "Function" function to the "resultFunction"
tag and pass the two tags ("myValueLocal1" and "myValueLocal2") (2).
resultFunction = Function(myValueLocal1, myValueLocal2);
Output the value of the tag "resultFunction" as a trace message (3).
HMIRuntime.Trace(resultFunction);
Note
Optionally, you can also use the script snippet "Trace" to generate the code for the trace message.
Further information to script snippets can be found in chapter 5.1.
Note
If you have a large number of tags in the global definition area or functions in global modules, you can import them collectively with the following line of code:
import * as myGMs from 'MyGlobalModules';
You can call or use functions or tags from the global definition area of global modules as follows:
myGMs.Function(); //call function from global modul myGMs.myValue1; /*call tag from global definition area in
*/global module

4 Tips and tricks for creating scripts (JavaScript in general)

Tips and Tricks JavaScript Article ID: 109758536, V3.0, 09/2020
24
© Siemens AG
2020 All rights reserved
4 Tips and tricks for creating scripts
(JavaScript in general)

4.1 Strings in JavaScript

In the following sections (chapters 4.1.1-4.1.3), selected examples will show you how to process strings.
Note
Further information on working with strings and JavaScript is available at the following link:
https://developer.mozilla.org/en­US/docs/Web/JavaScript/Reference/Global_Objects/String

4.1.1 Linking strings by script

If you wish to link several strings in a script, this is possible with the concatenation operator "+".
Example:
let Tag_01;
const Tag_Text_01 = 'Hello';
const Tag_Text_02 = 'my';
const Tag_Text_03 = 'world';
Tag_01 = Tag_Text_01 + Tag_Text_02 + Tag_Text_03;
// output Tag_01: 'Hellomyworld'
Note
The Object.prototype.toString() function allows corresponding expressions to be converted to the "String" data type. This conversion before linking the strings allows you to avoid runtime errors.
You can find further information on the "Object.prototype.toString()" method at the following link:
https://developer.mozilla.org/en­US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

4.1.2 Adding spaces to linked strings

If you link strings together, it may occur that they are output as one coherent word. This can consequently make the meaning and readability more difficult.
To separate the individual strings from each other, you can add additional separators (e.g. spaces). These are added to the string in quotation marks (" ") and via concatenation operator (+).
Example
let Tag_01, Tag_Text_01, Tag_Text_02, Tag_Text_03;
Tag_01 = Tag_Text_01 + " " + Tag_Text_02 + " " + Tag_Text_03;
Loading...
+ 56 hidden pages