Custom Action

SpreadJS fully supports undo/redo operations and lets the user to define a custom command which supports undo/redo functionality.

If a user wants to perform an undo/redo command, use the startTransaction method. In the following code, the custom command changes will be saved: To end a transaction, use the endTransaction method. In the following code, the custom command changes will be saved: If a user wants to undo the transaction, use the undoTransaction method. In the following code, the command changes will be undone:
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 charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.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; }