ピボットテーブルの並べ替え

SpreadJSピボットテーブルは データの並べ替えをサポートしています。 この機能は、データの順序付けと整理に使用できます。

概要 ピボットテーブルでは、次の4つの方法でフィールドを並べ替えることができます。 フィールド項目名で並べ替え 値で並べ替え カスタムフィールドの項目値で並べ替え カスタムコールバックで並べ替え カスタムソートリストで並べ替え sortTypeは任意の方法で設定できます。 API インターフェース API PivotTableManager APIs サンプルコード
<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 class="options-row"> <span>第1ソートキーの順序:</span> </div> <div class="options-row"> <input type="text" :class="{'text-input': true, 'disabled-style': autoSortStateRef || sortTypeRef === '2' || valueFieldNameRef !== ''}" v-model="firstKeyOrderListRef" placeholder="複数の項目はカンマ区切りで入力します"/> </div> <hr/> <div class="options-row"> <span >ソートするフィールド:</span> <select id="sort-field" v-model="sortFieldNameRef"> <option v-for="item in sortFieldNames" :key="item" :value="item">{{ item }}</option> </select> </div> <div class="options-row"> <label> <input type="checkbox" id="autoSort" v-model="autoSortStateRef">自動ソート: </label> </div> <div class="options-row"> <span>ソートタイプ:</span> </div> <div class="options-row"> <label v-for="(item, i) in sortTypeItems" :key="i"> <input type="radio" name="sortType" :id="sortType + i" :value="item.value" v-model="sortTypeRef"> {{ item.label }} </label> </div> <div v-show="sortTypeRef !=='2'"> <div class="options-row"> <span >値フィールド名:</span> <select id="valueFieldName" v-model="valueFieldNameRef"> <option v-for="item in valueFieldNameItems" :key="item" :value="item">{{ item }}</option> </select> </div> <div class="options-row"> <span>セル参照:</span> <div id="formulaTextBox"></div> </div> </div> <div v-show="sortTypeRef === '2'"> <div class="options-row"> <span>カスタムソートリスト:</span> </div> <div class="options-row"> <input type="text" class="text-input" v-model="customSortListRef" placeholder="複数の項目はカンマ区切りで入力します"/> </div> </div> <div class="options-row"> <button :id="set-sort-info" @click="handleSetSortInfoClick">ソート情報を設定する</button> </div> <div class="options-row"> <button :id="clear-sort-info" @click="handleClearSortInfoClick">ソート情報をクリアする</button> </div> </div> </div> </div> </template> <script setup> import GC from "@mescius/spread-sheets"; import "@mescius/spread-sheets-shapes"; import "@mescius/spread-sheets-vue"; import { ref, shallowRef } from "vue"; import "@mescius/spread-sheets-pivot-addon"; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); let spreadRef = shallowRef(null); let pivotTableRef = shallowRef(null); let _rangeSelectorRef = ref(null); let autoSortStateRef = ref(true); let firstKeyOrderListRef = ref(''); let customSortListRef = ref(''); let sortFieldNameRef = ref("Salesperson"); let sortTypeRef = ref('0'); let valueFieldNameRef = ref(''); let sortFieldNames = ["Salesperson", "Cars", "四半期 (Date)"]; let sortTypeItems = [ { value: "0", label: "昇順" }, { value: "1", label: "降順" }, { value: "2", label: "カスタム" }, ]; let valueFieldNameItems = ["", "Quantity"]; function initSpread (spread) { spreadRef.value = spread; spread.suspendPaint(); spread.setSheetCount(2); let sheet1 = spread.getSheet(0); let sheet2 = spread.getSheet(1); let tableName = getSource(sheet2, pivotSales); let pivotTable = addPivotTable(sheet1, tableName); pivotTableRef.value = pivotTable; initFormulaTextBox(spread); spread.focus(); spread.resumePaint(); _getElementById("sort-field").addEventListener("change", () => {syncSortInfo()}); } function initFormulaTextBox(spread) { let host = _getElementById("formulaTextBox"); _rangeSelectorRef.value = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(host, { rangeSelectMode: true, absoluteReference: true, needSheetName: false }, spread); _rangeSelectorRef.value.workbook(spread); } function handleSetSortInfoClick() { let spread = spreadRef.value; let pivotTable = pivotTableRef.value; let _rangeSelector = _rangeSelectorRef.value; spread.suspendPaint(); let sortInfo = generateSortInfo(); if (sortInfo) { pivotTable.autoSortState(sortFieldNameRef.value, autoSortStateRef.value); if (!autoSortStateRef.value) { const sheet = spread.getSheet(0); if (sheet) { if (firstKeyOrderListRef.value) { sheet.pivotTables.customList(firstKeyOrderListRef.value.split(',')); } else { sheet.pivotTables.customList(null); } } } pivotTable.sort(sortFieldNameRef.value, sortInfo); syncSortInfo(); _rangeSelector.endSelectMode(); spread.focus(); } spread.resumePaint(); } function handleClearSortInfoClick() { let spread = spreadRef.value; let pivotTable = pivotTableRef.value; let _rangeSelector = _rangeSelectorRef.value; pivotTable.sort(sortFieldNameRef.value, undefined); syncSortInfo(); _rangeSelector.endSelectMode(); spread.focus(); } function generateSortInfo() { let sortInfo = { sortType: +sortTypeRef.value }; if (sortInfo.sortType === 2) { if (customSortListRef.value) { sortInfo.customSortList = customSortListRef.value.split(','); } } else { let selectedValueFieldName = valueFieldNameRef.value; if (selectedValueFieldName !== '') { sortInfo.sortValueFieldName = selectedValueFieldName; } let pivotReferences = initPivotReferences(); if (pivotReferences) { sortInfo.sortByPivotReferences = pivotReferences; } } return sortInfo; } function syncSortInfo() { let pivotTable = pivotTableRef.value; let sortInfo = pivotTable.sort(sortFieldNameRef.value); let sortType = sortInfo && sortInfo.sortType; if (sortType === undefined) { sortType = GC.Spread.Pivot.SortType.asc; } sortTypeRef.value = sortType + ''; if (sortType === 2) { if (sortInfo && sortInfo.customSortList) { customSortListRef.value = sortInfo.customSortList.join(','); } else { customSortListRef.value = ''; } } else { let sortValueFieldName = sortInfo && sortInfo.sortValueFieldName; if (sortValueFieldName === undefined) { sortValueFieldName = ''; } valueFieldNameRef.value = sortValueFieldName; let pivotReferences = sortInfo && sortInfo.sortByPivotReferences; setPivotReferences(pivotReferences); } } function initPivotReferences() { let _rangeSelector = _rangeSelectorRef.value; let pivotTable = pivotTableRef.value; let cellRef = _rangeSelector.text(); if (!cellRef || !valueFieldNameRef.value) { return; } let spread = spreadRef.value; 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(sortFieldNameRef.value).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] }; }); } } } } } function setPivotReferences(pivotReferences) { let pivotTable = pivotTableRef.value; 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)); } _rangeSelectorRef.value.text(rangeStr); } function getSource (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 addPivotTable (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; } function _getElementById(id) { return document.getElementById(id); } </script> <style scoped> .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 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; } input.text-input { width: 100%; background-color: white; padding: 0.625rem 0.75rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 1rem; transition: border-color 0.2s, box-shadow 0.2s; } input.text-input:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1); } .text-input.disabled-style { background-color: #f3f4f6; color: #6b7280; border-color: #d1d5db; cursor: not-allowed; } .text-input.disabled-style::placeholder { color: #9ca3af; } .text-input.disabled-style:focus { border-color: #d1d5db; box-shadow: none; } #sortTypeContainer, #autoSortContainer { margin-bottom: 15px; } #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; } #app { height: 100%; } </style>
<!DOCTYPE html> <html lang="en" style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/pivot-data.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/ja/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/', 'cdn:': 'node_modules/' }, packageConfigPaths: [ '../node_modules/*/package.json', "../node_modules/@mescius/*/package.json", "../node_modules/@babel/*/package.json", "../node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.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", '@mescius/spread-sheets': 'cdn:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'cdn:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-vue': 'cdn:@mescius/spread-sheets-vue/index.js', '@mescius/spread-sheets-pivot-addon': 'cdn:@mescius/spread-sheets-pivot-addon/index.js', '@mescius/spread-sheets-shapes': 'cdn:@mescius/spread-sheets-shapes/index.js' }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);