How to combine multiple cell edits in one undo action

Posted by: niels on 3 May 2024, 1:17 am EST

    • Post Options:
    • Link

    Posted 3 May 2024, 1:17 am EST

    I dont understand how to do Undo, nor can I find an example

    Is the following code how it should be done?

    And how to combine all 4 commands in one Undo step. Currently I must press 4x Undo. But I have like 100 commands that I want to combine in 1 Undo action.

                var cs = new List<Command>
                {
                    new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 0, 0, 0, 0, "ABC", false, 0),
                    new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 1, 0, 1, 0, "DEF", false, 0),
                    new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 2, 0, 2, 0, "1+2", false, 0, false, 0),
                    new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 3, 0, 3, 0, "3+4", false, 0, false, 0)
                };
                fpSpread1.UndoManager.Execute(cs.ToArray());

  • Posted 6 May 2024, 11:30 am EST

    Hi Niels,

    We are discussions with the developers regarding this requirement. [Internal Tracking ID: SPNET-38153]

    We will update you on this as soon as we hear back from them.

    Thanks & Regards,

    Aastha

  • Posted 14 May 2024, 8:12 pm EST

    Hi Niels,

    As per the information provided by the developers, creating Command directly is not supported officially. This can be confirmed from the fact that the Command class is marked with EditorBrowsable(EditorBrowsableState.Never). It means that Command class is used for internal logic and is not intended to expose it to user.

    However, you can create a combined action which execute and undo all commands at once. Because Command is used internally at this time, the solution may have problem for some rare scenario or may not work with other types of commands. Here is the sample code for the same:

      [code]CombinedAction action = new CombinedAction(
            [
                new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 0, 0, 0, 0, "ABC", false, 0),
                new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 1, 0, 1, 0, "DEF", false, 0),
                new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 2, 0, 2, 0, "1+2", false, 0, false, 0),
                new CellEditCommand(fpSpread1.ActiveSheet.AsWorksheet(), 3, 0, 3, 0, "3+4", false, 0, false, 0)
            ]);
      fpSpread1.UndoManager.PerformUndoAction(action);
    public class CombinedAction : FarPoint.Win.Spread.UndoRedo.UndoAction
    {
      private Command[] _commands;
      public CombinedAction(Command[] commands)
      {
        _commands = commands;
      }
      public override bool PerformUndoAction(object sender)
      {
        SpreadView spreadView = sender as SpreadView;
        GrapeCity.Spreadsheet.Workbook workbook = (GrapeCity.Spreadsheet.Workbook)spreadView.Owner.AsWorkbook();
        for (int i = 0; i < _commands.Length; i++)
        {
          CommandResult result = _commands[i].Execute(workbook);
          if (!result.Success)
          {
            return false;
          }
        }
        return true;
      }
      protected override bool SaveUndoState()
      {
        for (int i = 0; i < _commands.Length; i++)
        {
          _commands[i].SaveUndoState();
        }
        return true;
      }
      public override bool Undo(object sender)
      {
        SpreadView spreadView = sender as SpreadView;
        GrapeCity.Spreadsheet.Workbook workbook = (GrapeCity.Spreadsheet.Workbook)spreadView.Owner.AsWorkbook();
    
        for (int i = 0; i < _commands.Length; i++)
        {
          CommandResult result = _commands[i].Undo();
          if (!result.Success)
          {
            return false;
          }
        }
        return true;
      }
    }
    

    [/code]

    Kindly refer to the attached sample for full implementation. See

    UndoManagerSample_Mod.zip

    Thanks & Regards,

    Aastha

  • Posted 15 May 2024, 4:34 am EST

    Brilliant! Thanks Aastha for the always superb support.

Need extra support?

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

Learn More

Forum Channels