概述
本 Demo 展示了如何为数据透视表的不同字段设置自定义格式。Demo 创建了一个包含行字段(销售员、汽车)、列字段(季度)和值字段(数量)的数据透视表,演示了如何根据字段类型构建透视区域对象,并为标签区域和数据区域应用不同的格式化字符串。
实现思路
创建数据源工作表,加载销售数据并生成表格
创建数据透视表,添加行字段、列字段和值字段
通过 getField 方法获取字段信息,根据 pivotArea 属性判断字段类型
为行字段和列字段构建标签区域对象(labelOnly: true)
为值字段构建数据区域对象(dataOnly: true)
使用 setStyle 方法为字段应用样式(包含 formatter 属性)
绑定交互事件,支持用户动态修改字段格式
代码解析
构建透视区域对象
这段代码通过 getField 方法获取字段信息,根据 pivotArea 属性判断字段类型。pivotArea 值为 1 表示行字段,2 表示列字段,3 表示值字段。对于行字段和列字段,构建标签区域对象(labelOnly: true);对于值字段,构建数据区域对象(dataOnly: true),并指定字段名称和项名称。
为字段设置初始格式
这段代码创建了两个样式对象,分别为汽车字段设置红色文本格式([red]@),为数量字段设置保留两位小数的数字格式(#0.00)。setStyle 方法接受透视区域对象和样式对象作为参数,将格式应用到指定字段。
动态设置字段格式
点击设置按钮时,获取用户选择的字段名称和格式化字符串,调用 initArea 构建透视区域对象,通过 getStyle 获取现有样式或创建新样式对象,设置 formatter 属性后应用到透视表。
运行效果
页面加载后,数据透视表中的汽车字段显示为红色文本,数量字段显示为保留两位小数的格式
在右侧面板的下拉框中选择字段(销售员、汽车、季度、数量)
在格式输入框中输入格式化字符串(如 0.00%、$#,##0 等)
点击"设置"按钮后,所选字段应用新的格式化字符串
点击"重置"按钮可恢复字段的当前格式
API 参考
PivotTable.setStyle 方法
area:透视区域对象,包含 labelOnly/dataOnly 和 references 属性
style:GC.Spread.Sheets.Style 对象,可设置 formatter 等属性
PivotTable.getStyle 方法
area:透视区域对象
返回值:该区域的当前样式对象,未设置时返回 null
PivotTable.getField 方法
fieldName:字段名称
返回值:字段信息对象,包含 pivotArea 属性(1=行字段,2=列字段,3=值字段)
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 = getSource(sheet2, pivotSales);
let pivotTable = addPivotTable(sheet1, tableName);
bindEvent(pivotTable,spread);
spread.resumePaint();
}
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);
let carsStyle = new GC.Spread.Sheets.Style();
carsStyle.formatter = '[red]@';
let valueStyle = new GC.Spread.Sheets.Style();
valueStyle.formatter = '#0.00';
pivotTable.setStyle(initArea('Cars', pivotTable), carsStyle);
pivotTable.setStyle(initArea('Quantity', pivotTable), valueStyle);
pivotTable.resumeLayout();
sheet.resumePaint();
pivotTable.autoFitColumn();
return pivotTable;
}
function _isNullOrUndefined (obj) {
return obj === null || obj === undefined;
}
function bindEvent(pivotTable,spread) {
_get('fieldName').addEventListener('change', (e) => {
let fieldName = e.target.value;
let area = initArea(fieldName, pivotTable);
let style = pivotTable.getStyle(area);
_get('formatter').value = (style && style.formatter) ? style.formatter : "";
})
_get('set').addEventListener('click', (e) => {
let fieldName = _get('fieldName').value;
let formatter = _get('formatter').value;
let area = initArea(fieldName, pivotTable);
let style = pivotTable.getStyle(area) || new GC.Spread.Sheets.Style();
style.formatter = formatter;
pivotTable.setStyle(area, style);
})
_get('reset').addEventListener('click', (e) => {
let fieldName = _get('fieldName').value;
let area = initArea(fieldName, pivotTable);
let formatter = pivotTable.getStyle(area)?.formatter || "";
_get('formatter').value = formatter;
})
}
function initArea (fieldName, pt) {
let fieldArea = pt.getField(fieldName).pivotArea;
if (fieldArea === 1 || fieldArea === 2) {
return {
labelOnly: true,
references: [
{
fieldName
}
]
}
} else if (fieldArea === 3) {
return {
dataOnly: true,
references: [
{
fieldName: "值",
items: [fieldName]
}
]
}
}
}
function _get (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$/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="$DEMOROOT$/spread/source/data/pivot-data.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 class="options-container">
<div class="whole-field">以下所有操作均作用于 <b>销售员</b> 字段:</div>
<div class="field-name">字段名称:</div>
<select name="fieldName" id="fieldName" class="field-name">
<option value="Salesperson" selected="">销售员</option>
<option value="Cars">汽车</option>
<option value="季度 (date)">日期</option>
<option value="Quantity">数量</option>
</select>
<br />
<div class="field-name">格式:</div>
<input type="text" class="label-filter-input filter-input" id="formatter" />
<input type="button" class="format-button" value="设置" id="set">
<input type="button" class="format-button" value="重置" id="reset">
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.filter-input {
width: 200px;
height: 20px;
display: block;
/* margin-left: 15px; */
margin-top: 10px;
}
.format-button {
width: 45%;
margin-top: 20px;
/* float: right; */
display: inline-block;
}
.field-name {
width: 200px;
height: 25px;
display: block;
margin-bottom: 10px;
float: left;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.whole-field {
margin-bottom: 10px;
}