Функции
String functions
When working with strings within expressions, it’s important to use single quotes (’’) to enclose literal strings. However, an exception arises when you want to include single quotes within a literal string. In such cases, you can use double quotes ("") to enclose the entire string.
For example:
- Correct: if(${yesno} = 1, “a string with ‘single quotes’ in it”, “no single quotes here”)
- Incorrect: if(${yesno} = 1, ‘a string with ‘single quotes’ in it’, ’no single quotes here’)
Regarding smart quotes, it’s crucial to be aware of their presence, as they can cause issues in expressions. Many rich text editors automatically convert straight quotes ("" or ‘’) into smart quotes or curly quotes (“” or ‘’), which may result in syntax errors or unexpected behavior. To avoid this, ensure that you are using straight quotes (’’) consistently in your expressions.
Please let me know if you have any further questions or need additional assistance!
rtSurvey supports various functions, including:
string(field): Converts a field to a string.- Example:
string(34.8)will be converted to'34.8'.
- Example:
string-length(field): Returns the length of a string field.- Example:
string-length(.) > 3 and string-length(.) < 10can be used to ensure the current field is between 3 and 10 characters.
- Example:
substr(fieldorstring, startindex, endindex): Returns a substring starting atstartindexand ending just beforeendindex. Indexes start at 0 for the first character in the string.- Example:
substr(${phone}, 0, 3)will return the first three digits of a phone number.
- Example:
concat(a, b, c, ...): Concatenates fields (and/or strings) together.- Example:
concat(${firstname}, ' ', ${lastname})will return a full name by combining the values in thefirstnameandlastnamefields.
- Example:
linebreak(): Returns a linebreak character.- Example:
concat(${field1}, linebreak(), ${field2}, linebreak(), ${field3})will return a list of three field values with linebreaks between them.
- Example:
lower(): Converts a string to all lowercase characters.- Example:
lower('Street Name')will return “street name”.
- Example:
upper(): Converts a string to all uppercase characters.- Example:
upper('Street Name')will return “STREET NAME”.
- Example:
select_one and select_multiple functions
count-selected(field): Returns the number of items selected in a select_multiple field.- Example:
count-selected(.) = 3can be used as a constraint expression to ensure exactly three choices are selected.
- Example:
selected(field, value): Returns true or false depending on whether the specified value was selected in the select_one or select_multiple field.- Example:
selected(${color}, 'Blue')can be used as a relevance expression to show a group or field only if the respondent selected “Blue” as their favorite color. - Note: The second parameter should always specify the choice value, not the choice label. Use the value from the value column in the choices worksheet of the form definition.
- Example:
selected-at(field, number): Returns the selected item at the specified position in a select_multiple field. When the passed number is 0, it returns the first selected item; when the number is 1, it returns the second selected item, and so on.- Example:
selected-at(${fruits}, 0) = 'Apple'can be used as a relevance expression to show a group or field only if the first selected choice is “Apple”. - Note: The returned value will be the choice value, not the choice label. Use the value from the value column in the choices worksheet of the form definition.
- Example:
choice-label(field, value): Returns the label for a select_one or select_multiple field choice, as defined in the choices worksheet of the form definition.- Example 1:
choice-label(${country}, ${country})will return the choice label for the currently selected choice in the field namedcountry. - Example 2:
choice-label(${languages}, selected-at(${languages}, 0))will return the label for the first selected choice in the field namedlanguages. - Note: This function retrieves the choice label, not the value. It uses the label column from the choices worksheet of the form definition.
- Example 1:
Repeated field functions
In rtSurvey, if you want to ask the same question(s) multiple times, you can put a field inside a repeat group. This results in multiple instances of the same field. The following functions can help you deal with these repeated fields and the repeated data they produce. See the help topic on repeating fields for more details.
join(string, repeatedfield): For a field within a repeat group, generates a string-separated list of values. The first parameter specifies the delimiter to use to separate the values.- Example:
join(', ', ${member_name})will generate a single comma-separated list from all entered names.
- Example:
join-if(string, repeatedfield, expression): Functions exactly likejoin(), except that it checks each instance in the repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the output.- Example:
join-if(', ', ${member_name}, ${age} >= 18)will generate a comma-separated list of names of only adult members (those with ages 18 and over).
- Example:
count(repeatgroup): Returns the current number of times that a repeat group has repeated.- Example:
count(${groupname})will return the number of instances of the group.
- Example:
count-if(repeatgroup, expression): Functions exactly likecount(), except that it checks each instance in the repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the output.- Example:
count-if(${members}, ${age} >= 18)will return the count of adult members based on the age field within the “members” repeat group.
- Example:
sum(repeatedfield): For a field within a repeat group, calculates the sum of all values.- Example:
sum(${loan_amount})will return the total value of all loans.
- Example:
sum-if(repeatedfield, expression): Functions exactly likesum(), except that it checks each instance in the repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the output.- Example:
sum-if(${loan_amount}, ${loan_amount} > 500)will return the total value of all loans that are over 500. Smaller loans will be ignored.
- Example:
min(repeatedfield): For a field within a repeat group, calculates the minimum of all values.- Example:
min(${member_age})will return the age of the youngest member in the group.
- Example:
min-if(repeatedfield, expression): Functions exactly likemin(), except that it checks each instance in the repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the output.- Example:
min-if(${member_age}, ${member_age} >= 18)will return the age of the youngest adult in the group. Those younger than 18 will be ignored.
- Example:
max(repeatedfield): For a field within a repeat group, calculates the maximum of all values.- Example:
max(${member_age})will return the age of the oldest member in the group.
- Example:
max-if(repeatedfield, expression): Functions exactly likemax(), except that it checks each instance in the repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the output.- Example:
max-if(${member_age}, ${member_age} >= 18)will return the age of the oldest adult in the group. Those younger than 18 will be ignored.
- Example:
index(): Called within a repeat group, returns the index number for the current group or instance.- Example:
index()when used within a repeat group will return 1 for the first instance, 2 for the second, and so on.
- Example:
indexed-repeat(repeatedfield, repeatgroup, index): References a field or group that is inside a repeat group from outside that repeat group. The first parameter specifies the repeated field or group of interest, the second specifies the repeat group within which the field or group is located, and the third specifies the instance number within the repeat group to use.- Example 1:
indexed-repeat(${name}, ${names}, 1)will return the first name available when the name field is inside a prior repeat group named “names”. - Example 2:
indexed-repeat(${name}, ${names}, index())will pull the name corresponding to the instance number of the current repeat group.
- Example 1:
rank-index(index, repeatedfield): This function calculates the ordinal rank of the specified instance of a repeated field for use outside the repeat group. The rank of 1 is assigned to the instance with the highest value, the rank of 2 to the instance with the next-highest value, and so on. If you pass an invalid index or an index to an instance with a non-numeric value, a rank of 999 will be returned.- Example:
rank-index(1, ${random_draw})calculates the rank of the first instance based on the value of itsrandom_drawfield compared to other instances’ values.
- Example:
rank-index-if(index, repeatedfield, expression): This function works similarly torank-index(), but it checks each instance in the repeated field’s repeat group using the supplied expression. If the expression evaluates to false, the item will be omitted from the calculation. The index used is based on the full set of instances before evaluating the expression for each instance. If you pass an index for an instance that was ignored due to not satisfying the expression, it is considered an invalid index, and a rank of 999 will be returned.- Example:
rank-index-if(1, ${age}, ${age} >= 18)calculates the age rank within the set of adults, considering only instances where the age is greater than or equal to 18.
- Example:
Number functions
| Operator | Operation | Example | Example answer |
|---|---|---|---|
+ | Addition | 1 + 1 | 2 |
- | Subtraction | 3 - 2 | 1 |
* | Multiplication | 3 * 2 | 6 |
div | Division | 10 div 2 | 5 |
mod | Modulus | 9 mod 2 | 1 |
rtSurvey supports number functions, including:
number(field): Converts the value of the field to a number.- Example:
number('34.8')= 34.8
- Example:
int(field): Converts the value of the field to an integer.- Example:
int('39.2')= 39
- Example:
min(field1, ..., fieldx): Returns the minimum value among the passed fields.- Example:
min(${father_age}, ${mother_age})will return the age of either the mother or the father, whichever is smaller.
- Example:
max(field1, ..., fieldx): Returns the maximum value among the passed fields.- Example:
max(${father_age}, ${mother_age})will return the age of either the mother or the father, whichever is larger.
- Example:
format-number(field): Formats the value of an integer or decimal field according to the user’s locale settings.- Example:
format-number(${income})This expression might format “120000” as “120,000”.
- Example:
round(field, digits): Rounds the numeric field value to the specified number of digits after the decimal place.- Example:
round(${interest_rate}, 2)
- Example:
abs(number): Returns the absolute value of a number.pow(base, exponent): Returns the value of the first parameter raised to the power of the second parameter.- Each parameter can be a field, number, or expression.
log10(fieldorvalue): Returns the base-ten logarithm of the field or value passed in.sin(fieldorvalue): Returns the sine of the field or value passed in, expressed in radians.cos(fieldorvalue): Returns the cosine of the field or value passed in, expressed in radians.tan(fieldorvalue): Returns the tangent of the field or value passed in, expressed in radians.asin(fieldorvalue): Returns the arcsine of the field or value passed in, expressed in radians.acos(fieldorvalue): Returns the arccosine of the field or value passed in, expressed in radians.atan(fieldorvalue): Returns the arctangent of the field or value passed in, expressed in radians.atan2(x, y): Returns the angle in radians subtended at the origin by the point with coordinates (x, y) and the positive x-axis. The result is in the range -pi() to pi().sqrt(fieldorvalue): Returns the non-negative square root of the field or value passed in.exp(x): Returns the value of e^x.pi(): Returns the value of pi.
Date and time functions
Date values in rtSurvey are stored as strings in YYYY-MM-DD format. Datetime values are stored as ISO 8601 strings (YYYY-MM-DDTHH:MM:SS). Use decimal-date-time() to convert to a number for arithmetic (e.g., calculating durations).
today(): Returns today’s date as a string inYYYY-MM-DDformat. Evaluated once when the form opens.- Example:
today()→'2024-03-15' - Common use:
defaultcolumn to pre-fill today’s date, or inrelevant/constraintto compare against a date field.
- Example:
now(): Returns the current date and time as an ISO 8601 string. Evaluated each time the expression is computed.- Example:
now()→'2024-03-15T14:32:00.000+03:00' - Common use: Recording the exact timestamp of a specific event during the survey.
- Example:
date(value): Converts a value (string or number) to a date string. Useful for coercing calculated values into a date type.- Example:
date('2024-03-15')→'2024-03-15'
- Example:
date-time(value): Converts a value to a datetime string.- Example:
date-time(${event_timestamp})
- Example:
decimal-date-time(value): Converts a date or datetime string to a decimal number representing milliseconds since the Unix epoch divided by 86400000 (i.e., fractional days since 1970-01-01). Use this to perform arithmetic on dates.- Example: Duration in days between two dates:
decimal-date-time(${end_date}) - decimal-date-time(${start_date}) - Example: Duration in minutes between two datetimes:
(decimal-date-time(${end_time}) - decimal-date-time(${start_time})) * 1440
- Example: Duration in days between two dates:
format-date(date, format): Formats a date value using a pattern string.- Format tokens:
%Y(4-digit year),%y(2-digit year),%m(month 01–12),%d(day 01–31),%a(abbreviated weekday),%b(abbreviated month name) - Example:
format-date(today(), '%d/%m/%Y')→'15/03/2024' - Example:
format-date(${dob}, '%B %d, %Y')→'March 15, 1990'
- Format tokens:
format-date-time(datetime, format): Formats a datetime value using a pattern string. Accepts allformat-datetokens plus:%H(hour 00–23),%h(hour 01–12),%M(minutes 00–59),%S(seconds 00–59),%3(milliseconds),%P(AM/PM)- Example:
format-date-time(now(), '%d/%m/%Y %H:%M')→'15/03/2024 14:32' - Example:
format-date-time(${event_time}, '%I:%M %p')→'02:32 PM'
Boolean functions
boolean(value): Converts any value to a boolean. Returnstruefor non-empty strings, non-zero numbers, andtrue; returnsfalsefor empty strings,0, andfalse.- Example:
boolean(${name})returnstrueifnameis not empty.
- Example:
boolean-from-string(string): Returnstrueif the string is'1'or'true'(case-insensitive); returnsfalseotherwise.- Example:
boolean-from-string(${enabled_flag})— useful when a field stores'true'/'false'as text.
- Example:
true(): Returns the boolean valuetrue.- Example: In the
requiredcolumn,true()is equivalent toyes.
- Example: In the
false(): Returns the boolean valuefalse.- Example:
if(${skip_section} = 'yes', false(), true())— dynamically set required.
- Example:
not(expression): Returns the logical negation of the expression. Returnstrueif the expression is false, and vice versa.- Example:
not(${consent} = 'yes')— show a warning when consent was NOT given. - Example:
not(selected(${issues}, 'none'))— require detail only if “none” was not selected.
- Example:
Additional string functions
starts-with(string, prefix): Returnstrueifstringbegins withprefix.- Example:
starts-with(${phone}, '+254')checks if the phone number begins with the Kenya country code.
- Example:
contains(string, substring): Returnstrueifstringcontainssubstring.- Example:
contains(${email}, '@')checks that an email address has an@sign. - Example:
contains(${notes}, 'urgent')triggers a follow-up question if the notes mention “urgent”.
- Example:
substring-before(string, needle): Returns the part ofstringthat appears before the first occurrence ofneedle.- Example:
substring-before(${full_name}, ' ')extracts the first word (first name).
- Example:
substring-after(string, needle): Returns the part ofstringthat appears after the first occurrence ofneedle.- Example:
substring-after(${email}, '@')extracts the domain part of an email address.
- Example:
normalize-space(string): Strips leading and trailing whitespace and collapses all internal whitespace sequences to a single space.- Example:
normalize-space(${name})— cleans up a name that may have been typed with extra spaces.
- Example:
translate(string, search_chars, replace_chars): Replaces each character instringthat appears insearch_charswith the corresponding character inreplace_chars. Characters insearch_charswith no corresponding character inreplace_charsare deleted.- Example:
translate(${code}, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')converts to uppercase (equivalent toupper()). - Example:
translate(${phone}, ' -()', '')removes spaces, dashes, and parentheses from a phone number.
- Example:
Additional math functions
floor(number): Returns the largest integer less than or equal tonumber(rounds towards negative infinity).- Example:
floor(4.9)= 4,floor(-2.1)= -3
- Example:
ceiling(number): Returns the smallest integer greater than or equal tonumber(rounds towards positive infinity).- Example:
ceiling(4.1)= 5,ceiling(-2.9)= -2
- Example:
random(): Returns a random decimal number between 0.0 (inclusive) and 1.0 (exclusive). Typically used incalculatefields to assign random values or randomize question order.- Example:
random()→ e.g.,0.7341 - Example:
int(random() * 6) + 1→ random number 1–6 (dice roll)
- Example:
coalesce(a, b): Returnsaifais non-empty; otherwise returnsb. Useful as a fallback when a field might be empty.- Example:
coalesce(${preferred_name}, ${full_name})— use preferred name if set, otherwise fall back to full name.
- Example:
once(value): Evaluatesvalueand stores it, but only if the current field is empty. If the field already has a value (e.g., was previously set),once()returns the existing value unchanged. This prevents recalculation from overwriting user input.- Example:
once(today())in thedefaultcolumn sets today’s date once and does not update if the enumerator re-opens the form. - Example:
once(uuid())generates a UUID once and keeps it stable across re-edits.
- Example:
Geo functions
area(geoshape_value): Calculates the area in square meters enclosed by a geoshape (polygon) value.- The parameter is a geoshape field value in the format
lat1 lon1 0 0; lat2 lon2 0 0; ... - Example:
area(${field_boundary})— calculate the area of a surveyed field in m². - Example:
round(area(${field_boundary}) div 10000, 2)— convert to hectares.
- The parameter is a geoshape field value in the format
distance(coordinates): Calculates the total path length in meters of a geotrace (line), or the distance between two geopoints.- For a geotrace:
distance(${route})returns the total path length in meters. - For two geopoints:
distance(concat(${point_a}, ' ', ${point_b}))returns the distance between them. - Example:
round(distance(${road_trace}) div 1000, 3)— road length in kilometers.
- For a geotrace:
Validation functions
regex(value, pattern): Returnstrueifvaluematches the regular expressionpattern. Use in theconstraintcolumn for pattern-based validation.- The pattern uses standard regex syntax (POSIX ERE subset).
- Example:
regex(., '^[0-9]{10}$')— validate a 10-digit number. - Example:
regex(., '^[A-Z]{2}[0-9]{6}$')— validate a passport number format (2 uppercase letters followed by 6 digits). - Example:
regex(., '^[^@]+@[^@]+\.[^@]{2,}$')— basic email format check.
checklist(min, max, v1, v2, ...): Evaluates a list of boolean expressions and returnstrueif the number oftruevalues is betweenminandmax(inclusive). Pass-1forminormaxto skip that bound.- Example:
checklist(2, 3, ${q1} = 'yes', ${q2} = 'yes', ${q3} = 'yes')— passes if exactly 2 or 3 of the three conditions are true. - Example:
checklist(1, -1, ${smoke_alarm}, ${fire_ext}, ${emergency_plan})— at least one safety measure must be true.
- Example:
weighted-checklist(min, max, v1, w1, v2, w2, ...): Likechecklist(), but each value has a weight. The sum of weights fortruevalues must be betweenminandmax.- Example:
weighted-checklist(10, -1, ${has_toilet}, 4, ${has_sink}, 3, ${has_shower}, 5)— sum of weights for present facilities must be at least 10.
- Example:
Utility functions
uuid(): Generates a random UUID (RFC 4122 v4 format) as a string.- Example:
uuid()→'a3f8b2c1-4d5e-6f7a-8b9c-0d1e2f3a4b5c' - Typically used with
once()to generate a stable unique ID:once(uuid())
- Example:
version(): Returns the value of the form’sversionattribute as set in the settings worksheet.- Example:
version()→'3.1' - Useful in
calculatefields to embed the form version in exported data.
- Example:
position(): When called inside a repeat group, returns the 1-based index of the current repeat instance.- Example:
position()in the first instance returns1, in the second returns2, and so on. - See also:
index()(alias),indexed-repeat()for referencing repeat values from outside the group.
- Example:
thousandsep(length, separator, value): Formats a number with a thousands separator.lengthis the minimum total string length (padded with spaces if shorter),separatoris the character to use (e.g.,','), andvalueis the number to format.- Example:
thousandsep(0, ',', 1234567)→'1,234,567' - Example:
thousandsep(0, '.', ${income})→ formats income with period as thousands separator.
- Example:
substr-jsonpath(value, jsonpath): Extracts a substring from a JSON string using a JSONPath expression.- Example:
substr-jsonpath(${api_response}, '$.data.name')— extract thenamefield from a JSON string stored inapi_response. - Typically used alongside
callapi()to extract specific values from API responses.
- Example: