# History

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

* [Enable / Disable History](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/history#enable-/-disable-history)

* [Undo / Redo Actions](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/history#undo-/-redo-actions)

* [Smart Undo](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/history#smart-undo)

* [Clear History](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/history#clear-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 through the `IsEnabled` property of the `WorkbookHistory` class. However, if you want to undo one or several of the recent changes, you need to enable the history.

**Example 1** enables the history of a workbook.

**Example 1: Enable workbook history tracking**

```C#
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.

**Example 2: Perform undo and redo operations**

```C#
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. You can achieve this by enclosing the assignments with `BeginUndoGroup()` and `EndUndoGroup()` methods.

**Example 3** demonstrates how to create an undo group.

**Example 3: Group multiple changes into one undo step**

```C#
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, call the `Clear()` method of the `WorkbookHistory` class. 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 thrown. The following snippet illustrates how to clear the workbook history.

**Example 4** clears the history of a workbook.

**Example 4: Clear the workbook history stack**

```C#
workbook.History.Clear();
```
