Skip to main content
Skip table of contents

CalcVar: How to use a Javascript expression to set a variable

Tag: CalcVar

The CalcVar function lets users create a script variable that contains the result of a JavaScript (JS) expression.

Detailed Description

All CalcVar must comply to the JS language. All formulas are evaluated as Javascript, even very simple ones like: <CalcVar num1="665 + 1"/>

Single line formulas do not require a terminating semi-colon. Formulas with more than a single line must use a terminating semi-colon, or will end up with a syntax error.

When the return value is an array (list of values), the variable receives a semi-colon separated serialization of the array.

Tag Attributes

AttributesValue Description
Variable NameAny unreserved name. Reserved names are listed on: Variables
Variable Value

Any string representing a valid JS expression (cf annotation comments in grid)

The value of the JS expression is:
- a text
- a number
- a boolean as text: True, False
- a date in ISO8601 format

If invalid JS code is used, the variable will receive the text of the error message. A simple arithmetic operation is a valid piece of JS: 567 + 99

It is recommended to use single quotes for strings.

PrecisionIf omitted, the result is rounded to the nearest integer; otherwise specifies the number of decimals.

starting execution of script - CalcVa

Example Scripts

XML
<CalcVar/>
XML
<CalcVar arrayNum=
"
	var a = [666, 55, 4];
	a
"
/>

=> the value in variable {%arrayNum%} is: "666;5;4"

XML
<ytriaAutomation>
	<CalcVar arrayNum=
	"
		var a = [666, 55, 4];
		a
	"
	/>
	<CalcVar arrayString=
	"
		var a = ['montreal'.'is'.'great']:
		a
	"
	/>
	<CalcVar arrayDate=
	"
		var a = [new date(). new Date('1000-01-01'). new Date ('1999-12-31)]:
	"
	/>
	<echo value="Numbers: {%arrayNum%}"/>
	<echo value="Strings: {%arrayString%}"/>
	<echo value="Dates: {%arrayDate%}"/>
	<CalcVar num1="665 + (1)"/>
	<CalcVar num2="-44.0 * ((1 + 50/25)/2)"/>
	<SetVar precision2="2"/>
	<SetVar precision6="6"/>
	<CalcVar result1="{%num1%} + {%num2%}"/>
	<CalcVar result2="{%num1%} - {%num2%}"/>
	<CalcVar result3="{%result1%} * {%num2%} + 88" precision="{%precision2%}"/>
	<CalcVar result4="{%result1%} * ({%num2%} + 1)" precision = "2"/>
	<CalcVar result5="{%result1%} / ({%num2%} + 2)" precision="{%precision6%}"/>
	<echo value="num1 = {%num1%}"/>
	<echo value="num2 = {%num2%}"/>
	<echo value="result1 = {%result1%} = {%num1%} + {%num2%}"/>
	<echo value="result2 = {%result2%} = {%num1%} - {%num2%}"/>
	<echo value="result3 = {%result3%} = {%result1%} * {%num2%} + 88 (precision {%precision2%})"/>
	<echo value="result4 = {%result4%} = {%result1%} * ({%num2%} + 1) (precision 2)"/>
	<echo value="result5 = {%result5%} = {%result1%} / ({%num2%} + 2) (precision {%precision6%})"/>
</ytriaAutomation>
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.