# Grouping and outlines

> Group rows and columns into collapsible outline levels with plus and minus buttons, level buttons and xlsx round-trip.

Grouping rows or columns lets readers fold away detail and see only the totals. Groups nest up to seven levels, as in Excel.

```ts
sheet.groupRows(2, 12);
sheet.groupRows(2, 4);
sheet.groupRows(6, 8);
sheet.groupRows(10, 12);
sheet.collapseGroup('rows', 5);
```

Here rows 2 to 4, 6 to 8 and 10 to 12 hold the months of three quarters, rows 5, 9 and 13 hold the quarter totals, and row 13 doubles as the summary of the outer group. Collapsing at row 5 folds the first quarter's months into its total.

## The outline bar

When a sheet has groups, a bar appears beside the row headers (and above the column headers for column groups).

- A bracket shows the extent of each group.
- The `−` button on the summary row collapses the group; `+` expands it again. By default the summary is the row below the group, or the column to its right. Set `outline.summaryBelow` or `outline.summaryRight` to `false` in the sheet JSON to put it above or to the left.
- The numbered buttons in the corner show one outline level: `1` shows only the outermost totals, the highest number shows everything.

The buttons are real buttons with names such as "Collapse rows 2 to 4" and "Show row outline level 1", and report their state with `aria-expanded`. Every change is undoable.

## Outlines in code

| Method | Purpose |
| --- | --- |
| `groupRows(first, last)` / `ungroupRows(first, last)` | Raises or lowers the outline level of rows (1-based). |
| `groupColumns(first, last)` / `ungroupColumns(first, last)` | The same for columns, by letter or 1-based number. |
| `collapseGroup(axis, index)` / `expandGroup(axis, index)` | Hides or shows the group containing `index`, or the group whose summary row or column is `index`. `axis` is `'rows'` or `'columns'`. |
| `showOutlineLevel(axis, level)` | Shows levels below `level` and hides the rest. |

Collapsing hides the rows or columns in the ordinary way, so formulas such as `SUBTOTAL(109, …)` skip them and the workbook JSON lists them in `hiddenRows` and `hiddenColumns`. The levels themselves are kept in the sheet's `outline`:

```json
{ "outline": { "rows": { "2": 1, "3": 2, "4": 2 }, "columns": { "C": 1 } } }
```

Outline levels follow inserted and deleted rows and columns, and round-trip to xlsx; see [Excel files](https://docs.birchui.com/spreadsheet/xlsx.md#what-round-trips).
