概述
本 Demo 展示了如何实现自定义特性的序列化与反序列化。Demo 中创建了 6 种自定义特性:自定义单元格类型(绘制菱形)、自定义函数(返回当前日期)、自定义格式化程序(数字与英文单词转换)、自定义迷你图(绘制圆形)、自定义标签(存储姓名和年龄)、自定义行筛选(筛选时高亮显示),并演示了如何将这些自定义特性从一个工作簿序列化到另一个工作簿。
实现思路
定义各种自定义类型,并为每个类型设置 typeName 属性,用于序列化时的类型标识
对于需要自定义序列化逻辑的类型,重写 toJSON 和 fromJSON 方法
对于定义在闭包中的私有类型,重写 getTypeFromString 方法来手动解析类型字符串
在源工作簿中创建并应用各种自定义特性
点击按钮时,调用 toJSON 将源工作簿序列化为 JSON,再调用 fromJSON 反序列化到目标工作簿
代码解析
定义自定义单元格类型
这段代码定义了一个自定义单元格类型,继承自 CellTypes.Base。typeName 属性是序列化的关键,SpreadJS 通过它识别类型。paint 方法定义了单元格的绘制逻辑。
定义自定义标签并重写序列化方法
自定义标签通过重写 toJSON 和 fromJSON 方法,精确控制序列化的数据内容。toJSON 必须返回包含 typeName 的对象。
使用命名空间定义自定义类型
使用命名空间避免类型名冲突,typeName 应包含完整的命名空间路径(如 mynamespace.MyFunction)。
重写 getTypeFromString 处理私有类型
对于定义在闭包中的私有类型(如 MyFormatter、MyRowFilter),它们无法通过 window 对象访问。需要重写 getTypeFromString 方法,手动将类型字符串映射到实际的类型构造函数。
执行序列化和反序列化
点击按钮时,将源工作簿序列化为 JSON 字符串,然后反序列化到目标工作簿。所有自定义特性都会被完整保留。
运行效果
源工作簿包含 6 个工作表,每个展示一种自定义特性:Cell type(菱形单元格)、Function(当前日期函数)、Formatter(数字转英文单词)、SparklineEx(圆形迷你图)、Tag(自定义标签)、Row Filter(高亮筛选行)
点击"JSON序列化"按钮后,目标工作簿会完整复制源工作簿的所有内容和自定义特性
在控制台中可以查看源工作簿和目标工作簿的标签数据序列化结果,验证自定义类型数据的正确传递
API 参考
setCellType 方法
row、col:单元格位置
value:单元格类型对象
sheetArea:可选,指定表单区域
tag 方法
value:标签对象
row、col:单元格位置(setTag 使用)
toJSON 和 fromJSON 方法
序列化和反序列化工作簿的所有内容,包括自定义特性。
getTypeFromString 方法
从类型字符串获取类型构造函数,用于自定义类型的反序列化。
function MyCellType(color) {
GC.Spread.Sheets.CellTypes.Base.apply(this, arguments);
this.color = color || "orange";
this.typeName = "MyCellType";
}
MyCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
MyCellType.prototype.paint = function(ctx, value, x, y, width, height, style, context) {
var MARGIN = 5,
plotLeft = x + MARGIN,
plotWidth = width - 2 * MARGIN,
plotTop = y + MARGIN,
plotHeight = height - 2 * MARGIN,
halfHeight = plotHeight / 2,
halfWidth = plotWidth / 2;
ctx.beginPath();
ctx.moveTo(plotLeft, plotTop + halfHeight);
ctx.lineTo(plotLeft + halfWidth, plotTop);
ctx.lineTo(plotLeft + plotWidth, plotTop + halfHeight);
ctx.lineTo(plotLeft + halfWidth, plotTop + plotHeight);
ctx.lineTo(plotLeft, plotTop + halfHeight);
ctx.strokeStyle = this.color;
ctx.stroke();
};
// Define custom tag
function MyTag(name, age) {
this.name = name;
this.age = age;
this.typeName = "MyTag";
}
MyTag.prototype.toJSON = function() {
return {
typeName: this.typeName, //necessary
name: this.name,
age: this.age
};
};
MyTag.prototype.fromJSON = function(settings) {
if (settings.name !== undefined) {
this.name = settings.name;
}
if (settings.age !== undefined) {
this.age = settings.age;
}
};
var mynamespace = {};
(function() {
// Define custom function (with namespace)
function MyFunction() {
GC.Spread.CalcEngine.Functions.Function.apply(this, ["MyFunction", 0, 0]);
this.typeName = "mynamespace.MyFunction";
}
MyFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
MyFunction.prototype.evaluate = function(args) {
var now = new Date();
return now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate();
};
mynamespace.MyFunction = MyFunction;
// Define custom sparklineEx (with namespace)
function MySparklineEx() {
GC.Spread.Sheets.Sparklines.SparklineEx.apply(this, arguments);
this.typeName = "mynamespace.MySparklineEx";
}
MySparklineEx.prototype = new GC.Spread.Sheets.Sparklines.SparklineEx();
MySparklineEx.prototype.createFunction = function() {
var func = new GC.Spread.CalcEngine.Functions.Function("CIRCLE", 0, 0);
func.evaluate = function(args) {
return {};
};
return func;
};
MySparklineEx.prototype.paint = function(context, value, x, y, width, height) {
context.beginPath();
context.arc(x + width / 2, y + height / 2, (Math.min(width, height) - 6) / 2, 0, Math.PI * 2);
context.strokeStyle = "orange";
context.stroke();
};
mynamespace.MySparklineEx = MySparklineEx;
})();
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
sheetCount: 6
});
var spread2 = new GC.Spread.Sheets.Workbook(document.getElementById("ss1"), {
sheetCount: 1
});
initSpread(spread);
};
function initSpread(spread) {
var tagSheetIndex, rowFilterSheetIndex;
var digitMap = [{
label: "Zero",
value: 0
},
{
label: "One",
value: 1
},
{
label: "Two",
value: 2
},
{
label: "Three",
value: 3
},
{
label: "Four",
value: 4
},
{
label: "Five",
value: 5
},
{
label: "Six",
value: 6
},
{
label: "Seven",
value: 7
},
{
label: "Eight",
value: 8
},
{
label: "Nine",
value: 9
},
{
label: "Ten",
value: 10
}
];
// Define custom formatter (private in function)
function MyFormatter(format, cultureName) {
GC.Spread.Formatter.FormatterBase.apply(this, arguments);
this.typeName = "MyFormatter";
}
MyFormatter.prototype = new GC.Spread.Formatter.FormatterBase();
MyFormatter.prototype.parse = function(str) {
var labels = digitMap.map(function(item) {
return item.label;
}),
index = labels.indexOf(str);
if (index === -1) {
index = parseInt(str);
}
return (index < 0 || index > 10) ? NaN : index;
};
MyFormatter.prototype.format = function(obj, conditionalForeColor) {
var numbers = digitMap.map(function(item) {
return item.value
}),
index = numbers.indexOf(obj);
return index === -1 ? "NaN" : digitMap[index].label;
};
// Define custom row filter (private in function)
function MyRowFilter(range) {
GC.Spread.Sheets.Filter.RowFilterBase.apply(this, arguments);
this.typeName = "MyRowFilter";
}
MyRowFilter.prototype = new GC.Spread.Sheets.Filter.RowFilterBase();
MyRowFilter.prototype.onFilter = function(args) {
if (args.action === GC.Spread.Sheets.Filter.FilterActionType.filter) {
var sheet = args.sheet,
range = args.range,
filterdRows = args.filteredRows,
filteredOutRows = args.filteredOutRows;
if (range.row < 0) {
range.row = 0;
range.rowCount = sheet.getRowCount();
}
if (range.col < 0) {
range.col = 0;
range.colCount = sheet.getColumnCount();
}
for (var i = 0, filterdRowCount = filterdRows.length; i < filterdRowCount; i++) {
var rowIndex = filterdRows[i];
sheet.getRange(rowIndex, range.col, 1, range.colCount).backColor("red");
}
}
};
var oldFun = GC.Spread.Sheets.getTypeFromString;
// Private types can not be accessed from window, so override getTypeFromString method.
GC.Spread.Sheets.getTypeFromString = function(typeString) {
switch (typeString) {
case "MyFormatter":
return MyFormatter;
case "MyRowFilter":
return MyRowFilter;
default:
return oldFun.apply(this, arguments);
}
};
spread.suspendPaint();
spread.options.allowSheetReorder = false;
spread.options.tabStripRatio = 0.9;
var sheet = spread.getSheet(0);
sheet.name("Cell type");
sheet.setCellType(0, 0, new MyCellType("green"));
sheet.setRowHeight(0, 60);
sheet = spread.getSheet(1);
sheet.name("Function");
sheet.addCustomFunction(new mynamespace.MyFunction());
sheet.setFormula(0, 0, "MyFunction()");
sheet.setColumnWidth(0, 150);
sheet = spread.getSheet(2);
sheet.name("Formatter");
sheet.setValue(0, 0, 1);
sheet.setFormatter(0, 0, new MyFormatter());
sheet.setFormula(0, 1, "A1");
sheet = spread.getSheet(3);
sheet.name("SparklineEx");
spread.addSparklineEx(new mynamespace.MySparklineEx());
sheet.setFormula(0, 0, "CIRCLE()");
sheet.setRowHeight(0, 60);
tagSheetIndex = 4;
sheet = spread.getSheet(tagSheetIndex);
sheet.name("Tag");
sheet.tag(new MyTag("Ivy", 24));
sheet.setTag(0, 0, new MyTag("Yang", 25));
sheet.options.allowCellOverflow = true;
sheet.setValue(0, 0, "Please check tag serialization result in console");
rowFilterSheetIndex = 5;
sheet = spread.getSheet(rowFilterSheetIndex);
sheet.name("Row Filter");
for (var r = 0; r < 3; r++) {
for (var c = 0; c < 3; c++) {
sheet.setValue(r, c, r + c);
}
}
var filter = new MyRowFilter(new GC.Spread.Sheets.Range(0, 0, 3, 3));
sheet.rowFilter(filter);
filter.filterButtonVisible(false);
var condition = new GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.numberCondition, {
compareType: GC.Spread.Sheets.ConditionalFormatting.GeneralComparisonOperators.greaterThan,
expected: 1
});
sheet.rowFilter().addFilterItem(0, condition);
var spread1 = GC.Spread.Sheets.findControl(document.getElementById('ss'));
spread1.getSheet(rowFilterSheetIndex).rowFilter().filter();
spread.resumePaint();
document.getElementById("fromtoJsonBtn").addEventListener('click',function() {
// ToJson
var spread1 = GC.Spread.Sheets.findControl(document.getElementById('ss'));
var jsonStr = JSON.stringify(spread1.toJSON());
// FromJson
var spread2 = GC.Spread.Sheets.findControl(document.getElementById('ss1'));
spread2.fromJSON(JSON.parse(jsonStr));
var sheet = spread1.getSheet(tagSheetIndex);
console.log("Source spread:");
console.log("Sheet tag: ", sheet.tag());
console.log("Cell Tag: ", sheet.getTag(0, 0));
var sheet2 = spread2.getSheet(tagSheetIndex);
console.log("Target spread:");
console.log("Sheet tag: ", sheet2.tag());
console.log("Cell Tag: ", sheet2.getTag(0, 0));
});
}
<!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-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="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div class="sample-spreadsheets" style="overflow: auto;">
<label style="font:bold 10pt arial">源工作簿:</label>
<div id="ss" style="height: 260px"></div>
<br />
<label style="font:bold 10pt arial">目标工作簿:</label>
<div id="ss1" style="height: 260px"></div>
</div>
<div class="options-container">
<div class="option-row">
<label>点击按钮将源工作簿中的自定义项序列化到目标工作簿。</label>
</div>
<div class="option-row">
<input type="button" id="fromtoJsonBtn" value="JSON序列化" title="将源工作表序列化为JSON,然后从JSON恢复到目标工作表。" />
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 600px;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
label {
display: block;
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}