Combine Variables
The following examples put multiple variables of a form on a single line with the necessary separators or special formatting. This is typically used for letterhead footers, business cards, special price formatting, etc.
NOTE: This procedure is similar to the one for creating an action to switch layers in a document. Refer to
“On Change Action to show/hide layers” for more details on creating the variable with an action, but replace the action code as specified below.
Example 1: Footer with simple combination
This example inserts different separators, commas, spaces, dashes, depending on the variable.
Figure 10.110: Multiple variables are combined in an variable action to create a single line.
|
The code for this action is one line with a statement that returns as many variables as you want. The variables are joined in the actions code by selecting the “&” operator. All the variables that will be combined must be set to Required in the General tab of the Variable Settings dialog, otherwise the separators will not make sense if a field remains empty.
Summary
•Action type: Value (In the Actions tab, edit Value.)
•Variable type: Calculated Field
•Document with multiple variables (all variables are set to Required):
•First_Name
•Last_Name
•Street
•City
•Country
•Email
•Action code for new variable:
1 return variable First_Name value & string <space> & variable Last_Name value & string <space>-<space> & variable Street value & string ,<space> & variable City value & string ,<space> & variable Country value & string <space>-<space>email:<space> & variable Email value |
Example 2: Footer combining variables with function
This example is the same as Example 1, but with only one separator, a dash. Logic has been added to remove the separators if one or more form fields are not filled in by the shopper. This means the variables that will be combined must not necessarily be set to Required.
Figure 10.111: All the combined variables have a value and separator.
|
Figure 10.112: The separator is omitted if a variable has no value entered.
|
Summary
•Action type: Value (In the Actions tab, edit Value.)
•Variable type: Calculated Field
•Document with multiple variables:
•Company
•Address
•Phone
•Tax_No
•Bank_Account
•Action code: for new variable Footer:
The code starts with 2 lines that each declare a local variable (localStrTemp and localBlnAddHyphen) which are subsequently used in the rest of the code. Lines 3 to 9 are a conditional statement that checks whether a variable is filled with a string value or not, and then sets the dash separator or leaves it out. These 7 lines can be repeated for as many variables that you want to include in the new combined variable. Use CTRL Copy and add the 7 lines to the clipboard, and then paste these lines and change the variable name.
1 declare string localStrTemp = string 2 declare boolean localBlnAddHyphen = false |
3 if variable Company value is not string 4 if local localBlnAddHyphen is true 5 set local localStrTemp = local localStrTemp & string <space>-<space> 6 end if 7 set local localStrTemp = local localStrTemp & variable Company value 8 set local localBlnAddHyphen = true 9 end if |
10 if variable Address value is not string 11 if local localBlnAddHyphen is true 12 set local localStrTemp = local localStrTemp & string <space>-<space> 13 end if 14 set local localStrTemp = local localStrTemp & variable Address value 15 set local localBlnAddHyphen = true 16 end if |
17 if variable Phone value is not string 18 if local localBlnAddHyphen is true 19 set local localStrTemp = local localStrTemp & string <space>-<space> 20 end if 21 set local localStrTemp = local localStrTemp & variable Phone value 22 set local localBlnAddHyphen = true 23 end if |
24 if variable Tax_No value is not string 25 if local localBlnAddHyphen is true 26 set local localStrTemp = local localStrTemp & string <space>-<space> 27 end if 28 set local localStrTemp = local localStrTemp & variable Tax_No value 29 set local localBlnAddHyphen = true 30 end if |
31 if variable Bank_Account value is not string 32 if local localBlnAddHyphen is true 33 set local localStrTemp = local localStrTemp & string <space>-<space> 34 end if 35 set local localStrTemp = local localStrTemp & variable Bank_Account value 36 set local localBlnAddHyphen = true 37 end if |
38 return local localStrTemp |
Example 3: Footer combining variables with regular expression
This example is the same as Example 2, but uses regular expressions for the dash separator, and one declare statement. The result is the same, but you use less code than for example 2.
In this example we add <space>-<space> to each variable and concatenate all of them into one single local and temporary variable "localStrTemp". This means that in case not all variables are filled in we might end up with multiple <space>-<space> values after each other, or in front or behind the final string.
To solve this, we create two regular expressions:
•one to replace the double occurrences of <space>-<space> (at least 2 and maximum 10 occurrences in this example) with just one occurrence.
•one to remove the <space>-<space> in the beginning or at the end of the result by replacing it with a <blank> string.
Summary
•Action code:
1 declare string localStrTemp = string |
2 set local localStrTemp = local localStrTemp & variable Company value & string <space>-<space> 3 set local localStrTemp = local localStrTemp & variable Address value & string <space>-<space> 4 set local localStrTemp = local localStrTemp & variable Phone value & string <space>-<space> 5 set local localStrTemp = local localStrTemp & variable Tax_No value & string <space>-<space> 6 set local localStrTemp = local localStrTemp & variable Bank_Account value |
7 set local localStrTemp = stringFunctions replaceRegExp string: local localStrTemp regexp: string /( <space>-<space> ){2,10}/g replace: string <space>-<space> 8 set local localStrTemp = stringFunctions replaceRegExp string: local localStrTemp regexp: string /^ <space>-<space> | <space>-<space> $/g replace: string <blank> |
9 return local localStrTemp |
Example 4: Price formatting with smaller decimals
This example creates a price with 2 decimals which are smaller and aligned with the top of the number before the decimal point. The visible price is a combination of two variables which are not visible on the form but only visible in the document.
Figure 10.113: Two variables, which are not visible, are combined to create a price variable with small decimals which are aligned along the top and without the decimal point
|
Summary
•Action type: On Change (In the Actions tab, edit On Change.)
•Variable type: Number
•Document with multiple variables
First create these two variables:
•A1: digits before the decimal point
•A2: digits after the decimal point
Then use these two variables in a third variable with an action
•Price: new variable with action
•Action code for the Price variable:
1 set variable A1 value = stringFunctions subString string: variable Price displayValue startIndex: number 0 length: stringFunctions indexOf string: variable Price displayValue find: string . caseSensitive: false |
2 set variable A2 value = stringFunctions subString string: variable Price displayValue startIndex: stringFunctions indexOf string: variable Price displayValue <blank> + number 1 find: string . caseSensitive: false length: number 2 |
Price with smaller, top-aligned decimals
1 In your document, create the following 3 variables in the Forms tab:
•A1: the digits before the decimal point; a variable of type Short Text; not visible on the form
•A2: the digits after the decimal point; a variable of type Short Text; not visible
•Price: the price label which is visible on the form and to which the action code is applied; this is a variable of type Number; set the Snap Interval, Decimal Separator, Precision and Rounding as follows:
In the Actions tab, create the code as follows:
1 set variable A1 value = stringFunctions subString string: variable Price displayValue startIndex: number 0 length: stringFunctions indexOf string: variable Price displayValue find: string . caseSensitive: false |
2 set variable A2 value = stringFunctions subString string: variable Price displayValue startIndex: stringFunctions indexOf string: variable Price displayValue <blank> + number 1 find: string . caseSensitive: false length: number 2 |
The screenshot below gives you an idea of how this code actually looks in the editor:
2 Insert variables A1 and A2 in the document.
Format these two variables in the Text Tab; set the Font Size of variable A2 so it’s smaller than A1 and increase the base line of A2 so it aligns with the top of A1.
3 Save the document and go back to the Forms tab and check that the Price variable works as expected.