Skip to content

Commit c99e491

Browse files
authored
Create functions_xls.md
- Will revise tables again
1 parent d216afe commit c99e491

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

source/functions_xls.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Using functions in XLSForm
2+
3+
Functions are predefined operations used to perform calculations or manipulate data in XLSForm. They are essential for automating tasks and deriving key insights in your forms, allowing you to calculate project indicators, create scoring systems, and manage dates efficiently.
4+
5+
This article lists common functions used in XLSForm, including functions to manipulate numbers, strings, dates, and GPS points.
6+
7+
<p class="note">
8+
To learn more about form logic in XLSForm, see <a href="https://support.kobotoolbox.org/form_logic_xls.html">Introduction to form logic in XLSForm</a>. To learn about functions specifically used in repeat groups, see <a href="https://support.kobotoolbox.org/repeat_groups_xls.html">Repeat groups in XLSForm</a>.
9+
</p>
10+
11+
## Commonly used functions in XLSForm
12+
13+
The following functions are some of the most frequently used in XLSForm. They help control form behavior, manage responses, and perform basic calculations or logical operations across questions. These functions can be applied in calculations, constraints, relevance conditions, and other expressions throughout your form.
14+
15+
| Function | Description |
16+
|:-----------|:-------------|
17+
| `if(expression, then, else)` | If the expression is TRUE, returns `then`. If not, returns `else`. |
18+
| `selected(${question_name}, option_name)` | Used to determine if a specific choice was selected in a `select_multiple` question. |
19+
| `random()` | Returns a random number between 0.0 (inclusive) and 1.0 (exclusive). |
20+
| `count-selected(${question_name})` | Returns the number of options selected in a `select_multiple` question. |
21+
| `coalesce(${question1}, ${question2})` | Returns the first non-empty value of the two arguments. Returns an empty string if both are empty or non-existent. |
22+
| `jr:choice-name(choice_name, '${question_name}')` | Returns the label value, in the active language, associated with the `choice_name` in the list of choices for a select type question. To retrieve the label of whichever response was selected, use `jr:choice-name(${question_name}, '${question_name}')`. |
23+
| `selected-at(${question_name}, n)` | Returns a selected choice in a `select_multiple` question at the nth position. For example, `selected-at(${question_name}, 2)` returns the second choice selected in a `select_multiple` question. |
24+
| `once(expression)` | Evaluates an expression only once (e.g., to ensure a random number is only generated once, or to store the first value entered for a question even if the response is changed later). |
25+
| `instance('list_name')/root/item[name = ${question}]/column_name` | Retrieves a value from the choices sheet. Searches the choice list named `list_name`, finds the row where the choice `name` matches the response to `${question}`, and returns the value from the column specified as `column_name`. |
26+
27+
## Functions to manipulate numbers
28+
29+
The following functions are used to perform mathematical operations or transform numeric values in XLSForm. They can help you calculate, round, or convert numbers, as well as apply more advanced mathematical expressions when needed.
30+
31+
| Function | Description |
32+
|:---------|:------------|
33+
| `int(number)` | Transforms a decimal number into an integer without rounding. |
34+
| `round(number, places)` | Rounds a decimal value to a predetermined number of decimal places. |
35+
| `pow(number, power)` | Calculates the power of a number. |
36+
| `number(x)` | Converts x (a string or boolean expression) to a number value. |
37+
| `log(number)` | Returns the natural log of a number. |
38+
| `log10(number)` | Returns the base-10 log of a number. |
39+
| `abs(number)` | Returns the absolute value of a number. |
40+
| `sin(number)` <br> `asin(number)` <br> `cos(number)` <br> `acos(number)` <br> `tan(number)` <br> `atan(number)` | Returns the sine/arc sine, cosine/arc cosine, or tangent/arc tangent of a number. |
41+
| `sqrt(number)` | Returns the square root of a number. |
42+
| `exp(x)` <br> `exp10(x)` | Returns e^x. |
43+
| `pi()` | Returns an approximation of the mathematical constant π. |
44+
45+
<p class="note">
46+
<strong>Note:</strong> Inside these functions, either constants or <a href="https://support.kobotoolbox.org/form_logic_xls.html#question-referencing">question references</a> can be included.
47+
</p>
48+
49+
## Functions to manipulate strings
50+
51+
The following functions are used to create, modify, or analyze text strings in XLSForm. They are useful for combining text, checking for specific patterns or characters, and cleaning or formatting text inputs.
52+
53+
| Function | Description |
54+
|:---------|:------------|
55+
| `concat()` | Concatenates one or more arguments (separated by commas) into a single string. |
56+
| `regex(string, expression)` | Returns `True` if the string is an exact and complete match for a <a href="https://support.kobotoolbox.org/restrict_responses.html">regular expression</a>. |
57+
| `contains(string, substring)` | Returns `True` if the string contains the substring. |
58+
| `starts-with(string, substring)` | Returns `True` if the string begins with the substring. |
59+
| `ends-with(string, substring)` | Returns `True` if the string ends with the substring. |
60+
| `substr(string, start[, end])` | Returns the substring of `string` beginning at the index start and extending to (but not including) index end (or to the termination of `string`, if end is not provided). |
61+
| `substring-before(string, target)` | Returns the substring of `string` before the first occurrence of the target substring. If the target is not found, or `string` begins with the target substring, then this will return an empty string. |
62+
| `substring-after(string, target)` | Returns the substring of `string` after the first occurrence of the target substring. If the target is not found this will return an empty string. |
63+
| `translate(string, fromchars, tochars)` | Returns a copy of string, where every occurrence of a character in `fromchars` is replaced by the corresponding character in `tochars` (e.g., replacing all lowercase letters with uppercase letters). <br><br> <strong>Note:</strong> If fromchars is longer than tochars, every occurrence of a character in `fromchars` that does not have a corresponding character in `tochars` will be removed. |
64+
| `string-length(string)` | Returns the number of characters in `string` (e.g., to add a word limit to a text question). |
65+
| `normalize-space(string)` | Returns a string in which any leading and trailing whitespaces in string are removed, and sequences of whitespaces are replaced with a single space. |
66+
67+
## Functions to manipulate dates
68+
69+
The following functions are used to record, format, and calculate date and time values in XLSForm. They can help capture the current date or time, convert text into date format, or display dates and times in a specific format.
70+
71+
| Function | Description |
72+
|:---------|:------------|
73+
| `today()` | Returns the current date without a time component. |
74+
| `now()` | Returns the current date and time in ISO 8601 format, including the timezone. |
75+
| `date('YYYY-MM-DD')` | Forces dates into the correct date format (especially for dates before 1970). |
76+
| `format-date(date, format)` | Returns date as a string formatted as defined by <code>format</code>. Common formats include: <ul><li><code>%Y</code>: 4-digit year</li><li><code>%y</code>: 2-digit year</li><li><code>%m</code>: 0-padded month</li><li><code>%n</code>: numeric month</li><li><code>%b</code>: short text month (Jan, Feb, Mar…)</li><li><code>%d</code>: 0-padded day of month</li><li><code>%e</code>: day of month</li><li><code>%a</code>: short text day (Sun, Mon, Tue…)</li></ul> |
77+
| `format-date-time(dateTime, format)` | Returns dateTime as a string formatted as defined by <code>format</code>. Common formats include: <ul><li><code>%H</code>: 0-padded hour (24-hr time)</li><li><code>%h</code>: hour (24-hr time)</li><li><code>%M</code>: 0-padded minute</li><li><code>%S</code>: 0-padded second</li><li><code>%3</code>: 0-padded millisecond ticks</li></ul> |
78+
79+
80+
## Functions to manipulate GPS data
81+
82+
The following functions are used to work with geographic data collected through GPS questions in XLSForm. They can calculate distances, perimeters, or areas based on geopoint, geotrace, or geoshape responses.
83+
84+
| Function | Description |
85+
|:---------|:------------|
86+
| `area(${geoshape})` | Returns the area, in square meters, of a `geoshape` value. |
87+
| `distance(geo)` | Returns the distance, in meters, of either: <ul><li>the perimeter of a `geoshape`</li><li>the length of a `geotrace` value</li><li>a list of geopoints either specified as strings or references to other fields (including from repeat groups), separated by commas</li></ul> |
88+
| `geofence(${geopoint}, ${geoshape})` | Returns `True` if the specified ${geopoint} is inside the specified ${geoshape}, `False` otherwise. Supported only in KoboCollect. |
89+

0 commit comments

Comments
 (0)