Calculations

Calculations are used to create hidden fields that automatically compute values based on other answers in the form.

In XLSForm, calculations are defined using the calculate question type. These fields are not shown to the user, but they run in the background and store results for later use or analysis.

In ngSurvey, calculated values will be saved as hidden fields and included in the form data output, just like other answers.

🔢 How to define a calculation

To create a calculation:

  1. Use the type calculate in the survey sheet.

  2. Assign a name to the field.

  3. Use the calculation column to define the logic.

  4. Optionally, use the label column for internal reference (it won’t be shown to the user).

Example: Total score from multiple answers

type
name
calculation

integer

q1

integer

q2

integer

q3

calculate

total_score

${q1} + ${q2} + ${q3}

In this example:

  • The user answers three numeric questions (q1, q2, q3).

  • The total_score field adds those three values together.

  • The value of total_score is saved in the form data, even though it is never displayed.

Useful calculation functions

You can use most standard operators and functions, such as:

  • Math: +, -, *, div, mod

  • Text: string-length(), regex, round, substr, concat

  • Logic: if(condition, true, false), coalesce()

  • Dates: today(), now(), date()

Last updated

Was this helpful?