# Freeze panes, outlines and merges

> Lay out worksheets with column widths, frozen header rows and columns, autofilter, grouped outline rows and columns, merged cells, tab colours and right-to-left sheets.

Beyond cell values and styles, a sheet has layout: which rows stay visible while scrolling, which rows fold into groups, which cells span several columns. All of it is set on the `XlsxSheet` object.

## Column widths and row heights

```ts
const sheet: XlsxSheet = {
  name: 'Fleet',
  columns: [{ width: 10 }, { width: 32 }, { width: 14 }, { width: 14, hidden: true }],
  rows: [
    { cells: ['Unit', 'Vehicle', 'Registration', 'Insurer ref.'], height: 24 },
    ['V-104', 'Ford Transit Custom 2.0 EcoBlue', 'KX24 RFU', 'AXA-55120931'],
  ],
};
```

- `width` is in characters of the default 11-point Calibri font, the same unit as Excel's column width dialog. The default is about 8.43. Widths above 255 are clamped.
- `height` on a row is in points. Rows without a height use Excel's default of 15 points and grow when users edit them.
- `hidden: true` hides a column or row. Users can unhide it in Excel.

There is no automatic width calculation, because text width depends on fonts the writer cannot measure. A good estimate is the longest value's length plus two:

```ts
function widthFor(values: unknown[], min = 8, max = 60): number {
  const longest = Math.max(...values.map((value) => String(value ?? '').length));
  return Math.min(max, Math.max(min, longest + 2));
}
```

## Freeze panes

`freeze` keeps rows at the top and columns at the left visible while the user scrolls.

```ts
const sheet: XlsxSheet = {
  name: 'Orders',
  freeze: { rows: 1, columns: 2 },
  rows,
};
```

`rows: 1` freezes the header row, `columns: 2` freezes the first two columns, such as an order number and a customer. Use either on its own or both together.

## Autofilter

`autoFilter` adds Excel's filter drop-downs to a header row.

```ts
{ name: 'Orders', rows, autoFilter: true }
{ name: 'Orders', rows, autoFilter: 'A3:H250' }
```

- `true` covers the used range of the sheet, from its first written cell to its last, with the first row as the header.
- A range string sets the range explicitly, which you need when the table does not start in the first row, for example below a title.

An invalid range is ignored with a warning.

## Merged cells

`merges` lists ranges in A1 notation. The value and style of the top-left cell are shown across the merged area.

```ts
const sheet: XlsxSheet = {
  name: 'Summary',
  columns: [{ width: 22 }, { width: 14 }, { width: 14 }, { width: 14 }],
  merges: ['A1:D1', 'B2:D2'],
  rows: [
    [{ value: 'Regional revenue, Q3 2026', style: { bold: true, align: 'center', font: { size: 14 } } }],
    ['Region', { value: 'Revenue by month', style: { align: 'center' } }],
    ['', 'July', 'August', 'September'],
    ['EMEA', 412000, 398500, 474000],
  ],
};
```

Use [`cellRef()`](https://docs.birchui.com/excel/utilities.md#cell-references) to build ranges from indices:

```ts
import { cellRef } from '@birchui/excel';

const titleSpan = `${cellRef(0, 0)}:${cellRef(0, columnCount - 1)}`;
```

Ranges that are invalid, cover a single cell, or overlap a range listed earlier are skipped with a warning, because Excel refuses to open files with overlapping merges.

## Outlines

Outlines let users fold groups of rows or columns with the `+` and `-` buttons in Excel's margin. Give detail rows an `outlineLevel` from 1 to 7; consecutive rows with a higher level than their neighbour form a group.

```ts title="regional-outline.ts"
const rows: XlsxSheet['rows'] = [
  { cells: ['Region / country', 'Revenue'], style: { bold: true } },
  { cells: ['EMEA', 1284500], style: { bold: true } },
  { cells: ['Germany', 512300], outlineLevel: 1 },
  { cells: ['France', 401900], outlineLevel: 1 },
  { cells: ['United Kingdom', 370300], outlineLevel: 1 },
  { cells: ['APAC', 846210], style: { bold: true } },
  { cells: ['Japan', 488000], outlineLevel: 1, hidden: true },
  { cells: ['Australia', 358210], outlineLevel: 1, hidden: true },
];

const sheet: XlsxSheet = {
  name: 'Revenue',
  columns: [{ width: 24 }, { width: 14, style: { numFmt: '#,##0' } }],
  outlineSummaryBelow: false,
  freeze: { rows: 1 },
  rows,
};
```

- **Collapsed groups.** To ship a group folded, set `hidden: true` on its detail rows. Setting `collapsed: true` on the summary row as well makes Excel show the `+` button in the collapsed state.
- **Summary position.** By default Excel expects the summary row below its details, like a subtotal. When your summary rows come first, as in the example and in most grouped reports, set `outlineSummaryBelow: false`.
- **Nesting.** Use level 2 for rows inside a level-1 group, and so on, up to 7.
- **Columns.** `outlineLevel` on an `XlsxColumn` groups columns the same way, for example monthly columns under a quarter.

Combine outline levels with the `indent` style so the hierarchy is visible even when expanded. [Grid Pro's Excel export](https://docs.birchui.com/grid-pro/excel-export.md) writes grouped grids exactly this way.

## Tab colour and direction

```ts
{ name: 'Overdue', rows, tabColor: '#D92D20' }
{ name: 'تقرير', rows, rightToLeft: true }
```

`tabColor` takes a hex colour. `rightToLeft` shows the sheet from right to left, with column A on the right, for Arabic, Hebrew and Persian content.

## Several sheets

A workbook can hold many sheets. The first is selected when the file opens.

```ts
const workbook: XlsxWorkbook = {
  sheets: [
    { name: 'Summary', rows: summaryRows, freeze: { rows: 1 } },
    { name: 'Orders', rows: orderRows, freeze: { rows: 1 }, autoFilter: true },
    { name: 'Returns', rows: returnRows, tabColor: '#F79009' },
  ],
};
```

Formulas can refer to other sheets in the usual Excel way, quoting names that contain spaces: `{ formula: "SUM('Order lines'!E2:E500)" }`.
