透视表排序

SpreadJS 数据透视表支持数据排序。这个函数可以用来排序和组织数据。

概括 Pivot Table可以通过4种方式对字段进行排序: Sort By Field Item Name Sort By Value Sort By Custom Field Item Value Sort By Custom Callback SortType可以以任何方式设置。 API 接口 API 示例
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> </gc-spread-sheets> <div class="options-container" style="float:right; width: 380px;"> <div> <div v-for="(option, index) in options" :key="index" class="options-row"> <span v-if="option.type==='text'">{{option.text}}</span> <span v-if="option.type==='select'">{{option.text}}</span> <select v-if="option.type==='select'" :id="option.id" v-model="option.selected"> <option v-for="item in option.items" :key="item" :value="item">{{item}}</option> </select> <input v-else-if="option.type==='input'" :id="option.id" type="text" v-model="option.value"> <div v-if="option.type==='radio-group'"> <span>{{option.text}}</span> <label v-for="(item, i) in option.items" :key="i"> <input type="radio" :name="option.id" :id="option.id+i" :value="item.value" v-model="option.selected"> {{item.label}} </label> </div> <button v-else-if="option.type==='button'" :id="option.id" @click="option.click">{{option.text}}</button> <div v-else-if="option.type==='split-line'" style="border-bottom: 1px solid #ccc; margin: 10px 0;"></div> <span v-if="option.type==='div'">{{option.text}}</span> <div v-if="option.type==='div'" :id="option.id"></div> </div> </div> </div> </div> </template> <script> import Vue from 'vue'; import '@grapecity-software/spread-sheets-vue'; import GC from '@grapecity-software/spread-sheets'; import "@grapecity-software/spread-sheets-shapes"; import "@grapecity-software/spread-sheets-pivot-addon"; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); import './styles.css'; function _getElementById(id) { return document.getElementById(id); } let App = Vue.extend({ name: "app", data: function() { return { spread: null, pivotTable: null, _rangeSelector: null, options: [ { type: "select", text: "Sort Field", id: "sort-field", items: ["Salesperson", "Cars", "Date"], selected: "Salesperson" }, { type: "radio-group", text: "Sort Type", items: [ { value: "0", label: "Ascending" }, { value: "1", label: "Descending" }, ], selected: "0", }, { type: "select", text: "Value Field Name", id: "value-field-name", items: ["", "Quantity"], selected: "" }, { type: "div", text: "Cell Ref", id: "formulaTextBox" }, { type: "split-line" }, { type: "button", text: "Set Sort Info", id: "set-sort-info", click: this.handleSetSortInfoClick }, { type: "button", text: "Clear Sort Info", id: "clear-sort-info", click: this.handleClearSortInfoClick }, ], }; }, methods: { initSpread: function(spread) { this.spread = spread; spread.suspendPaint(); spread.setSheetCount(2); let sheet1 = spread.getSheet(0); let sheet2 = spread.getSheet(1); let tableName = this.getSource(sheet2, pivotSales); let pivotTable = this.addPivotTable(sheet1, tableName); this.pivotTable = pivotTable; this.initFormulaTextBox(spread); spread.focus(); spread.resumePaint(); _getElementById("sort-field").addEventListener("change", () => { this.syncSortInfo(); }); }, initFormulaTextBox(spread) { let host = _getElementById("formulaTextBox"); this._rangeSelector = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(host, { rangeSelectMode: true, absoluteReference: true, needSheetName: false }, spread); this._rangeSelector.workbook(spread); }, handleSetSortInfoClick() { let spread = this.spread; let pivotTable = this.pivotTable; let _rangeSelector = this._rangeSelector; spread.suspendPaint(); let sortInfo = this.generateSortInfo(); if (sortInfo) { pivotTable.sort(this.$data.options[0].selected, sortInfo); this.syncSortInfo(); _rangeSelector.endSelectMode(); spread.focus(); } spread.resumePaint(); }, handleClearSortInfoClick() { let spread = this.spread; let pivotTable = this.pivotTable; let _rangeSelector = this._rangeSelector; pivotTable.sort(this.$data.options[0].selected, undefined); this.syncSortInfo(); _rangeSelector.endSelectMode(); spread.focus(); }, generateSortInfo() { let pivotReferences = this.initPivotReferences(); let sortInfo = { sortType: +this.$data.options[1].selected }; let selectedValueFieldName = this.$data.options[2].selected; if (selectedValueFieldName !== '') { sortInfo.sortValueFieldName = selectedValueFieldName; } if (pivotReferences) { sortInfo.sortByPivotReferences = pivotReferences; } return sortInfo; }, syncSortInfo() { let pivotTable = this.pivotTable; let sortInfo = pivotTable.sort(this.$data.options[0].selected); let sortType = sortInfo && sortInfo.sortType; if (sortType === undefined) { sortType = GC.Spread.Pivot.SortType.asc; } this.$data.options[1].selected = sortType + ''; let sortValueFieldName = sortInfo && sortInfo.sortValueFieldName; if (sortValueFieldName === undefined) { sortValueFieldName = ''; } this.$data.options[2].selected = sortValueFieldName; let pivotReferences = sortInfo && sortInfo.sortByPivotReferences; this.setPivotReferences(pivotReferences); }, initPivotReferences() { let _rangeSelector = this._rangeSelector; let pivotTable = this.pivotTable; let cellRef = _rangeSelector.text(); if (!cellRef || !this.$data.options[2].selected) { return; } let spread = this.spread; let sheet = spread.getActiveSheet(); let range = GC.Spread.Sheets.CalcEngine.formulaToRange(sheet, _rangeSelector.text()); if (range) { let row = range.row, col = range.col; if (sheet.pivotTables.findPivotTable(row, col)) { let pivotInfo = pivotTable.getPivotInfo(row, col); let fieldArea = pivotTable.getField(this.$data.options[0].selected).pivotArea; let infos; if (fieldArea === GC.Spread.Pivot.PivotTableFieldType.rowField) { infos = pivotInfo.colInfos; } if (fieldArea === GC.Spread.Pivot.PivotTableFieldType.columnField) { infos = pivotInfo.rowInfos; } if (infos && infos.length > 0) { let isGrandTotal = infos.length === 1 && infos[0].isGrandTotal; if (!isGrandTotal) { // if is grand total, nothing to do. return infos.map((info) => { return { fieldName: info.fieldName, items: [info.itemName] }; }); } } } } }, setPivotReferences(pivotReferences) { let pivotTable = this.pivotTable; let rangeStr = '', resultRow, resultCol; if (pivotReferences) { let refSourceNames = pivotReferences.map(ref => ref.fieldName); let allFields = pivotTable.getFieldsByArea(GC.Spread.Pivot.PivotTableFieldType.rowField).concat(pivotTable.getFieldsByArea(GC.Spread.Pivot.PivotTableFieldType.columnField)); let pivotArea = { references: allFields.map(function(field) { let index = refSourceNames.indexOf(field.sourceName); if (index !== -1) { return { fieldName: field.fieldName, items: pivotReferences[index].items }; } else { return { fieldName: field.fieldName }; } }) }; let range = pivotTable.getPivotAreaRanges(pivotArea)[0]; resultRow = range.row; resultCol = range.col; rangeStr = GC.Spread.Sheets.CalcEngine.rangeToFormula(new GC.Spread.Sheets.Range(resultRow, resultCol, 1, 1)); } this._rangeSelector.text(rangeStr); }, getSource: function(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(); }, addPivotTable: function(sheet, source) { sheet.suspendPaint(); sheet.name("PivotTable"); sheet.setRowCount(10000); let pivotTable = sheet.pivotTables.add("PivotTable", source, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.light8); 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("quantity", "Quantity", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); pivotTable.options.subtotalsPosition = GC.Spread.Pivot.SubtotalsPosition.top; pivotTable.resumeLayout(); sheet.resumePaint(); pivotTable.autoFitColumn(); return pivotTable; } }, computed: { dataSource() { return getData(); } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script>
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/pivot-data.js" type="text/javascript"></script> <!-- SystemJS --> <script src="$DEMOROOT$/zh/vue/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); System.import('$DEMOROOT$/zh/lib/vue/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 400px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 380px; padding: 10px; font-family: Arial, sans-serif; font-size: 14px; } .options-row div { width: 100%; display: flex; } .options-row { display: flex; align-items: center; margin-bottom: 10px; } .options-row span { flex: 1; margin-right: 10px; min-width: 100px; text-align: left; } .options-row select, .options-row input[type="text"] { flex: 2; padding: 5px; border: 1px solid #ccc; border-radius: 3px; font-family: inherit; font-size: inherit; } .options-row label { flex: 2; } .options-row label:first-of-type{ padding-left: 15px; } .options-row button { flex: 1; background-color: #007bff; color: #fff; padding: 5px 10px; border: none; border-radius: 3px; font-family: inherit; font-size: inherit; cursor: pointer; } .options-row button:hover { background-color: #0069d9; } #formulaTextBox { flex: 2; padding: 2px 5px; border: 1px solid #ccc; border-radius: 3px; font-family: inherit; font-size: inherit; background-color: #fff; overflow: hidden; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(function(global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: 'vue-loader' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js', '@grapecity-software/spread-sheets-shapes': 'npm:@grapecity-software/spread-sheets-shapes/index.js', '@grapecity-software/spread-sheets-pivot-addon': 'npm:@grapecity-software/spread-sheets-pivot-addon/index.js', '@grapecity-software/spread-sheets-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js', '@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.min.js', 'vue-loader': 'npm:systemjs-vue-browser/index.js', 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' } } }); })(this);