GETPIVOTDATA

GETPIVOTDATA函数查询数据透视表并根据数据透视表结构而不是单元格引用返回特定数据。使用此函数的主要优点是它确保返回正确的数据,即使数据透视表布局发生变化。

语法 参数 描述 data_field (必须) 包含要检索数据的透视表字段的名称。 这需要用双引号引起来。 pivot_table (必须) 数据透视表中对任何单元格,单元格范围或命名单元格范围的引用。 此信息用于确定哪个数据透视表包含要检索的数据。 field1, item1, field2, item2… (可选) 1至126对用来描述您要检索的数据的字段名称和项目名称。配对可以是任何顺序。 日期和数字之外的项目名称需要用引号引起来。 使用说明 GETPIVOTDATA函数返回一个数据透视表中可见的数据。 要从数据透视表中的单元格提取数据,可以输入常规的单元格链接,例如=B5,或者你可以使用GetPivotData公式,该公式是专门为从数据透视表中获取数据而设计的。当你引用的单元格在数据透视表中时,该公式会自动被创建。 你还可以通过工作簿中的pivotAreaReference选项选择是显示GETPIVOTDATA公式还是显示正常的单元格引用 优点 使用GETPIVOTDATA函数的优点是,即使数据透视表的布局发生了改变,它也会使用规则来确保返回正确的数据。 扩展 GETPIVOTDATA函数支持溢出,它支持你根据你定义的字段和项目参考来获得一系列的透视数据。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 2 }); initSpread(spread); var pivotLayoutSheet = spread.getSheet(0); initPivotTable(pivotLayoutSheet); initStyles(spread); setGetPivotDataFunction(pivotLayoutSheet); }; function initSpread(spread) { spread.suspendPaint(); spread.options.allowDynamicArray = true; var sheet = spread.getSheet(1); sheet.name("DataSource"); sheet.setRowCount(117); sheet.setColumnWidth(0, 120); sheet.getCell(-1, 0).formatter("YYYY-mm-DD"); sheet.getRange(-1,4,0,2).formatter("$ #,##0"); sheet.setArray(0, 0, pivotSales); let table = sheet.tables.add('tableSales', 0, 0, 117, 6); for(let i=2;i<=117;i++) { sheet.setFormula(i-1,5,'=D'+i+'*E'+i) } table.style(GC.Spread.Sheets.Tables.TableThemes["none"]); var sheet0 = spread.getSheet(0); sheet0.name("PivotLayout"); spread.resumePaint(); } function initPivotTable(sheet) { var myPivotTable = sheet.pivotTables.add("myPivotTable", "tableSales", 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.light8); myPivotTable.suspendLayout(); myPivotTable.options.showRowHeader = true; myPivotTable.options.showColumnHeader = true; myPivotTable.add("salesperson", "Salesperson", GC.Spread.Pivot.PivotTableFieldType.rowField); myPivotTable.add("car", "Cars", GC.Spread.Pivot.PivotTableFieldType.rowField); myPivotTable.add("date", "Qt", GC.Spread.Pivot.PivotTableFieldType.columnField); let groupInfo = { originFieldName: "date", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }] }; myPivotTable.group(groupInfo); myPivotTable.add("total", "Totals", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); let itemList = ["Alan","John", "Tess"]; myPivotTable.labelFilter("Salesperson", { textItem: { list: itemList, isAll: false } }); myPivotTable.sort("Salesperson", { sortType: GC.Pivot.SortType.asc }); let carList = ["Audi","BMW","Mercedes"]; myPivotTable.labelFilter("Cars", { textItem: { list: carList, isAll: false } }); myPivotTable.sort("Cars", { sortType: GC.Pivot.SortType.asc }); let style = new GC.Spread.Sheets.Style(); style.formatter = "$ #,##0"; myPivotTable.setStyle({dataOnly: true}, style); myPivotTable.resumeLayout(); myPivotTable.autoFitColumn(); } function setGetPivotDataFunction(sheet) { sheet.setColumnWidth(8, 20); sheet.setValue(1, 9, 'GETPIVOTDATA basic usage'); sheet.setStyle(1, 9, 'intro'); var formula1 = '=GETPIVOTDATA("Totals",$B$2,"Salesperson","Tess","Cars","Audi","Qt","季度2")'; sheet.setValue(3, 9, 'Returns the total Audi sales made by Tess in the second quarter'); sheet.setStyle(3, 9, 'introSec'); sheet.setValue(4, 9, formula1); sheet.setStyle(4, 9, 'formula'); sheet.getCell(5,9).formatter("$ #,##0"); sheet.setFormula(5, 9, formula1); sheet.setStyle(5, 9, 'result'); var formula2 = '=GETPIVOTDATA("Totals",$B$2,"Salesperson","Alan","Cars","BMW")'; sheet.setValue(7, 9, 'Returns the total BMW sales made by Alan'); sheet.setStyle(7, 9, 'introSec'); sheet.setValue(8, 9, formula2); sheet.setStyle(8, 9, 'formula'); sheet.getCell(9,9).formatter("$ #,##0"); sheet.setFormula(9, 9, formula2); sheet.setStyle(9, 9, 'result'); // Invoked formula var formula3 = '=GETPIVOTDATA("Totals",$B$2,"Qt","季度1")'; sheet.setValue(11, 9, 'Returns the total sales for the first quarter'); sheet.setStyle(11, 9, 'introSec'); sheet.setValue(12, 9, formula3); sheet.setStyle(12, 9, 'formula'); sheet.getCell(13,9).formatter("$ #,##0"); sheet.setFormula(13, 9, formula3); sheet.setStyle(13, 9, 'result'); var formula4 = '=GETPIVOTDATA("Totals",$B$2)'; sheet.setValue(15, 9, 'Returns the grand total'); sheet.setStyle(15, 9, 'introSec'); sheet.setValue(16, 9, formula4); sheet.setStyle(16, 9, 'formula'); sheet.getCell(17,9).formatter("$ #,##0"); sheet.setFormula(17, 9, formula4); sheet.setStyle(17, 9, 'result'); var formula5 = '=GETPIVOTDATA("Totals",$B$2,"Salesperson",{"Alan";"John";"Tess"},"Cars",C10:C12,"Qt",E3:G3)'; sheet.setValue(19, 9, 'Returns the spill range of total Audi/BMW/Mercedes sales made by Alan/John/Tess in the Qtr2/Qtr3/Qtr4 quarter'); sheet.setStyle(19, 9, 'introSec'); sheet.setValue(20, 9, formula5); sheet.setStyle(20, 9, 'formula'); sheet.setFormula(21, 9, formula5); sheet.getRange(21, 9, 3, 3).hAlign(0); } function initStyles(spread) { var introStyle = new GC.Spread.Sheets.Style(); introStyle.name = 'intro'; introStyle.font = 'normal bold 13px Segoe UI'; introStyle.foreColor = "#172b4d"; spread.addNamedStyle(introStyle); var introSecStyle = new GC.Spread.Sheets.Style(); introSecStyle.name = 'introSec'; introSecStyle.font = 'normal bold 12px Segoe UI'; introSecStyle.foreColor = "#000"; spread.addNamedStyle(introSecStyle); var formulaStyle = new GC.Spread.Sheets.Style(); formulaStyle.name = 'formula'; formulaStyle.font = 'normal bold 12px Consolas'; formulaStyle.foreColor = "#c00000"; spread.addNamedStyle(formulaStyle); var resultStyle = new GC.Spread.Sheets.Style(); resultStyle.name = 'result'; resultStyle.hAlign = 0; spread.addNamedStyle(resultStyle); } function _getElementById(id) { return document.getElementById(id); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/pivot-data.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 210px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #app { height: 100%; }