# Tables

> Excel tables with header rows, totals rows, banded styles, calculated columns, automatic growth and structured references such as Orders[Amount] and [@Units].

A table turns a block of cells into a named list with a header row. Formulas then refer to its columns by name with [structured references](https://docs.birchui.com/spreadsheet/formulas.md#structured-references), and the table grows as you type below it. Tables round-trip to xlsx as Excel tables.

## Create a table

```ts
sheet.setRange('A1:E4', [
  ['Region', 'Rep', 'Units', 'Unit Price', 'Amount'],
  ['North', 'Maya Patel', 12, 24.5, '=[@Units]*[@[Unit Price]]'],
  ['South', 'Liam Chen', 8, 31, '=[@Units]*[@[Unit Price]]'],
  ['West', 'Ana Souza', 20, 18.25, '=[@Units]*[@[Unit Price]]'],
]);
sheet.addTable('A1:E4', { name: 'Orders', totalsRow: true });
sheet.setFormula('H1', '=SUM(Orders[Amount])');
```

`addTable(range, options?)` returns the table's name. Without a `name` the table is called `Table1`, `Table2` and so on.

**`TableOptions`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface TableOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `name?` | `string` |  |
| `headerRow?` | `boolean` |  |
| `totalsRow?` | `boolean` |  |
| `style?` | `string` |  |
| `showRowStripes?` | `boolean` |  |
| `showColumnStripes?` | `boolean` |  |
| `showFirstColumn?` | `boolean` |  |
| `showLastColumn?` | `boolean` |  |
| `autoFilter?` | `boolean` |  |
| `columns?` | `TableJSON['columns']` |  |

- The first row becomes the header row. Empty header cells are filled with `Column1`, `Column2` and so on, and duplicate headers get a number appended (`Month`, `Month2`). Pass `headerRow: false` for a table without a header row.
- A table cannot overlap another table, merged cells or a spilled array result. Those cases fire an `error` event with the code `'table'` and throw.
- Table names follow the rules for defined names, must not look like a cell address, and must be unique across the workbook.

## Calculated columns

A column whose data cells all hold the same formula is a calculated column. You can also declare one with `columns: [{ name: 'With tax', formula: '=[@Cost]*1.2' }]`. New rows get the formula automatically.

## Growing and shrinking

- Typing a value directly below the last row extends the table by one row and fills in the calculated columns. Typing directly to the right of the table adds a column. Undo reverses both the value and the growth.
- Inserting or deleting rows and columns inside a table resizes it, and formulas keep pointing at the right cells.
- `resizeTable(name, range)` sets a new range. The header row must stay in the same row.

## Totals row

`updateTable(name, { totalsRow: true })` adds a totals row below the data. The first column shows "Total", and the last column gets `=SUBTOTAL(109,[Amount])`, a sum that ignores rows hidden by a filter. Set `totalsFunction` on a column (`'sum'`, `'average'`, `'count'`, `'countNums'`, `'max'`, `'min'`, `'stdDev'`, `'var'` or `'custom'` with `totalsFormula`) in the workbook JSON to choose a different function.

## Renaming, styles and removal

```ts
sheet.updateTable('Orders', { name: 'Sales', showColumnStripes: true, style: 'TableStyleMedium9' });
sheet.setValue('C1', 'Quantity');
sheet.removeTable('Sales');
```

- Renaming a table rewrites every formula that refers to it, on every sheet.
- Editing a header cell renames the column and rewrites `[@Units]` to `[@Quantity]` wherever it is used.
- `removeTable(name)` converts the table back to a plain range. Its values stay, and structured references become absolute references such as `$E$2:$E$4`.
- `getTables(sheet?)` and `getTable(name)` return `TableJSON` objects. Tables are stored per sheet in the workbook JSON under `tables`.

**`TableJSON`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface TableJSON
```

| Property | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `range` | `string` |  |
| `columns` | `TableColumn[]` |  |
| `headerRow?` | `boolean` |  |
| `totalsRow?` | `boolean` |  |
| `style?` | `string` |  |
| `showRowStripes?` | `boolean` |  |
| `showColumnStripes?` | `boolean` |  |
| `showFirstColumn?` | `boolean` |  |
| `showLastColumn?` | `boolean` |  |
| `autoFilter?` | `boolean` |  |
| `filter?` | `TableFilterState` |  |
| `sortState?` | `SortState` |  |

## Headless engine

The [headless engine](https://docs.birchui.com/spreadsheet/headless-engine.md) supports tables too:

```ts
const engine = createFormulaEngine({ sheets: ['Orders'] });
engine.setRange('Orders', 'A1', [['Item', 'Cost'], ['Paper', 12], ['Toner', 85]]);
engine.addTable('Orders', 'A1:B3', { name: 'Supplies' });
engine.evaluate('=SUM(Supplies[Cost])'); // 97
```
