如果你想实现一个支持撤销/重做的命令,你可以通过如下方法启动一个事务,命令中的任何行为和数据变更将开启自动存储。
在命令执行结束后,通过如下方法结束当前事务,当前命令的任何变更将被自动存储。
如果你想撤销上一次的命令,通过如下方法撤销事务,那么上次命令执行的结果将被撤销。
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet>
</gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div class="options-row">
<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>
<div class="options-row">
<input type="button" value="Set BackColor" @click="setBackColor()">
</div>
<div class="options-row">
<input type="button" value="Undo" @click="undo()">
</div>
<div class="options-row">
<input type="button" value="Redo" @click="redo()">
</div>
<hr />
<div class="options-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>
</template>
<script>
import Vue from "vue";
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import '@grapecity-software/spread-sheets-vue';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
let App = Vue.extend({
name: "app",
data: function() {
return {
spread: null
};
},
methods: {
initSpread: function(spread) {
this.spread = spread;
},
undo() {
let spread = this.spread;
let undoManager = spread.undoManager();
undoManager.undo();
},
redo() {
let spread = this.spread;
let undoManager = spread.undoManager();
undoManager.redo();
},
setBackColor() {
let spread = this.spread;
let sheet = spread.getActiveSheet();
let command = {
canUndo: true,
execute: function(spread, options, isUndo) {
let Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(spread, options);
return true;
} else {
Commands.startTransaction(spread, options);
spread.suspendPaint();
let selections = options.selections;
let 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;
}
}
};
let selections = sheet.getSelections();
let commandManager = spread.commandManager();
commandManager.register('changeBackColor', command);
commandManager.execute({
cmd: 'changeBackColor',
sheetName: spread.getSheet(0).name(),
selections: selections,
backColor: 'rgb(130, 188, 0)'
});
}
}
});
new Vue({ render: h => h(App) }).$mount('#app');
</script>
<!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$/zh/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/vue/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('./src/app.vue');
System.import('$DEMOROOT$/zh/lib/vue/license.js');
</script>
</head>
<body>
<div id="app"></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;
}
.options-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
label {
display: inline-block;
margin-bottom: 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function(global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' },
'*.vue': { loader: 'vue-loader' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'css': 'npm:systemjs-plugin-css/css.js',
'vue': 'npm:vue/dist/vue.min.js',
'vue-loader': 'npm:systemjs-vue-browser/index.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'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
}
}
});
})(this);