# @salmonui/export/pivot API reference

> 20 exported symbols of @salmonui/export/pivot: 5 functions, 12 interfaces and 3 types.

Canonical: https://salmonui.dev/docs/reference/export/pivot

```ts
import { … } from '@salmonui/export/pivot';
```

## Functions

### createPivotExportModel

Kind: function

Page: https://salmonui.dev/docs/reference/export/pivot/createPivotExportModel

```ts
function createPivotExportModel(result: PivotExportResult, options?: PivotExportOptions, numberFormat?: PivotNumberFormatCode): ExportModel<number>;
```

The export model of a pivot result: `writeXlsx`, `writePdf`, `writeCsv`
and `streamCsv` accept it. `numberFormat` turns the model's `formats` into
Excel codes (the XLSX entry passes the writer's converter).

### exportPivot

Kind: function

Page: https://salmonui.dev/docs/reference/export/pivot/exportPivot

```ts
function exportPivot(source: PivotExportSource, options: PivotFileExportOptions & {
    readonly format: 'csv';
}, handlers?: PivotExportHandlers<string>): Promise<ExportFile<string> | null>;

function exportPivot(source: PivotExportSource | readonly PivotExportSource[], options?: PivotFileExportOptions & {
    readonly format?: 'xlsx' | 'pdf';
}, handlers?: PivotExportHandlers): Promise<ExportFile | null>;

function exportPivot(source: PivotExportSource | readonly PivotExportSource[], options?: PivotFileExportOptions, handlers?: PivotExportHandlers<Uint8Array | string>): Promise<ExportFile<Uint8Array | string> | null>;
```

Exports a pivot result (or a window source, paged) as XLSX
(default; several results become one sheet each, or `xlsx.layout:
'stacked'`), PDF or CSV. XLSX: an outlined sheet with stacked merged column
headers, row outline levels, number formats from `model.formats`, subtotal
and grand total styles, conditional formats and the `cell` hook.
PDF: the same table with the column headers repeated on every page, then one page per `chart` image. CSV: one header line of column paths (`2024 / Sum of Amount`,
the labels of every column header level joined), then raw numbers, or the
formatted texts with `csv.values: 'formatted'`. Resolves to
`{ fileName, mimeType, data }`, or null when `onBeforeExport` prevents it.

### isPivotWindowSource

Kind: function

Page: https://salmonui.dev/docs/reference/export/pivot/isPivotWindowSource

```ts
function isPivotWindowSource(source: PivotExportSource): source is PivotWindowSource;
```

### loadPivotResult

Kind: function

Page: https://salmonui.dev/docs/reference/export/pivot/loadPivotResult

```ts
function loadPivotResult(source: PivotWindowSource, options?: {
    readonly windowCells?: number;
    readonly signal?: unknown;
}): Promise<PivotExportResult>;
```

Pages a window source into one in-memory result: all columns, row windows
of `windowCells` cells. Values are kept as numbers (8 bytes per cell) and
the cell text is kept per distinct value and measure.

### streamPivotCsv

Kind: function

Page: https://salmonui.dev/docs/reference/export/pivot/streamPivotCsv

```ts
function streamPivotCsv(source: PivotExportSource, options?: PivotCsvStreamOptions): AsyncGenerator<string, void, undefined>;
```

Streams a pivot as CSV chunks (large results). A result streams
through `streamCsv`; a window source is paged one row window at a time and
never held whole.

## Interfaces

### PivotCsvStreamOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotCsvStreamOptions

```ts
interface PivotCsvStreamOptions extends PivotExportOptions, CsvStreamOptions {
    readonly signal?: unknown;
}
```

Members (27):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `signal` | `readonly signal?: unknown;` | optional |  |
| `delimiter` | `delimiter?: string;` | optional, inherited | Default `,`. |
| `lineBreak` | `lineBreak?: string;` | optional, inherited | Default `\r\n` (RFC 4180). |
| `includeHeaders` | `includeHeaders?: boolean;` | optional, inherited | Header line with the leaf header texts. Default true. |
| `values` | `values?: "formatted" \| "raw";` | optional, inherited | `formatted` (default): the column display text. `raw`: numbers and booleans as is, dates as ISO 8601. |
| `aggregateRows` | `aggregateRows?: boolean;` | optional, inherited | Group caption, group footer, and grid footer aggregate lines. Default true. |
| `bom` | `bom?: boolean;` | optional, inherited | Prefix a UTF-8 byte order mark (Excel detects UTF-8). Default false. |
| `quoteAll` | `quoteAll?: boolean;` | optional, inherited | Quote every field instead of only those that need it. Default false. |
| `escapeFormulas` | `escapeFormulas?: boolean;` | optional, inherited | Neutralize spreadsheet formulas in text cells: a leading `'` on tab, CR, or `=`, `+`, `-`, `@` after optional whitespace. Real numbers are kept. Default true; `false` writes raw text. |
| `treeDepthColumn` | `treeDepthColumn?: string \| boolean;` | optional, inherited | Tree mode: a leading column with each row's tree depth (0 for roots). `true` names it `Depth`; a string is the header text. Ignored outside tree mode. |
| `chunkRows` | `chunkRows?: number;` | optional, inherited | Upper bound of rows per chunk. Default 5,000. |
| `sliceMs` | `sliceMs?: number;` | optional, inherited | Time slice per chunk in ms; a chunk ends early when it runs longer. Default 25. |
| `yield` | `yield?: (() => Promise<void>);` | optional, inherited | Awaited between chunks. Default: a macrotask (`setTimeout(0)`) when available, else a resolved promise. |
| `fileName` | `fileName?: string;` | optional, inherited | File name; the extension is added. Default `export`. |
| `rowLayout` | `rowLayout?: "compact" \| "tabular";` | optional, inherited | `compact` (default): one indented row header column; `tabular`: one column per row level. |
| `rowHeaders` | `rowHeaders?: boolean;` | optional, inherited | Row header columns. Default true. |
| `rowHeaderCaption` | `rowHeaderCaption?: string;` | optional, inherited | Header text of the row header column(s). Default: the row field captions. |
| `rowHeaderWidth` | `rowHeaderWidth?: number;` | optional, inherited | Column widths in CSS px. Defaults 200 (row headers) and 110. |
| `columnWidth` | `columnWidth?: number;` | optional, inherited |  |
| `includeColumn` | `includeColumn?: ((header: PivotExportHeader, index: number) => boolean);` | optional, inherited | Exported value columns (hidden columns, current page only). Default every leaf column. |
| `rowRange` | `rowRange?: readonly [number, number];` | optional, inherited | Exported leaf row positions `[start, end)` (current page only). Default every row. |
| `includeRow` | `includeRow?: ((header: PivotExportHeader, index: number) => boolean);` | optional, inherited | Exported rows. Default every visible leaf row. |
| `pivotStyles` | `pivotStyles?: PivotExportStyles;` | optional, inherited | Subtotal, grand total and row header styles (merged under conditional formats and the hook). |
| `cell` | `cell?: ((ctx: PivotExportCellContext) => void \| ExportCellOverride \| null \| undefined);` | optional, inherited | Per body cell. |
| `headerCell` | `headerCell?: ((ctx: PivotExportHeaderContext) => void \| { readonly text?: string; readonly style?: ExportCellStyle; } \| null \| undefined);` | optional, inherited | Per header cell. |
| `windowCells` | `windowCells?: number;` | optional, inherited | Window sources: cells per `pivot` request while paging. Default 200,000. |
| `locale` | `locale?: string;` | optional, inherited | Locale of the model (number formats in PDF / CSV come from `formattedValue`). |

### PivotExportCell

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportCell

```ts
interface PivotExportCell {
    readonly rowIndex: number;
    readonly columnIndex: number;
    readonly measure: string | null;
    readonly value: number | null;
    readonly formattedValue: string;
    readonly kind: 'value' | 'subtotal' | 'grandTotal';
}
```

A value cell (`PivotCell`).

Members (6):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `rowIndex` | `readonly rowIndex: number;` |  |  |
| `columnIndex` | `readonly columnIndex: number;` |  |  |
| `measure` | `readonly measure: string \| null;` |  |  |
| `value` | `readonly value: number \| null;` |  |  |
| `formattedValue` | `readonly formattedValue: string;` |  |  |
| `kind` | `readonly kind: 'value' \| 'subtotal' \| 'grandTotal';` |  |  |

### PivotExportCellContext

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportCellContext

```ts
interface PivotExportCellContext {
    readonly row: PivotExportHeader;
    readonly column: PivotExportHeader | null;
    readonly pivotCell: PivotExportCell | null;
    readonly level: number;
    readonly cell: ExportCell;
}
```

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `row` | `readonly row: PivotExportHeader;` |  | Leaf row and column headers; `column` is null in row header columns. |
| `column` | `readonly column: PivotExportHeader \| null;` |  |  |
| `pivotCell` | `readonly pivotCell: PivotExportCell \| null;` |  | The value cell; null in row header columns. |
| `level` | `readonly level: number;` |  | Row header level (tabular layout) or -1 for value cells. |
| `cell` | `readonly cell: ExportCell;` |  | The computed export cell. |

### PivotExportHandlers

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportHandlers

```ts
interface PivotExportHandlers<Data = Uint8Array> {
    readonly onBeforeExport?: (event: BeforeExportEvent) => void;
    readonly onExport?: (event: ExportEvent<Data>) => void;
}
```

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `onBeforeExport` | `readonly onBeforeExport?: (event: BeforeExportEvent) => void;` | optional |  |
| `onExport` | `readonly onExport?: (event: ExportEvent<Data>) => void;` | optional |  |

### PivotExportHeader

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportHeader

```ts
interface PivotExportHeader {
    readonly key: string;
    readonly kind: 'member' | 'subtotal' | 'grandTotal' | 'value';
    readonly path: readonly JsonLike[];
    readonly field: string | null;
    readonly measure: string | null;
    readonly label: string;
    readonly depth: number;
    readonly position: number;
    readonly span: number;
    readonly children?: readonly PivotExportHeader[];
}
```

A header node (`PivotHeaderNode`).

Members (10):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `key` | `readonly key: string;` |  |  |
| `kind` | `readonly kind: 'member' \| 'subtotal' \| 'grandTotal' \| 'value';` |  |  |
| `path` | `readonly path: readonly JsonLike[];` |  |  |
| `field` | `readonly field: string \| null;` |  |  |
| `measure` | `readonly measure: string \| null;` |  |  |
| `label` | `readonly label: string;` |  |  |
| `depth` | `readonly depth: number;` |  |  |
| `position` | `readonly position: number;` |  |  |
| `span` | `readonly span: number;` |  |  |
| `children` | `readonly children?: readonly PivotExportHeader[];` | optional |  |

### PivotExportHeaderContext

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportHeaderContext

```ts
interface PivotExportHeaderContext {
    readonly header: PivotExportHeader | null;
    readonly text: string;
    readonly level: number;
    readonly colIndex: number;
}
```

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `header` | `readonly header: PivotExportHeader \| null;` |  | The column header node; null for the row header captions. |
| `text` | `readonly text: string;` |  |  |
| `level` | `readonly level: number;` |  |  |
| `colIndex` | `readonly colIndex: number;` |  |  |

### PivotExportModelLike

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportModelLike

```ts
interface PivotExportModelLike {
    readonly rows?: readonly {
        readonly field: string;
        readonly caption?: string;
    }[];
    readonly formats?: unknown;
    readonly conditionalFormats?: unknown;
}
```

The part of `PivotModel` the export reads.

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `rows` | `readonly rows?: readonly {     readonly field: string;     readonly caption?: string; }[];` | optional |  |
| `formats` | `readonly formats?: unknown;` | optional |  |
| `conditionalFormats` | `readonly conditionalFormats?: unknown;` | optional |  |

### PivotExportOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportOptions

```ts
interface PivotExportOptions {
    readonly fileName?: string;
    readonly rowLayout?: 'compact' | 'tabular';
    readonly rowHeaders?: boolean;
    readonly rowHeaderCaption?: string;
    readonly rowHeaderWidth?: number;
    readonly columnWidth?: number;
    readonly includeColumn?: (header: PivotExportHeader, index: number) => boolean;
    readonly rowRange?: readonly [
        number,
        number
    ];
    readonly includeRow?: (header: PivotExportHeader, index: number) => boolean;
    readonly pivotStyles?: PivotExportStyles;
    readonly cell?: (ctx: PivotExportCellContext) => ExportCellOverride | null | undefined | void;
    readonly headerCell?: (ctx: PivotExportHeaderContext) => {
        readonly text?: string;
        readonly style?: ExportCellStyle;
    } | null | undefined | void;
    readonly windowCells?: number;
    readonly locale?: string;
}
```

Options of every pivot export format.

Members (14):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `fileName` | `readonly fileName?: string;` | optional | File name; the extension is added. Default `export`. |
| `rowLayout` | `readonly rowLayout?: 'compact' \| 'tabular';` | optional | `compact` (default): one indented row header column; `tabular`: one column per row level. |
| `rowHeaders` | `readonly rowHeaders?: boolean;` | optional | Row header columns. Default true. |
| `rowHeaderCaption` | `readonly rowHeaderCaption?: string;` | optional | Header text of the row header column(s). Default: the row field captions. |
| `rowHeaderWidth` | `readonly rowHeaderWidth?: number;` | optional | Column widths in CSS px. Defaults 200 (row headers) and 110. |
| `columnWidth` | `readonly columnWidth?: number;` | optional |  |
| `includeColumn` | `readonly includeColumn?: (header: PivotExportHeader, index: number) => boolean;` | optional | Exported value columns (hidden columns, current page only). Default every leaf column. |
| `rowRange` | `readonly rowRange?: readonly [     number,     number ];` | optional | Exported leaf row positions `[start, end)` (current page only). Default every row. |
| `includeRow` | `readonly includeRow?: (header: PivotExportHeader, index: number) => boolean;` | optional | Exported rows. Default every visible leaf row. |
| `pivotStyles` | `readonly pivotStyles?: PivotExportStyles;` | optional | Subtotal, grand total and row header styles (merged under conditional formats and the hook). |
| `cell` | `readonly cell?: (ctx: PivotExportCellContext) => ExportCellOverride \| null \| undefined \| void;` | optional | Per body cell. |
| `headerCell` | `readonly headerCell?: (ctx: PivotExportHeaderContext) => {     readonly text?: string;     readonly style?: ExportCellStyle; } \| null \| undefined \| void;` | optional | Per header cell. |
| `windowCells` | `readonly windowCells?: number;` | optional | Window sources: cells per `pivot` request while paging. Default 200,000. |
| `locale` | `readonly locale?: string;` | optional | Locale of the model (number formats in PDF / CSV come from `formattedValue`). |

### PivotExportResult

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportResult

```ts
interface PivotExportResult {
    readonly model: PivotExportModelLike;
    readonly rowCount: number;
    readonly columnCount: number;
    readonly rowDepth: number;
    readonly columnDepth: number;
    readonly rowTree: readonly PivotExportHeader[];
    readonly columnTree: readonly PivotExportHeader[];
    memberAt(axis: 'rows' | 'columns', index: number): PivotExportHeader;
    cellAt(rowIndex: number, columnIndex: number): PivotExportCell;
}
```

A pivot result (`PivotResult`, a remote snapshot, or a reference handler `compute()` result).

Members (9):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `model` | `readonly model: PivotExportModelLike;` |  |  |
| `rowCount` | `readonly rowCount: number;` |  |  |
| `columnCount` | `readonly columnCount: number;` |  |  |
| `rowDepth` | `readonly rowDepth: number;` |  |  |
| `columnDepth` | `readonly columnDepth: number;` |  |  |
| `rowTree` | `readonly rowTree: readonly PivotExportHeader[];` |  |  |
| `columnTree` | `readonly columnTree: readonly PivotExportHeader[];` |  |  |
| `memberAt` | `memberAt(axis: 'rows' \| 'columns', index: number): PivotExportHeader;` |  |  |
| `cellAt` | `cellAt(rowIndex: number, columnIndex: number): PivotExportCell;` |  |  |

### PivotExportStyles

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportStyles

```ts
interface PivotExportStyles {
    readonly rowHeader?: ExportCellStyle;
    readonly subtotal?: ExportCellStyle;
    readonly grandTotal?: ExportCellStyle;
}
```

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `rowHeader` | `readonly rowHeader?: ExportCellStyle;` | optional |  |
| `subtotal` | `readonly subtotal?: ExportCellStyle;` | optional |  |
| `grandTotal` | `readonly grandTotal?: ExportCellStyle;` | optional |  |

### PivotFileExportOptions

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotFileExportOptions

```ts
interface PivotFileExportOptions extends PivotExportOptions {
    readonly format?: PivotExportFormat;
    readonly xlsx?: XlsxOptions;
    readonly pdf?: PdfOptions;
    readonly csv?: CsvOptions;
    readonly chart?: ExportImage | readonly ExportImage[];
}
```

`exportPivot()` options: the pivot options, the format, and that format's writer options.

Members (19):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `format` | `readonly format?: PivotExportFormat;` | optional | Default `xlsx`. |
| `xlsx` | `readonly xlsx?: XlsxOptions;` | optional | XLSX writer options (sheet names, `layout: 'stacked'` for several results on one sheet, header and footer rows, styles). |
| `pdf` | `readonly pdf?: PdfOptions;` | optional | PDF writer options (page size, orientation, font, page header and footer). |
| `csv` | `readonly csv?: CsvOptions;` | optional | CSV options (delimiter, BOM; `values: 'formatted'` writes the formatted texts instead of the raw numbers). |
| `chart` | `readonly chart?: ExportImage \| readonly ExportImage[];` | optional | PDF only: chart image(s) (PNG or JPEG bytes with their CSS px size, e.g. from `Chart`'s `toBlob('png')`), each on its own page after the table: table and chart in one PDF. Same as `pdf.appendImages`. |
| `fileName` | `fileName?: string;` | optional, inherited | File name; the extension is added. Default `export`. |
| `rowLayout` | `rowLayout?: "compact" \| "tabular";` | optional, inherited | `compact` (default): one indented row header column; `tabular`: one column per row level. |
| `rowHeaders` | `rowHeaders?: boolean;` | optional, inherited | Row header columns. Default true. |
| `rowHeaderCaption` | `rowHeaderCaption?: string;` | optional, inherited | Header text of the row header column(s). Default: the row field captions. |
| `rowHeaderWidth` | `rowHeaderWidth?: number;` | optional, inherited | Column widths in CSS px. Defaults 200 (row headers) and 110. |
| `columnWidth` | `columnWidth?: number;` | optional, inherited |  |
| `includeColumn` | `includeColumn?: ((header: PivotExportHeader, index: number) => boolean);` | optional, inherited | Exported value columns (hidden columns, current page only). Default every leaf column. |
| `rowRange` | `rowRange?: readonly [number, number];` | optional, inherited | Exported leaf row positions `[start, end)` (current page only). Default every row. |
| `includeRow` | `includeRow?: ((header: PivotExportHeader, index: number) => boolean);` | optional, inherited | Exported rows. Default every visible leaf row. |
| `pivotStyles` | `pivotStyles?: PivotExportStyles;` | optional, inherited | Subtotal, grand total and row header styles (merged under conditional formats and the hook). |
| `cell` | `cell?: ((ctx: PivotExportCellContext) => void \| ExportCellOverride \| null \| undefined);` | optional, inherited | Per body cell. |
| `headerCell` | `headerCell?: ((ctx: PivotExportHeaderContext) => void \| { readonly text?: string; readonly style?: ExportCellStyle; } \| null \| undefined);` | optional, inherited | Per header cell. |
| `windowCells` | `windowCells?: number;` | optional, inherited | Window sources: cells per `pivot` request while paging. Default 200,000. |
| `locale` | `locale?: string;` | optional, inherited | Locale of the model (number formats in PDF / CSV come from `formattedValue`). |

### PivotWindowSource

Kind: interface

Page: https://salmonui.dev/docs/reference/export/pivot/PivotWindowSource

```ts
interface PivotWindowSource {
    loadWindow(range: {
        readonly rows: readonly [
            number,
            number
        ];
        readonly columns: readonly [
            number,
            number
        ];
    }, options?: {
        readonly signal?: unknown;
    }): Promise<PivotExportResult>;
}
```

A remote result paged by window (`RemotePivotEngine` of `@salmonui/data`).

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `loadWindow` | `loadWindow(range: {     readonly rows: readonly [         number,         number     ];     readonly columns: readonly [         number,         number     ]; }, options?: {     readonly signal?: unknown; }): Promise<PivotExportResult>;` |  |  |

## Types

### PivotExportFormat

Kind: type

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportFormat

```ts
type PivotExportFormat = 'xlsx' | 'pdf' | 'csv';
```

### PivotExportSource

Kind: type

Page: https://salmonui.dev/docs/reference/export/pivot/PivotExportSource

```ts
type PivotExportSource = PivotExportResult | PivotWindowSource;
```

### PivotNumberFormatCode

Kind: type

Page: https://salmonui.dev/docs/reference/export/pivot/PivotNumberFormatCode

```ts
type PivotNumberFormatCode = (options: Intl.NumberFormatOptions) => string | number | null;
```

Excel number format code of `Intl.NumberFormat` options (supplied by the XLSX writer).
