# Cell Context

DsExcel allows you to work with context relationships, context cell, and context precedence in templates.

## Content

A template layout can contain multiple bound fields which depend on each other while expansion in the final Excel report.
**Value**: Cell location or Data field
*Custom:* Cell context must be specified explicitly.
*Default* (default value): The adjacent cell on the left with E=V, or the adjacent cell on the top with E= H.
*None:* The cell has no context.
**Example**
{{ds.field(C=A1, E=H)}}
Hello World! {{(C=A2)}}
{{=SUM(F4) (C=ds1.team)}}
{{=SUM(ds1.score) (C=ds1.team\*ds1.season)}}
For example, in the below image, the 'team' and 'name' are two bound fields in the template layout where team is the former cell and name is the latter cell. Now, the 'name' field will depend on the 'team' field to group or filter its values based on the team. Also, the direction of expansion of the 'name' field will be decided by the 'team' field. Here, team is the context cell of name.
![DsExcel Templates uses the team field as the context cell so dependent name values group, filter, and expand with each team.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/capturetemp.png)

## Context Relationships

When multiple fields bound to a data source are defined in a template layout, a relationship is established between them which is called 'Context' relationship. The former cell is called the context cell of latter cell. Based on this relationship, the data is filtered or grouped while expansion in the final report.
There are two types of context relationships:

* **Filtering Relationship**: The data in the cell is filtered using data of the context cell as the filter condition. For example, in the below image, the data in the 'name' cell is filtered corresponding to the data in its context cell:
    ![DsExcel Templates demonstrates a filtering context relationship where dependent name values are filtered by the corresponding context field value.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/filter_rltnshp.png)
* **Following Relationship**: The data in the cell is grouped according to the expansion direction of the data in context cell. For example, in the below image, the data in the 'name' cell is grouped and expanded horizontally depending on its context cell:
    ![DsExcel Templates demonstrates a grouping context relationship where dependent name values group and expand horizontally with their context field.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/image.9c7954.png)

## Context Cell

The context of a cell is defined using the '**C'** property. The data in cells expand vertically or horizontally depending on their context. A cell's context can be set in the below ways:

* **None**: No cell context (C= None)
    ![DsExcel Templates uses C=None to remove cell context so the bound field expands independently of neighboring template fields.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/none_context.png)
* **Custom**: The cell context is specified explicitly using '**C'** property
    ![DsExcel Templates uses the C property with an explicit cell reference to assign a custom context relationship between bound fields.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/custom_context.png)
* **Default**: If no context is defined in the cell, the default context cell is the adjacent cell on the left with E=V (expanding vertically),
    or adjacent cell on the top with E= H (expanding horizontally)
    For example, in the below image, A1 is the context cell of B1 and expands vertically.
    ![DsExcel Templates uses the team field as the context cell so dependent name values group, filter, and expand with each team.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/capturetemp.png)
<br>
    And, A1 is the context cell of A2 and expands horizontally.
    ![DsExcel Templates uses the default horizontal context relationship where A1 controls the dependent field expansion across columns.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/capture2.png)

## Context Precedence

The priority order in which context should be applicable on a cell is determined in the following order:
**Explicit context > Default Context > Fallback context**

* **Explicit context**: The context defined by **C** property in the cell itself
* **Default context**: If no context is defined in the cell, the default context is given priority
* **Fallback context**: If there is no adjacent cell value on the left or top, the cell looks for a cell with **R** (Range) property which covers its location, and use it as its context.

The **Fallback context** can be defined in a cell using the Range property, in case no default or explicit context is defined. The cell that defines the range is followed as a context for other cells to expand.
For example, the below template layout is created to display the sales details for different camera models, which means the data needs to expand with repect to the model of the camera. Then after a break, the sales details need to be displayed for another model of the camera. Instead of adding context to every field, we can define the range R=B11:F16 for Camera model - {{ds.Name (R=B11:F16)}}, stating that the sales details need to expand and group with respect to the camera model.
![DsExcel Templates assigns a Range property to the camera model field as fallback context for grouped sales detail cells.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/template10.png)
The above template layout will generate the following Excel report:
![DsExcel Templates generates an Excel report where camera sales details expand and group within the fallback Range context.](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/template14.png)