Undo and Redo in Spreadsheet control

7 Jan 20234 minutes to read

Undo option helps you to undone the last action performed and Redo option helps you to do the same action which is reverted in the Spreadsheet. You can use the allowUndoRedo property to enable or disable undo redo functionality in spreadsheet.

NOTE

  • The default value for allowUndoRedo property is true.

By default, the UndoRedo module is injected internally into Spreadsheet to perform undo redo.

Undo

It reverses the last action you performed with Spreadsheet. Undo can be done by any of the following ways:

  • Select the undo item from HOME tab in Ribbon toolbar.
  • Use Ctrl + Z keyboard shortcut to perform the undo.
  • Use the undo method programmatically.

Redo

It reverses the last undo action you performed with Spreadsheet. Redo can be done by any of the following ways:

  • Select the redo item from HOME tab in Ribbon toolbar.
  • Use Ctrl + Y keyboard shortcut to perform the redo.
  • Use the redo method programmatically.

Update custom actions in UndoRedo collection

You can update your own custom actions in UndoRedo collection, by using the updateUndoRedoCollection method. And also customize the undo redo operations of your custom action by using actionComplete event.

The following code example shows How to update and customize your own actions for undo redo functionality in the Spreadsheet control.

<button id="customBtn" class="e-btn"> add/remove Class</button>

    <ejs-spreadsheet id="spreadsheet" allowUndoRedo="true" actionComplete="onActionComplete">
    
    </ejs-spreadsheet>
<script>
document.getElementById("customBtn").addEventListener('click', updateCollection);

  function onActionComplete(args) {
      var actionEvents = args;
      var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet');
        if (actionEvents.eventArgs.action == "customCSS") {
            var Element = spreadsheetObj.getCell(actionEvents.eventArgs.rowIdx,actionEvents.eventArgs.colIdx);
            if (actionEvents.eventArgs.requestType == "undo") {
                Element.classList.remove('customClass'); // To remove the custom class in undo action
            }
            else {
                Element.classList.add('customClass');// To add the custom class in redo action
            }
        }
    }
    
    function updateCollection() {
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'),
         cell = spreadsheetObj.getActiveSheet().activeCell,
         cellIdx = ej.spreadsheet.getRangeIndexes(cell),
         Element = spreadsheetObj.getCell(cellIdx[0], cellIdx[1]);
        if (!Element.classList.contains("customClass")) {
            Element.classList.add('customClass'); // To add the custom class in active cell element
            spreadsheetObj.updateUndoRedoCollection({ eventArgs: { class: 'customClass', rowIdx: cellIdx[0], colIdx: cellIdx[1], action: 'customCSS' } }); // To update the undo redo collection
        }
    }
</script>

<style>
  .customClass.e-cell {
   background-color: red;
}
</style>
public IActionResult Index()
{
    return View();
}

See Also