Edit this page
Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight

History

The document model provides the possibility to maintain a history stack that tracks all changes to the content of the workbook. The history is implemented via the WorkbookHistory class and the Workbook exposes a property of this type. All changes introduced to the workbook are automatically recorded, however, the model also allows manual control over the history.

Enable / Disable History

By default, the WorkbookHistory class does not record all changes introduced to the workbook, but there are scenarios that need the history feature. For example, if you construct an entire document from code behind, you do not need to record each action you perform. In such cases you do not need to enable the history for the workbook via the IsEnabled property of the WorkbookHistory class. However, if you want to be able to undo one or several of the recent changes, you would need to enable the history.

Example 1 enables the history of a workbook.

[C#] Example 1: Enable history

Workbook workbook = new Workbook();
workbook.History.IsEnabled = true;

Undo / Redo Actions

Once the history is enabled you can invoke its Undo() and Redo() methods to perform undo and redo actions respectively. Both methods return a Boolean value that indicates whether the operations were successful. The Workbook class exposes the Boolean properties CanUndo and CanRedo that indicate whether the respective action is applicable.

Example 2 creates a new workbook with a single worksheet and sets the value of cell A1 twice. Further, the snippet performs the undo and redo actions.

[C#] Example 2: Perform undo and redo

Workbook workbook = new Workbook();
workbook.History.IsEnabled = true;
Worksheet worksheet = workbook.Worksheets.Add();

worksheet.Cells[0, 0].SetValue("First cell value");
worksheet.Cells[0, 0].SetValue("Second cell value");

workbook.History.Undo();
workbook.History.Redo();

Smart Undo

The Workbook history offers a friendly API that allows grouping multiple changes into one undo step. For example, you may want to set the value of a cell and apply formatting to the same cell, and treat these two actions as a single undo operation. This can be easily achieved by enclosing the assignments with BeginUndoGroup() and EndUndoGroup() methods.

Example 3 demonstrates how to create an undo group.

[C#] Example 3: Create undo group

Workbook workbook = new Workbook();
workbook.History.IsEnabled = true;
Worksheet worksheet = workbook.Worksheets.Add();

workbook.History.BeginUndoGroup();

worksheet.Cells[0, 0].SetValue(15);
worksheet.Cells[0, 0].SetFormat(new CellValueFormat("0.00"));

workbook.History.EndUndoGroup();

workbook.History.Undo();
workbook.History.Redo();

Clear History

To clear the history you just have to call the Clear() method of the WorkbookHistory class. Note that you cannot clear the history if you are recording an undo group. If you attempt to call the method after invoking BeginUndoGroup() an exception is be thrown. The following snippet illustrates how to clear workbook's history.

Example 4 clears the history of a workbook.

[C#] Example 4: Clear history

workbook.History.Clear();