数据透视表右键菜单

SpreadJS 为数据透视表的不同区域提供了丰富的右键菜单选项,包括刷新、删除字段、值汇总依据、筛选、排序、展开/折叠、组合等操作。用户可以通过右键菜单快速执行数据透视表的常见操作,提升数据分析效率。

概述 本 Demo 展示了数据透视表的右键菜单功能。通过创建一个销售数据透视表,演示了 SpreadJS 如何在不同区域提供不同的右键菜单选项,帮助用户快速执行数据分析和报表操作。 实现思路 创建数据源工作表,包含销售日期、销售人员、汽车型号、数量、单价和总价等字段 在第二个工作表中创建数据透视表,使用 outline 布局和 light8 主题 添加行字段(Salesperson 和 Cars)和列字段(季度) 使用日期分组功能将日期字段按季度分组 添加值字段(Totals),使用求和汇总 应用标签筛选和排序,只显示指定的销售人员和汽车型号 设置数值格式为货币格式并自动调整列宽 代码解析 创建数据透视表 这段代码创建了一个名为 "PivotTable" 的数据透视表,使用 tableName 作为数据源,起始位置为第 1 行第 1 列。PivotTableLayoutType.outline 指定使用大纲布局,PivotTableThemes.light8 指定使用浅色主题。 添加字段到数据透视表 add 方法用于向数据透视表添加字段。第一个参数是源字段名,第二个参数是显示名称,第三个参数指定字段类型(rowField 为行字段,columnField 为列字段,valueField 为值字段)。对于值字段,第四个参数指定汇总方式(此处为求和)。 对日期字段进行分组 group 方法用于对字段进行分组。这里对日期字段按季度分组,originFieldName 指定原始字段名,dateGroups 指定分组方式。分组后会自动生成新的字段,可以在后续代码中使用该字段。 应用标签筛选和排序 labelFilter 方法用于对字段应用标签筛选。textItem.list 指定要显示的项,isAll: false 表示不显示全部。sort 方法用于对字段排序,sortType 指定排序方式(升序或降序)。 设置样式和自动调整列宽 setStyle 方法为数据透视表设置样式。{dataOnly: true} 指定只对数据区域应用样式,formatter 设置数字格式为货币格式。autoFitColumn 方法自动调整列宽以适应内容。 运行效果 在 PivotTable 工作表中,可以看到按销售人员和汽车型号分组的销售数据 数据按季度列显示,每个单元格显示总销售额(货币格式) 只显示指定的销售人员(Alan、John、Tess)和汽车型号(Audi、BMW、Mercedes) 右键点击数据透视表的不同区域,会显示相应的上下文菜单选项,如刷新、删除字段、值字段设置等 可以通过右键菜单快速操作数据透视表,例如刷新数据源、更改汇总方式、筛选数据等 API 参考 pivotTables.add() 方法 name:数据透视表名称,在工作簿中应唯一 sourceData:数据源,支持表名、集算表名或范围引用 row、col:起始行和列位置 layout:布局类型(可选) theme:主题样式(可选) options:数据透视表选项(可选) pivotTable.add() 方法 sourceName:源字段名称 displayName:显示名称 area:字段区域类型(rowField、columnField、valueField、filterField) subtotal:汇总类型(可选,仅用于值字段) index:字段位置索引(可选) pivotTable.group() 方法 groupInfo.originFieldName:源字段名称 groupInfo.dateGroups:日期分组信息数组,每项包含 by 属性指定分组类型 pivotTable.labelFilter() 方法 fieldName:字段名称 filterInfo.textItem.list:要显示的项列表 filterInfo.textItem.isAll:是否显示全部 pivotTable.sort() 方法 fieldName:字段名称 sortInfo.sortType:排序类型(GC.Pivot.SortType.asc 为升序,desc 为降序)
window.onload = function () { let spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 2}); initSpread(spread); }; function initSpread(spread) { spread.suspendPaint(); let sheet1 = spread.getSheet(0); let sheet2 = spread.getSheet(1); let tableName = getDataSource(sheet2, pivotSales); initPivotTable(sheet1, tableName); spread.resumePaint(); } function getDataSource(sheet, tableSource){ 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"); let table = sheet.tables.add('table', 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"]); sheet.setArray(0, 0, tableSource); return table.name(); } function initPivotTable(sheet, tableName){ sheet.name("PivotTable"); sheet.setRowCount(1000); let pivotTableOptions = {bandRows:true,bandColumns:true}; let pivotTable = sheet.pivotTables.add("PivotTable", tableName, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.light8, pivotTableOptions); pivotTable.suspendLayout(); pivotTable.add("salesperson", "Salesperson", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("car", "Cars", GC.Spread.Pivot.PivotTableFieldType.rowField); let groupInfo = { originFieldName: "date", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }] }; pivotTable.group(groupInfo); pivotTable.add("季度 (date)", "季度 (date)",GC.Spread.Pivot.PivotTableFieldType.columnField); pivotTable.add("total", "Totals", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); let itemList = ["Alan","John", "Tess"]; pivotTable.labelFilter("Salesperson", { textItem: { list: itemList, isAll: false } }); pivotTable.sort("Salesperson", { sortType: GC.Pivot.SortType.asc }); let carList = ["Audi","BMW","Mercedes"]; pivotTable.labelFilter("Cars", { textItem: { list: carList, isAll: false } }); pivotTable.sort("Cars", { sortType: GC.Pivot.SortType.asc }); let style = new GC.Spread.Sheets.Style(); style.formatter = "$ #,##0"; pivotTable.setStyle({dataOnly: true}, style); pivotTable.resumeLayout(); pivotTable.autoFitColumn(); }
<!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/js/license.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="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 id="container" class="options-container"> <div class="option-item"> <label><b>数据透视表右键菜单</b></label> </div> <hr> <div class="option-item"> <label>在数据透视表中选择一个单元格,然后右键单击鼠标以查看数据透视表右键菜单及其可用选项。</label> </div> <br><br> <div class="option-item"> <label>例如:点击<b>刷新</b>以刷新数据透视表数据源。</label> </div> </div> </div> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 300px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 300px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .option-item{ display: block; height: 20px; margin-bottom: 10px; }