如果你想实现一个支持撤销/重做的命令,你可以通过如下方法启动一个事务,命令中的任何行为和数据变更将开启自动存储。
在命令执行结束后,通过如下方法结束当前事务,当前命令的任何变更将被自动存储。
如果你想撤销上一次的命令,通过如下方法撤销事务,那么上次命令执行的结果将被撤销。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 });
var undoManager = spread.undoManager();
_getElementById('undo').addEventListener('click', function() {
undoManager.undo();
});
_getElementById('redo').addEventListener('click', function() {
undoManager.redo();
});
_getElementById('setBackColor').addEventListener('click', function() {
initSpread(spread);
});
};
function initSpread(spread) {
var sheet = spread.getActiveSheet();
var command = {
canUndo: true,
execute: function(spread, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(spread, options);
return true;
} else {
Commands.startTransaction(spread, options);
spread.suspendPaint();
var selections = options.selections;
var value = options.backColor;
selections.forEach(function(sel) {
sheet.getRange(sel.row, sel.col, sel.rowCount, sel.colCount).backColor(value);
});
spread.resumePaint();
Commands.endTransaction(spread, options);
return true;
}
}
};
var selections = sheet.getSelections();
var commandManager = spread.commandManager();
commandManager.register('changeBackColor', command);
commandManager.execute({
cmd: 'changeBackColor',
sheetName: spread.getSheet(0).name(),
selections: selections,
backColor: 'rgb(130, 188, 0)'
});
}
function _getElementById(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-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 id="ss" class="sample-spreadsheets"></div>
<div class="options-container">
<label>Please select a range and click 'Set BackColor' button will set backColor for the range, and click
'Undo' button and 'Redo' button will undo and redo the set backColor action.</label>
<div class="option-row">
<input type="button" id="setBackColor" value="Set BackColor" />
</div>
<div class="option-row">
<input type="button" id="undo" value="Undo" />
</div>
<div class="option-row">
<input type="button" id="redo" value="Redo" />
</div>
<hr>
<div class="option-row">
<label>Use the keyboard shortcuts 'Ctrl-Z' to undo the custom action, and use the keyBoard shortcuts
'Ctrl-Y' to redo the custom action.</label>
</div>
</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;
}
.option-row {
font-size: 14px;
padding: 5px;
}
label {
display: block;
margin-bottom: 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}