> For the complete documentation index, see [llms.txt](https://docs.ngsurvey.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ngsurvey.com/projects/import-export/xlsform/survey-sheet/calculations.md).

# 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()`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ngsurvey.com/projects/import-export/xlsform/survey-sheet/calculations.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
