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
Attributes | Value Description |
---|---|
Variable Name | Any unreserved name. Reserved names are listed on: Variables |
Variable Value | Any string representing a valid JS expression (cf annotation comments in grid) It is recommended to use single quotes for strings. |
Precision | If omitted, the result is rounded to the nearest integer; otherwise specifies the number of decimals. |
Example Scripts
<CalcVar/>
<CalcVar arrayNum=
"
var a = [666, 55, 4];
a
"
/>
=> the value in variable {%arrayNum%} is: "666;5;4"
<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>