Ctrl+Z Does Not Undo Values Set in Other Cells

Posted by: prince.chauhan on 4 June 2025, 1:22 am EST

    • Post Options:
    • Link

    Posted 4 June 2025, 1:22 am EST

    I’m using SpreadJS with a table where entering a value in a specific column triggers code that sets values in two additional columns (based on the initial input). While the values are correctly set initially, I’ve noticed an issue when performing undo/redo actions using Ctrl+Z or Ctrl+Y:

    The original cell value (the one where data was manually entered) is reverted as expected.

    However, the associated column values that were set programmatically are not reverted or retained accordingly.

    This leads to data inconsistency in the table after undo/redo operations. Please advise on how to ensure the associated column values are also tracked and reverted/restored properly with undo/redo.

  • Posted 5 June 2025, 1:44 am EST - Updated 5 June 2025, 1:49 am EST

    Hi,

    It is possible to perform undo/redo operations using Ctrl+Z/Y on the data set programmatically in the SpreadJS cells using SpreadJS Command Manager: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Commands.CommandManager by wrapping your logic inside a custom command.

    This ensures that changes made to adjacent or related cells are tracked correctly and included in the undo/redo stack.

    const commandManager = spread.commandManager();
    const setAdjacentColumnCommand = {
      canUndo: true,
      execute: function (context, options, isUndo) {
        const Commands = GC.Spread.Sheets.Commands;
        options.cmd = "setAdjacentColumn";
        if (isUndo) {
          Commands.undoTransaction(context, options);
          return true;
        } else {
          Commands.startTransaction(context, options);
          const sheet = context.getSheetFromName(options.sheetName);
          sheet.suspendPaint();
          sheet.setValue(options.row, options.col, options.data);
          sheet.resumePaint();
          Commands.endTransaction(context, options);
          return true;
        }
      }
    };
    
    commandManager.register("setAdjacentColumn", setAdjacentColumnCommand);

    You may also refer to the attached code sample, which demonstrates the implementation of the above snippet and successfully replicates the desired undo/redo behavior (see below).

    Sample: https://jscodemine.mescius.io/share/aMXACc-AV0eiK2l2ebOYQQ/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fapp.js"]%2C"ActiveFile"%3A"%2Fapp.js"}

    GIF:

    Please feel free to reach out if you require any additional assistance.

    Regards,

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels