modern-xlsx API Reference - v1.3.0
    Preparing search index...

    Class PivotTableBuilder

    Fluent builder for constructing PivotTableData objects.

    Every pivot field is built with empty items. On write, the row, column and page fields get the items Excel needs from the cache that cacheId names, and that cache refreshes when Excel opens the file, which lays the table out and shows its values (see PivotTableData). With two or more data fields the writer also puts the values field on the columns.

    The builder makes pivot fields only up to the highest fieldIndex used. On write, pivotFields gets empty fields up to the number of fields in the cache that cacheId names, because Excel refuses a table with fewer.

    ws.addPivotTableFromBuilder((b) => {
    b.name('SalesPivot')
    .cacheId(0)
    .location('A3:D20')
    .addRowField({ fieldIndex: 0, name: 'Region' })
    .addColField({ fieldIndex: 1, name: 'Product' })
    .addDataField({ fieldIndex: 2, subtotal: 'sum', name: 'Total Revenue' })
    .addPageField({ fieldIndex: 3, name: 'Year' });
    });
    Index
    • Set the location reference (e.g., 'A3:D20' or 'A3').

      Parameters

      • ref: string

        The A1-style range or cell reference.

      • Optionaloptions: { firstDataCol?: number; firstDataRow?: number; firstHeaderRow?: number }

        Optional first header row, data row, and data column offsets.

      Returns this

      this for chaining.