Using Expressions for Surveys
Expressions allow you to calculate default values or apply logic within your survey, similar to formulas in Excel. They can be used for operations such as summing values, calculating BMI, or inserting today’s date. Expressions follow a structured format, using curly brackets {}
to reference variables and supporting mathematical operators and functions.
Instructions:
-
Basic Calculations:
- Use mathematical operators such as
+
,-
,*
, and/
to perform calculations. - Example:
{question1} + {question2}
(Sum of two values). - Example:
({price} * {quantity}) * (100 - {discount})
(Calculating discounted price).
- Use mathematical operators such as
-
Using Functions:
- Conditional logic:
iif(condition, value_if_true, value_if_false)
- Example:
iif({age} >= 18, "Adult", "Minor")
- Example:
- Date functions:
today()
→ Inserts the current date.age({birthdate})
→ Calculates age based on a birthdate.
- Mathematical functions:
min(value1, value2, ...)
→ Returns the smallest value.max(value1, value2, ...)
→ Returns the largest value.count(value1, value2, ...)
→ Counts the number of values.avg(value1, value2, ...)
→ Computes the average of values.
- Conditional logic:
-
Ensuring Correct Order:
- Always use parentheses
()
to ensure the correct order of operations. - Example:
({price} * {quantity}) / (1 + {tax_rate})
- Always use parentheses
By following these guidelines, you can create dynamic and interactive surveys that adjust values automatically based on user inputs.
Built-in Functions in Ripple Survey Creator
Ripple Survey Creator provides built-in functions to enhance survey logic and automate calculations. These functions allow you to implement conditional logic, validate responses, perform mathematical operations, and manipulate dates.
TIP: Always test your expressions using the "Open as a Live Survey" feature after creating the function.
Instructions for Using Built-in Functions:
1. Conditional Logic:
iif(condition, valueIfTrue, valueIfFalse)
- Returns
valueIfTrue
if the condition is met; otherwise, returnsvalueIfFalse
. - The condition is an expression that can contain typical operands/operators and references to question values (as question names in curly brackets).
- Example:
"iif({question1} + {question2} > 20, 'high', 'low')"
- Returns
2. Survey Validation & Mode Detection:
isContainerReady(nameOfPanelOrPage)
-
- Boolean
- This function recursively validates all questions in the container. If there is an error, the function returns false, otherwise true. In case a question's value is empty and neither validators no required status are defined for the editor, validation would pass successfully.
- Returns
true
if all required questions on a page or panel are answered correctly. - Example:
"isContainerReady('page1')"
-
-
isDisplayMode()
- Returns
true
if the survey is in display mode. - Example:
"isDisplayMode()"
- Returns
3. Date & Time Functions:
age(birthDate)
- Returns the respondent’s age based on their birth date.
- The passed date value (which is typically taken from the referenced question) should be defined as a valid JavaScript Date .
- Example:
"age({birthdate})"
currentDate()
- Returns today’s date.
- Example:
"currentDate()"
today(daysToAdd?)
- Returns today’s date, or a date with added/subtracted days. For example, "today()" returns the current date 0 hours, 0 minutes, "today(-1) returns yesterday's date, "today(1)" returns tomorrow's date, "today(2) returns day after tomorrow date, and so on.
- Example:
"today(2)"
(Day after tomorrow)
userDate()
- Returns the respondent’s local date.
- Example:
"userDate()"
userDateTime()
- Returns the respondent’s local date and time.
Note
This Expressions
userDate()
,userDateTime()
andgetDate()
can only link to a Text-type participant variable."userDateTime()"
- Returns the respondent’s local date and time.
getDate()
- Returns the full date, time, and timezone.
- Example:
"getDate()"
userTimeZone()
- Returns the respondent’s timezone offset in minutes from GMT.
- Example:
"userTimeZone() / 60"
(Convert minutes to hours)
now()
- Returns the current timestamp in milliseconds since January 1, 1970.
- Used to track the respondent's timestamp.
- Example:
"now()"
diffDays(dateFrom, dateTo:any)
- Returns the number of days between two dates.
- Dates are typically specified by expressions that correspond to questions (by their names in curly brackets) containing date values.
- Example:
"diffDays({startDate}, {endDate})"
NOTE:
This Expressions userDate()
, userDateTime()
and getDate()
can only link to a Text-type participant variable.
4. Mathematical Functions:
sum(par1: number, par2:number,...)
- Returns the sum of passed arguments.
- Example:
"sum({total1}, {total2})"
max(par1:number, par2:number, ...)
- Returns the largest value from the given numbers.
- Example:
"max({total1}, {total2})"
min(par1:number, par2:number, ...)
- Returns the smallest value from the given numbers.
- Example:
"min({total1}, {total2})"
avg(par1:number, par2:number, ...)
- Returns the average of the provided numbers.
- Example:
"avg({total1}, {total2}, {total3})"
By integrating these functions into your survey expressions, you can automate calculations, enhance validation, and improve user experience.
Populating Today’s Date by Default in a Consent Form
To automatically populate the current date into a date field in a survey, follow these steps:
1. Add an Expression (Read-Only) Question
- Open the Toolbox in the survey editor.
- Drag and drop an "Expression (read-only)" question into your form.
2. Configure the Expression
- Navigate to the "Properties" panel on the right.
- Under the "General" section, locate the "Expression(?)" field.
- Enter the following expression:
today()
3. Adjust Display Settings
- Find the "Display style" dropdown menu.
- Select "Date" to format the output correctly.
4. Save & Test
- Click the "Save Survey" button.
