TX Sheet Control

GETPIVOTDATA Function

Function

GETPIVOTDATA reads a visible value from a PivotTable in the workbook. It follows the report's filters, expanded groups and Show Values As calculation. No external formula-data provider is required.

Syntax

GETPIVOTDATA(data_field, pivot_table, [field1, item1], ...)

Category: Lookup. Minimum arguments: 2. Maximum arguments: 254, including up to 126 field/item pairs.

ArgumentDescription
data_fieldValue-field caption or source-field name, such as "Sales" or "Revenue".
pivot_tableA cell or rectangular reference that intersects the PivotTable output.
field1, item1, ...Row, column or visible single-selection report-filter fields and their members. Field names and configured axis captions are matched without regard to case. Member values retain their data types.

Return Value

The displayed value at the requested intersection, including percentages and other Show Values As calculations. A displayed text replacement, error or blank retains that result instead of becoming an unformatted source aggregate.

With no field/item pairs, the function reads the displayed grand-total intersection. A parent path reads a displayed subtotal or collapsed group total. #REF! is returned when the requested intersection is absent, including details hidden by collapsing a parent, filtered-out members and disabled subtotal or grand-total cells. Repeating a field in the arguments also returns #REF!.

Examples

Run this browser example after editor.ready. It creates a sales report with an amount and a share, then places formulas beside the source data.

import { Spreadsheet } from '@txtextcontrol/tx-sheet-control';
const host = document.createElement('div');
host.style.height = '600px';
document.body.append(host);
const editor = new Spreadsheet(host, { license: 'YOUR_SIGNED_LICENSE', fontCatalogURL: '/assets/sheet/fonts/catalog.json' });
await editor.ready;

const workbook = editor.workbook;
const source = workbook.sheets.add('Pivot Orders');
source.range('A1:C4').setValues([
    ['Region', 'Category', 'Revenue'],
    ['Europe', 'Software', 100],
    ['Europe', 'Services', 200],
    ['Americas', 'Software', 300]
]);
const report = workbook.sheets.add('Pivot Summary');
const pivot = report.pivotTables.add({
    name: 'RegionalSales',
    source: { sheetId: source.id, range: 'A1:C4' },
    destination: { sheetId: report.id, cell: 'B3' },
    rows: [{ field: 'Region' }],
    values: [
        { field: 'Revenue', caption: 'Sales' },
        { field: 'Revenue', caption: 'Share', showAs: 'percentGrandTotal' }
    ]
});
source.cell('E1').setFormula('=GETPIVOTDATA("Sales",\'Pivot Summary\'!B3,"Region","Europe")');
source.cell('E2').setFormula('=GETPIVOTDATA("Share",\'Pivot Summary\'!B3,"Region","Europe")');
source.cell('E2').format({ numberFormatCode: '0.0%' });

source.cell('C2').setValue(200);
pivot.refresh();

The first formula initially returns 300; the second returns 0.5, displayed as 50.0%. After the source edit and refresh, they return 400 and 400 / 700. The report and its formulas use the last refreshed cache until you call refresh() or enable autoRefresh.

Remarks

PivotTable.getData returns the underlying aggregate, including for a collapsed path. Use GETPIVOTDATA when the formula should follow the value and visibility presented in the report.

When a rectangular reference intersects several PivotTables, the most recently created matching report is used. Grouped fields use the members returned by getItems(); use numeric date serials for ungrouped numeric date values.

See Also

Replace YOUR_SIGNED_LICENSE with your issued Trial, Developer or OEM token before running the editor example.