[{"id":"dc3147d7-d3fb-4bc1-b1b3-142c0633e6d3","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"ca5ca1be-6d75-4ad1-880e-1589e3d52695","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"ea870089-5ea1-499a-bb0c-29cf378ead8d","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"d304d34f-f329-4f3b-b45e-2acb478427f8","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"3a79d24d-b853-4a51-a1e4-361feab06c57","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]},{"id":"5d31b3db-69bb-4388-9a57-450a63e44456","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]},{"id":"95e6d892-7bcd-4f6a-a152-504dec37c741","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"ec137f4b-3069-487f-bb0c-69f40fe6805d","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"9a2b2f20-5f3d-43a4-90d0-6f517fd3eebc","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]},{"id":"8d43dc09-ddd1-4c42-8b02-90765c4c705b","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"a0c3d1e1-4a95-4eaf-808e-93242f2f9aee","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]},{"id":"bd44c5b0-cd6b-443c-95dc-b8d8054fe1d7","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"fd5ccaa1-50b7-4c33-9ec5-c1eb48317119","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]},{"id":"ea61139a-0ecf-45ba-83c3-c2f8b20a4126","tags":[{"name":"New","color":"Red","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"c3c0ef13-c7a9-4649-9fcb-23b3abec175d"}]},{"id":"ca986d5e-dfda-46e9-84f3-f44e4248ab94","tags":[{"name":"Upd","color":"Blue","productId":"c15646e4-63d8-4965-8d62-7549a2986e99","links":null,"id":"2f377cfe-f512-431a-895b-f89af32041ce"}]}]
        
(Showing Draft Content)

GETPIVOTDATA

This function returns visible data from a Pivot Table. It queries the pivot table and gets the specific data based on the pivot table structure, instead of cell references. The main advantage of using this function is that it ensures that the correct data is returned, even if the pivot table layout is changed.

Syntax

=GETPIVOTDATA(data_field, pivot_table, [field1, item1, field2, item2], ...)

Arguments

This function has the following arguments:

Arguments

Descriptions

data_field

The name of the PivotTable field that contains the data you want to retrieve. This needs to be in double-quotes.

pivot_table

A reference to any cell, range of cells, or named range of cells in a PivotTable. This information is used to determine which PivotTable contains the data that you want to retrieve.

field1, item1, field2, item2..

[Optional] Up to 126 pairs of field names and item names that describe the data that you want to retrieve. The pairs can be in any order.

Usage

You can enter the GETPIVOTDATA function by using = (the equal sign) in the cell in which you want to return the value. You can also use this function with other functions.

Remarks

The following points hold true while working with the GETPIVOTTABLE function:

  • #REF! error value occurs in the following cases:

    • If the given pivot_table reference is not related to the pivot table.

    • If an invalid field for the data_field, [field], or [item] arguments is provided.

    • If the arguments do not describe a visible field, or if they include a report filter in which the filtered data is not displayed.

  • When there is a sigma value, the data_field will contain calc type, otherwise, it will use the field name only.

Return value

Returns visible data from a pivot table.

Examples

The following images show the usage of the GETPIVOTDATA function.




The following code sample shows the usage of the GETPIVOTDATA function.

$(document).ready(function () {
     // initializing Spread
     var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 2 });
     spread.suspendPaint();
     // get sheets
     var pivotLayoutSheet = spread.getSheet(0);
     var dataSourceSheet = spread.getSheet(1);
     // set sheet name
     pivotLayoutSheet.name("PivotLayout");
     dataSourceSheet.name("DataSource");
     // set row count
     dataSourceSheet.setRowCount(245);
     // set datasource
     dataSourceSheet.setArray(0, 0, pivotDB_UseCase);
     // add table to dataSourceSheet
     dataSourceSheet.tables.add('tableSales', 0, 0, 245, 8);
     spread.resumePaint();
     // initialize pivottable
     initPivotTable(pivotLayoutSheet);
     // auto fit columns in both the sheets
     autoFit(pivotLayoutSheet);
     autoFit(dataSourceSheet);
 });

 function initPivotTable(sheet) {
     // add pivottable
     var myPivotTable = sheet.pivotTables.add("myPivotTable", "tableSales", 0, 0, GC.Spread.Pivot.PivotTableLayoutType.tabular, GC.Spread.Pivot.PivotTableThemes.dark3);
     myPivotTable.suspendLayout();
     // show rowHeader and columnHeader for PivotTable
     myPivotTable.options.showRowHeader = true;
     myPivotTable.options.showColumnHeader = true;
     // add column fields
     myPivotTable.add("Category", "Category", GC.Spread.Pivot.PivotTableFieldType.columnField);
     // add row fields
     myPivotTable.add("Region", "Region", GC.Spread.Pivot.PivotTableFieldType.rowField);
     myPivotTable.add("City", "City", GC.Spread.Pivot.PivotTableFieldType.rowField);
     // add value field with SubtotalType Sum
     myPivotTable.add("Quantity", "Sum of quantity", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
     // add filter field
     myPivotTable.add("OrderDate", "OrderDate", GC.Spread.Pivot.PivotTableFieldType.filterField);
          
     var formula1 = '=GETPIVOTDATA("Quantity",$A$3)';
     sheet.setValue(3, 10, 'Total Quantity');
     sheet.setStyle(3, 10, 'introSec');
     sheet.setFormula(3, 11, formula1);

     var formula2 = '=GETPIVOTDATA("Quantity",$A$3,"Category","Bakery")';
     sheet.setValue(4, 10, 'Total Bakery Quantity');
     sheet.setStyle(4, 10, 'introSec');
     sheet.setFormula(4, 11, formula2);

     var formula3 = '=GETPIVOTDATA("Quantity",$A$3,"Region","West")';
     sheet.setValue(5, 10, 'West Region');
     sheet.setStyle(5, 10, 'introSec');
     sheet.setFormula(5, 11, formula3);

     var formula4 = '=GETPIVOTDATA("Quantity",$A$3,"Region","East","City","Jersey")';
     sheet.setValue(6, 10, 'Quantity in Jersey');
     sheet.setStyle(6, 10, 'introSec');
     sheet.setFormula(6, 11, formula4);
     myPivotTable.resumeLayout();
     return myPivotTable;
 }

 function autoFit(sheet) {
    // auto fit columns
    let columnCount = sheet.getColumnCount();
    for (let i = 0; i < columnCount; i++) {
        sheet.autoFitColumn(i);
    }
}

The GETPIVOTDATA function also supports spilling, which means, it can populate multiple cells with its results. However, the allowDynamicArray property should be set to true to observe the spilling results. The below image shows a pivot table that will be used to extract some meaningful results using the GETPIVOTDATA function.


pivottable_spill.png


The below image shows the output when the GETPIVOTDATA function is used in the above pivot table and the result is spilled to multiple cells.


pivottable_spill_results.png


The following code sample shows the usage of the GETPIVOTDATA function to get results that spill to multiple cells.

function setGetPivotDataFunction(sheet) {
    spread.options.allowDynamicArray = true;
    var formula = '=GETPIVOTDATA("Sum of quantity",$B$2,"City",{"Jersey";"San Francisco";"Seattle";"Washington, DC"},"Category",C11:C14,"Qt",E3:F3)';
    sheet.setStyle(1, 9, 'introSec');
    sheet.addSpan(1, 9, 2, 8, GC.Spread.Sheets.SheetArea.viewport);
    sheet.setValue(1, 9, 'Returns the spill range of total Bakery, Beverages, Chocolates and Snacks quantity sold in Jersey, San Francisco, Seattle and Washington, DC respectively in the Qtr2 & Qtr3');
    sheet.getCell(1, 9).wordWrap(true);
    sheet.setStyle(4, 9, 'formula');
    sheet.addSpan(4, 9, 2, 8, GC.Spread.Sheets.SheetArea.viewport);
    sheet.getCell(4, 9).wordWrap(true);
    sheet.setValue(4, 9, formula);
    sheet.setFormula(7, 9, formula);
}