You can configure the PivotTable with different parameters:
These parameters that can be used for PivotTable are as follows:
allowMultipleFiltersPerField: whether multiple filters can be used in one field.
insertBlankLineAfterEachItem: whether a blank row should be inserted at end of each item.
grandTotalPosition: show the grandtotal in the row, column or both.
subtotalsPosition: show subtotal top, bottom, or not.
displayFieldsInPageFilterArea: display the page area fields first over then down or first down then over
reportFilterFieldsPerColumn: the number of report filter fields per column
bandRows: show banded rows or not
bandColumns: show banded columns or not
showRowHeader: show row header styles or not
showColumnHeader: show column header styles or not
showDrill: show expand/collapse button or not
showMissing: show missing caption or not
showToolTip: show tool tip or not
missingCaption: replace empty cell in content area to custom string or number
fillDownLabels: show repeat label items or not
repeatAllItemLabels: show repeat label items or not
rowLabelIndent: set the indent of each level of title in compact layout
mergeItem: merge and center cells with labels
showHeaders: show row and column headers or not
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import "@mescius/spread-sheets-shapes";
import "@mescius/spread-sheets-pivot-addon";
import { SpreadSheets } from '@mescius/spread-sheets-react';
import './styles.css';
const useState = React.useState;
function PivotOption(props) {
return (<div className="option-item">
<input type="checkbox" id={props.value} name={props.value} value={props.value} class="select-option" checked={props.checked} onChange={props.onChange}></input>
<label for={props.value}>{props.text}</label>
</div>);
}
export function AppFunc() {
const [pivotTable, setPivotTable] = useState(null);
const [state, setState] = useState({
option: {
allowMultipleFiltersPerField: false,
insertBlankLineAfterEachItem: false,
fillDownLabels: false,
bandRows: true,
bandColumns: true,
showRowHeader: true,
showColumnHeader: true,
showDrill: true,
showFilter: true,
showMissing: true,
showToolTip: true,
mergeItem: false,
showHeaders: true
},
grandTotalPosition: 3,
displayFieldsInPageFilterArea: 1,
subtotalsPosition: 2,
pivotTableLayout: 1,
missingCaption: null,
reportFilterFieldsPerColumn: 1,
rowLabelIndent: null
});
const handleSetOption = () => {
pivotTable.suspendLayout();
let actualOption = state.option;
let option = pivotTable.options, item;
for (item in actualOption) {
if (Object.prototype.hasOwnProperty.call(actualOption, item)) {
option[item] = actualOption[item];
}
}
option["grandTotalPosition"] = parseInt(state.grandTotalPosition, 10);
option["subtotalsPosition"] = parseInt(state.subtotalsPosition, 10);
option["displayFieldsInPageFilterArea"] = parseInt(state.displayFieldsInPageFilterArea, 10);
option["reportFilterFieldsPerColumn"] = state.reportFilterFieldsPerColumn;
let rowLabelIndent = state.rowLabelIndent;
let missingCaption = state.missingCaption;
option["missingCaption"] = state.missingCaption;
if (!isNaN(parseFloat(missingCaption))) {
option["missingCaption"] = parseFloat(missingCaption);
} else {
option["missingCaption"] = missingCaption;
}
if (!isNaN(parseFloat(rowLabelIndent))) {
option["rowLabelIndent"] = parseFloat(rowLabelIndent);
}
pivotTable.layoutType(+state.pivotTableLayout);
pivotTable.resumeLayout();
}
const handleInputClick = (event) => {
const target = event.target;
const value = target.checked;
const name = target.name;
let option = { ...state.option, [name]: value };
setState((state) => ({
...state, option
}));
}
const handleInputChange = (event) => {
const target = event.target;
const value = target.value;
const name = target.name;
setState((state) => ({
...state,
[name]: value
}));
}
const initSpread = (spread) => {
spread.suspendPaint();
spread.setSheetCount(2);
let sheet1 = spread.getSheet(0);
let sheet2 = spread.getSheet(1);
let tableName = getDataSource(sheet2, pivotSales);
let pt = initPivotTable(sheet1, tableName);
setPivotTable(pt);
spread.resumePaint();
}
const 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();
}
const initPivotTable = (sheet, source) => {
sheet.name("PivotTable");
sheet.setRowCount(1000);
let option = {
bandRows: true,
bandColumns: true
};
let pt = sheet.pivotTables.add("pivotTable", source, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium8, option);
pt.suspendLayout();
pt.add("quantity", "Quantity", GC.Spread.Pivot.PivotTableFieldType.filterField);
pt.add("price", "Price", GC.Spread.Pivot.PivotTableFieldType.filterField);
pt.add("salesperson", "Salesperson", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt.add("car", "Cars", GC.Spread.Pivot.PivotTableFieldType.rowField);
let groupInfo = { originFieldName: "date", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }] };
pt.group(groupInfo);
pt.add("Quarters (date)", "Quarters (date)", GC.Spread.Pivot.PivotTableFieldType.columnField);
pt.add("total", "Totals", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
let style = new GC.Spread.Sheets.Style();
style.formatter = "$ #,##0";
pt.setStyle({ dataOnly: true }, style);
pt.resumeLayout();
pt.autoFitColumn();
return pt;
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread)}>
</SpreadSheets>
</div>
<div id="container" class="options-container">
<div class="option-item">
<label><b>Pivot Options</b></label>
</div>
<hr />
<PivotOption value={'allowMultipleFiltersPerField'} text="Allow Multiple Filters Per Field" checked={state.option.allowMultipleFiltersPerField} onChange={handleInputClick}></PivotOption>
<PivotOption value={'insertBlankLineAfterEachItem'} text="Insert Blank Line After Each Item" checked={state.option.insertBlankLineAfterEachItem} onChange={handleInputClick}></PivotOption>
<PivotOption value={'fillDownLabels'} text="fillDownLabels" checked={state.option.fillDownLabels} onChange={handleInputClick}></PivotOption>
<PivotOption value={'bandRows'} text="Band Rows" checked={state.option.bandRows} onChange={handleInputClick}></PivotOption>
<PivotOption value={'bandColumns'} text="Band Columns" checked={state.option.bandColumns} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showRowHeader'} text="Show Row Header" checked={state.option.showRowHeader} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showColumnHeader'} text="Show Column Header" checked={state.option.showColumnHeader} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showDrill'} text="Show Drill" checked={state.option.showDrill} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showFilter'} text="Show Filter" checked={state.option.showFilter} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showMissing'} text="Show Missing" checked={state.option.showMissing} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showToolTip'} text="Show ToolTip" checked={state.option.showToolTip} onChange={handleInputClick}></PivotOption>
<PivotOption value={'mergeItem'} text="Merge Item" checked={state.option.mergeItem} onChange={handleInputClick}></PivotOption>
<PivotOption value={'showHeaders'} text="showHeaders" checked={state.option.showHeaders} onChange={handleInputClick}></PivotOption>
<hr />
<div>
<div class="select-option-class">Grand Total Position:</div>
<select value={state.grandTotalPosition} name="grandTotalPosition" id="grandTotalPosition" class="grandTotalPosition select-option-select" onChange={handleInputChange}>
<option value="0">none</option>
<option value="1">row</option>
<option value="2">col</option>
<option value="3">both</option>
</select>
</div>
<div>
<div class="select-option-class">Subtotals Position:</div>
<select value={state.subtotalsPosition} name="subtotalsPosition" id="subtotalsPosition" class="subtotalsPosition select-option-select" onChange={handleInputChange}>
<option value="2">bottom</option>
<option value="0">none</option>
<option value="1">top</option>
</select>
</div>
<div>
<div class="select-option-class">Display Fields In Page Filter Area:</div>
<select value={state.displayFieldsInPageFilterArea} name="displayFieldsInPageFilterArea" id="displayFieldsInPageFilterArea" class="displayFieldsInPageFilterArea select-option-select" onChange={handleInputChange}>
<option value="1">overThenDown</option>
<option value="0">downThenOver</option>
</select>
</div>
<div>
<div class="select-option-class">PivotTable Layout:</div>
<select id="pivotTableLayout" class="pivotTableLayout select-option-select" name="pivotTableLayout" value={state.pivotTableLayout} onChange={handleInputChange}>
<option value="0">Compact</option>
<option value="1">Outline</option>
<option value="2">Tabular</option>
</select>
</div>
<div>
<div class="select-option-class">Show Empty Value In Content Area As:</div>
<input type="text" name="missingCaption" id="missingCaption" class="missingCaption select-option-select" value={state.missingCaption} onChange={handleInputChange} />
</div>
<div>
<div class="select-option-class">Set Row Label Indent In Compact Layout:</div>
<input type="number" name="rowLabelIndent" id="rowLabelIndent" class="rowLabelIndent select-option-select" value={state.rowLabelIndent} onChange={handleInputChange} />
</div>
<div>
<label for="reportFilterFieldsPerColumn">Report Filter Fields Per Column:</label>
<input type="number" name="reportFilterFieldsPerColumn" id="reportFilterFieldsPerColumn" min="1" value={state.reportFilterFieldsPerColumn} onChange={handleInputChange}></input>
</div>
<input type="button" value="Apply" class="set-option" id="set-option" onClick={handleSetOption}></input>
</div>
</div>
);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import "@mescius/spread-sheets-shapes";
import "@mescius/spread-sheets-pivot-addon";
import { SpreadSheets } from '@mescius/spread-sheets-react';
import './styles.css';
const Component = React.Component;
function PivotOption(props) {
return (<div className="option-item">
<input type="checkbox" id={props.value} name={props.value} value={props.value} class="select-option" checked={props.checked} onChange={props.onChange}></input>
<label for={props.value}>{props.text}</label>
</div>);
}
export class App extends Component {
constructor(props) {
super(props);
this.state = {
renderChild: false,
option: {
allowMultipleFiltersPerField: false,
insertBlankLineAfterEachItem: false,
fillDownLabels: false,
bandRows: true,
bandColumns: true,
showRowHeader: true,
showColumnHeader: true,
showDrill: true,
showFilter: true,
showMissing: true,
showToolTip: true,
mergeItem: false,
showHeaders: true
},
grandTotalPosition: 3,
displayFieldsInPageFilterArea: 1,
subtotalsPosition: 2,
pivotTableLayout: 1,
missingCaption: null,
reportFilterFieldsPerColumn: 1,
rowLabelIndent: null
}
this.handleSetOption = this.handleSetOption.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleInputClick = this.handleInputClick.bind(this);
}
handleSetOption() {
let pivotTable = this.pivotTable;
let state = this.state;
pivotTable.suspendLayout();
let actualOption = state.option;
let option = pivotTable.options, item;
for (item in actualOption) {
if (Object.prototype.hasOwnProperty.call(actualOption, item)) {
option[item] = actualOption[item];
}
}
option["grandTotalPosition"] = parseInt(state.grandTotalPosition, 10);
option["subtotalsPosition"] = parseInt(state.subtotalsPosition, 10);
option["displayFieldsInPageFilterArea"] = state.displayFieldsInPageFilterArea;
option["reportFilterFieldsPerColumn"] = state.reportFilterFieldsPerColumn;
let rowLabelIndent = state.rowLabelIndent;
let missingCaption = state.missingCaption;
option["missingCaption"] = state.missingCaption;
if (!isNaN(parseFloat(missingCaption))) {
option["missingCaption"] = parseFloat(missingCaption);
} else {
option["missingCaption"] = missingCaption;
}
if (!isNaN(parseFloat(rowLabelIndent))) {
option["rowLabelIndent"] = parseFloat(rowLabelIndent);
}
pivotTable.layoutType(+state.pivotTableLayout);
pivotTable.resumeLayout();
}
handleInputClick(event) {
const target = event.target;
const value = target.checked;
const name = target.name;
let option = this.state.option;
option[name] = value;
this.setState({
option
});
}
handleInputChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
</SpreadSheets>
</div>
<div id="container" class="options-container">
<div class="option-item">
<label><b>Pivot Options</b></label>
</div>
<hr />
<PivotOption value={'allowMultipleFiltersPerField'} text="Allow Multiple Filters Per Field" checked={this.state.option.allowMultipleFiltersPerField} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'insertBlankLineAfterEachItem'} text="Insert Blank Line After Each Item" checked={this.state.option.insertBlankLineAfterEachItem} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'fillDownLabels'} text="fillDownLabels" checked={this.state.option.fillDownLabels} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'bandRows'} text="Band Rows" checked={this.state.option.bandRows} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'bandColumns'} text="Band Columns" checked={this.state.option.bandColumns} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showRowHeader'} text="Show Row Header" checked={this.state.option.showRowHeader} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showColumnHeader'} text="Show Column Header" checked={this.state.option.showColumnHeader} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showDrill'} text="Show Drill" checked={this.state.option.showDrill} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showFilter'} text="Show Filter" checked={this.state.option.showFilter} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showMissing'} text="Show Missing" checked={this.state.option.showMissing} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showToolTip'} text="Show ToolTip" checked={this.state.option.showToolTip} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'mergeItem'} text="Merge Item" checked={this.state.option.mergeItem} onChange={this.handleInputClick}></PivotOption>
<PivotOption value={'showHeaders'} text="showHeaders" checked={this.state.option.showHeaders} onChange={this.handleInputClick}></PivotOption>
<hr />
<div>
<div class="select-option-class">Grand Total Position:</div>
<select value={this.state.grandTotalPosition} name="grandTotalPosition" id="grandTotalPosition" class="grandTotalPosition select-option-select" onChange={this.handleInputChange}>
<option value="0">none</option>
<option value="1">row</option>
<option value="2">col</option>
<option value="3">both</option>
</select>
</div>
<div>
<div class="select-option-class">Subtotals Position:</div>
<select value={this.state.subtotalsPosition} name="subtotalsPosition" id="subtotalsPosition" class="subtotalsPosition select-option-select" onChange={this.handleInputChange}>
<option value="2">bottom</option>
<option value="0">none</option>
<option value="1">top</option>
</select>
</div>
<div>
<div class="select-option-class">Display Fields In Page Filter Area:</div>
<select value={this.state.displayFieldsInPageFilterArea} name="displayFieldsInPageFilterArea" id="displayFieldsInPageFilterArea" class="displayFieldsInPageFilterArea select-option-select" onChange={this.handleInputChange}>
<option value="1">overThenDown</option>
<option value="0">downThenOver</option>
</select>
</div>
<div>
<div class="select-option-class">PivotTable Layout:</div>
<select id="pivotTableLayout" class="pivotTableLayout select-option-select" name="pivotTableLayout" value={this.state.pivotTableLayout} onChange={this.handleInputChange}>
<option value="0">Compact</option>
<option value="1">Outline</option>
<option value="2">Tabular</option>
</select>
</div>
<div>
<div class="select-option-class">Show Empty Value In Content Area As:</div>
<input type="text" name="missingCaption" id="missingCaption" class="missingCaption select-option-select" value={this.state.missingCaption} onChange={this.handleInputChange} />
</div>
<div>
<div class="select-option-class">Set Row Label Indent In Compact Layout:</div>
<input type="number" name="rowLabelIndent" id="rowLabelIndent" class="rowLabelIndent select-option-select" value={this.state.rowLabelIndent} onChange={this.handleInputChange} />
</div>
<div>
<label for="reportFilterFieldsPerColumn">Report Filter Fields Per Column:</label>
<input type="number" name="reportFilterFieldsPerColumn" id="reportFilterFieldsPerColumn" min="1" value={this.state.reportFilterFieldsPerColumn} onChange={this.handleInputChange}></input>
</div>
{this.state.renderChild ? <input type="button" value="Apply" class="set-option" id="set-option" onClick={this.handleSetOption}></input> : ""}
</div>
</div>
);
}
initSpread(spread) {
spread.suspendPaint();
spread.setSheetCount(2);
let sheet1 = spread.getSheet(0);
let sheet2 = spread.getSheet(1);
let tableName = this.getDataSource(sheet2, pivotSales);
let pivotTable = this.initPivotTable(sheet1, tableName);
this.pivotTable = pivotTable;
spread.resumePaint();
}
componentDidMount() {
this.setState(() => ({
renderChild: true
}))
}
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();
}
initPivotTable(sheet, source) {
sheet.name("PivotTable");
sheet.setRowCount(1000);
let option = {
bandRows: true,
bandColumns: true
};
let pivotTable = sheet.pivotTables.add("pivotTable", source, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium8, option);
pivotTable.suspendLayout();
pivotTable.add("quantity", "Quantity", GC.Spread.Pivot.PivotTableFieldType.filterField);
pivotTable.add("price", "Price", GC.Spread.Pivot.PivotTableFieldType.filterField);
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("Quarters (date)", "Quarters (date)", GC.Spread.Pivot.PivotTableFieldType.columnField);
pivotTable.add("total", "Totals", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
let style = new GC.Spread.Sheets.Style();
style.formatter = "$ #,##0";
pivotTable.setStyle({ dataOnly: true }, style);
pivotTable.resumeLayout();
pivotTable.autoFitColumn();
return pivotTable;
}
}
<!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$/en/react/node_modules/@mescius/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$/en/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/en/lib/react/license.js').then(function () {
System.import('./src/app');
});
</script>
</head>
<body>
<div id="app" style="height: 100%;"></div>
</body>
</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;
}
.set-option {
display: block;
margin-top: 20px;
width: 250px;
}
#reportFilterFieldsPerColumn {
width: 28px;
}
.select-option-class{
display: block;
margin-top: 20px;
margin-bottom: 10px
}
.select-option-select{
width: 250px;
display: block;
margin-bottom: 20px;
}
.option-item{
height: 20px;
margin-bottom: 10px;
}
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js',
'@mescius/spread-sheets-pivot-addon': 'npm:@mescius/spread-sheets-pivot-addon/index.js',
'@mescius/spread-sheets-react': 'npm:@mescius/spread-sheets-react/index.js',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.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: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);