# UndoRedo

Manages undo/redo stacks for all spreadsheet operations.

## oldData: Preserving Formula ASTs Across Irreversible Transformations

Some structural operations (e.g., removing rows/columns, moving cells) destroy
formula information that cannot be reconstructed from the transformation alone.
For example, when a row is removed, formulas referencing that row are rewritten
to `#REF!` — an irreversible change.

To support undo of such operations, `oldData` stores snapshots of formula AST
hashes keyed by the LazilyTransformingAstService version at which the irreversible
transformation was applied. Each entry maps a version number to an array of
`[cellAddress, astHash]` pairs that can be used to restore the original formula
from the parser cache.

### Memory Management

Without cleanup, `oldData` grows indefinitely as undo entries are evicted but
their oldData keys remain. Three mechanisms prevent this:

1. **Eviction cleanup**: When undo entries are evicted (due to `undoLimit`),
   `cleanupOldDataForEntries()` deletes their referenced oldData keys
   (unless still needed by entries on the other stack).
2. **Orphan cleanup**: Compaction may force lazy formula evaluation, which
   writes new oldData entries for already-evicted undo entries. After compaction,
   `cleanupOrphanedOldData()` removes any keys not referenced by entries on
   either stack or the in-progress batch.
3. **Short-circuit**: When `undoLimit` is 0 (undo disabled),
   `storeDataForVersion()` returns immediately to avoid storing data that
   would never be used.

## Constructors

### constructor 

\+ **new UndoRedo**(`config`: [Config](config.md), `operations`: [Operations](operations.md)): *[UndoRedo](undoredo.md)*

*Defined in [src/UndoRedo.ts:506](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L506)*

**Parameters:**

Name | Type |
------ | ------ |
`config` | [Config](config.md) |
`operations` | [Operations](operations.md) |

**Returns:** *[UndoRedo](undoredo.md)*

## Properties

### oldData 

• **oldData**: *Map‹number, [[SimpleCellAddress](../interfaces/simplecelladdress.md), string][]›* = new Map()

*Defined in [src/UndoRedo.ts:502](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L502)*

## Methods

### beginBatchMode 

▸ **beginBatchMode**(): *void*

*Defined in [src/UndoRedo.ts:523](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L523)*

**Returns:** *void*

___

### cleanupOrphanedOldData 

▸ **cleanupOrphanedOldData**(): *void*

*Defined in [src/UndoRedo.ts:887](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L887)*

Removes oldData entries whose version keys are not referenced by any
entry on the undo stack, redo stack, or in-progress batch. Called after
compaction forces lazy formula evaluation, which may insert oldData for
already-evicted entries.

**Returns:** *void*

___

### clearRedoStack 

▸ **clearRedoStack**(): *void*

*Defined in [src/UndoRedo.ts:551](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L551)*

Clears the redo stack and removes oldData entries no longer referenced by any remaining entry.

**Returns:** *void*

___

### clearUndoStack 

▸ **clearUndoStack**(): *void*

*Defined in [src/UndoRedo.ts:557](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L557)*

Clears the undo stack and removes oldData entries no longer referenced by any remaining entry.

**Returns:** *void*

___

### commitBatchMode 

▸ **commitBatchMode**(): *void*

*Defined in [src/UndoRedo.ts:527](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L527)*

**Returns:** *void*

___

### isRedoStackEmpty 

▸ **isRedoStackEmpty**(): *boolean*

*Defined in [src/UndoRedo.ts:566](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L566)*

**Returns:** *boolean*

___

### isUndoStackEmpty 

▸ **isUndoStackEmpty**(): *boolean*

*Defined in [src/UndoRedo.ts:562](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L562)*

**Returns:** *boolean*

___

### redo 

▸ **redo**(): *void*

*Defined in [src/UndoRedo.ts:763](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L763)*

**Returns:** *void*

___

### redoAddColumns 

▸ **redoAddColumns**(`operation`: [AddColumnsUndoEntry](addcolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:815](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L815)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddColumnsUndoEntry](addcolumnsundoentry.md) |

**Returns:** *void*

___

### redoAddNamedExpression 

▸ **redoAddNamedExpression**(`operation`: [AddNamedExpressionUndoEntry](addnamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:848](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L848)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddNamedExpressionUndoEntry](addnamedexpressionundoentry.md) |

**Returns:** *void*

___

### redoAddRows 

▸ **redoAddRows**(`operation`: [AddRowsUndoEntry](addrowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:811](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L811)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddRowsUndoEntry](addrowsundoentry.md) |

**Returns:** *void*

___

### redoAddSheet 

▸ **redoAddSheet**(`operation`: [AddSheetUndoEntry](addsheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:823](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L823)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddSheetUndoEntry](addsheetundoentry.md) |

**Returns:** *void*

___

### redoBatch 

▸ **redoBatch**(`batchOperation`: [BatchUndoEntry](batchundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:775](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L775)*

**Parameters:**

Name | Type |
------ | ------ |
`batchOperation` | [BatchUndoEntry](batchundoentry.md) |

**Returns:** *void*

___

### redoChangeNamedExpression 

▸ **redoChangeNamedExpression**(`operation`: [ChangeNamedExpressionUndoEntry](changenamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:856](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L856)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [ChangeNamedExpressionUndoEntry](changenamedexpressionundoentry.md) |

**Returns:** *void*

___

### redoClearSheet 

▸ **redoClearSheet**(`operation`: [ClearSheetUndoEntry](clearsheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:839](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L839)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [ClearSheetUndoEntry](clearsheetundoentry.md) |

**Returns:** *void*

___

### redoMoveCells 

▸ **redoMoveCells**(`operation`: [MoveCellsUndoEntry](movecellsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:785](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L785)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveCellsUndoEntry](movecellsundoentry.md) |

**Returns:** *void*

___

### redoMoveColumns 

▸ **redoMoveColumns**(`operation`: [MoveColumnsUndoEntry](movecolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:835](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L835)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveColumnsUndoEntry](movecolumnsundoentry.md) |

**Returns:** *void*

___

### redoMoveRows 

▸ **redoMoveRows**(`operation`: [MoveRowsUndoEntry](moverowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:831](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L831)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveRowsUndoEntry](moverowsundoentry.md) |

**Returns:** *void*

___

### redoPaste 

▸ **redoPaste**(`operation`: [PasteUndoEntry](pasteundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:793](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L793)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [PasteUndoEntry](pasteundoentry.md) |

**Returns:** *void*

___

### redoRemoveColumns 

▸ **redoRemoveColumns**(`operation`: [RemoveColumnsUndoEntry](removecolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:789](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L789)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveColumnsUndoEntry](removecolumnsundoentry.md) |

**Returns:** *void*

___

### redoRemoveNamedExpression 

▸ **redoRemoveNamedExpression**(`operation`: [RemoveNamedExpressionUndoEntry](removenamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:852](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L852)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveNamedExpressionUndoEntry](removenamedexpressionundoentry.md) |

**Returns:** *void*

___

### redoRemoveRows 

▸ **redoRemoveRows**(`operation`: [RemoveRowsUndoEntry](removerowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:781](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L781)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveRowsUndoEntry](removerowsundoentry.md) |

**Returns:** *void*

___

### redoRemoveSheet 

▸ **redoRemoveSheet**(`operation`: [RemoveSheetUndoEntry](removesheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:819](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L819)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveSheetUndoEntry](removesheetundoentry.md) |

**Returns:** *void*

___

### redoRenameSheet 

▸ **redoRenameSheet**(`operation`: [RenameSheetUndoEntry](renamesheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:827](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L827)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RenameSheetUndoEntry](renamesheetundoentry.md) |

**Returns:** *void*

___

### redoSetCellContents 

▸ **redoSetCellContents**(`operation`: [SetCellContentsUndoEntry](setcellcontentsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:805](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L805)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetCellContentsUndoEntry](setcellcontentsundoentry.md) |

**Returns:** *void*

___

### redoSetColumnOrder 

▸ **redoSetColumnOrder**(`operation`: [SetColumnOrderUndoEntry](setcolumnorderundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:864](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L864)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetColumnOrderUndoEntry](setcolumnorderundoentry.md) |

**Returns:** *void*

___

### redoSetRowOrder 

▸ **redoSetRowOrder**(`operation`: [SetRowOrderUndoEntry](setroworderundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:860](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L860)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetRowOrderUndoEntry](setroworderundoentry.md) |

**Returns:** *void*

___

### redoSetSheetContent 

▸ **redoSetSheetContent**(`operation`: [SetSheetContentUndoEntry](setsheetcontentundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:843](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L843)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetSheetContentUndoEntry](setsheetcontentundoentry.md) |

**Returns:** *void*

___

### saveOperation 

▸ **saveOperation**(`operation`: [UndoEntry](../interfaces/undoentry.md)): *void*

*Defined in [src/UndoRedo.ts:515](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L515)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [UndoEntry](../interfaces/undoentry.md) |

**Returns:** *void*

___

### storeDataForVersion 

▸ **storeDataForVersion**(`version`: number, `address`: [SimpleCellAddress](../interfaces/simplecelladdress.md), `astHash`: string): *void*

*Defined in [src/UndoRedo.ts:539](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L539)*

Stores a formula AST hash snapshot for the given LazilyTransformingAstService version.
Skipped when `undoLimit` is 0 (undo disabled) to avoid storing data that would never be used.

**Parameters:**

Name | Type |
------ | ------ |
`version` | number |
`address` | [SimpleCellAddress](../interfaces/simplecelladdress.md) |
`astHash` | string |

**Returns:** *void*

___

### undo 

▸ **undo**(): *void*

*Defined in [src/UndoRedo.ts:570](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L570)*

**Returns:** *void*

___

### undoAddColumns 

▸ **undoAddColumns**(`operation`: [AddColumnsUndoEntry](addcolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:627](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L627)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddColumnsUndoEntry](addcolumnsundoentry.md) |

**Returns:** *void*

___

### undoAddNamedExpression 

▸ **undoAddNamedExpression**(`operation`: [AddNamedExpressionUndoEntry](addnamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:743](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L743)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddNamedExpressionUndoEntry](addnamedexpressionundoentry.md) |

**Returns:** *void*

___

### undoAddRows 

▸ **undoAddRows**(`operation`: [AddRowsUndoEntry](addrowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:619](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L619)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddRowsUndoEntry](addrowsundoentry.md) |

**Returns:** *void*

___

### undoAddSheet 

▸ **undoAddSheet**(`operation`: [AddSheetUndoEntry](addsheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:685](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L685)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [AddSheetUndoEntry](addsheetundoentry.md) |

**Returns:** *void*

___

### undoBatch 

▸ **undoBatch**(`batchOperation`: [BatchUndoEntry](batchundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:581](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L581)*

**Parameters:**

Name | Type |
------ | ------ |
`batchOperation` | [BatchUndoEntry](batchundoentry.md) |

**Returns:** *void*

___

### undoChangeNamedExpression 

▸ **undoChangeNamedExpression**(`operation`: [ChangeNamedExpressionUndoEntry](changenamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:751](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L751)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [ChangeNamedExpressionUndoEntry](changenamedexpressionundoentry.md) |

**Returns:** *void*

___

### undoClearSheet 

▸ **undoClearSheet**(`operation`: [ClearSheetUndoEntry](clearsheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:718](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L718)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [ClearSheetUndoEntry](clearsheetundoentry.md) |

**Returns:** *void*

___

### undoMoveCells 

▸ **undoMoveCells**(`operation`: [MoveCellsUndoEntry](movecellsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:673](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L673)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveCellsUndoEntry](movecellsundoentry.md) |

**Returns:** *void*

___

### undoMoveColumns 

▸ **undoMoveColumns**(`operation`: [MoveColumnsUndoEntry](movecolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:666](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L666)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveColumnsUndoEntry](movecolumnsundoentry.md) |

**Returns:** *void*

___

### undoMoveRows 

▸ **undoMoveRows**(`operation`: [MoveRowsUndoEntry](moverowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:659](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L659)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [MoveRowsUndoEntry](moverowsundoentry.md) |

**Returns:** *void*

___

### undoPaste 

▸ **undoPaste**(`operation`: [PasteUndoEntry](pasteundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:652](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L652)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [PasteUndoEntry](pasteundoentry.md) |

**Returns:** *void*

___

### undoRemoveColumns 

▸ **undoRemoveColumns**(`operation`: [RemoveColumnsUndoEntry](removecolumnsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:603](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L603)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveColumnsUndoEntry](removecolumnsundoentry.md) |

**Returns:** *void*

___

### undoRemoveNamedExpression 

▸ **undoRemoveNamedExpression**(`operation`: [RemoveNamedExpressionUndoEntry](removenamedexpressionundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:747](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L747)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveNamedExpressionUndoEntry](removenamedexpressionundoentry.md) |

**Returns:** *void*

___

### undoRemoveRows 

▸ **undoRemoveRows**(`operation`: [RemoveRowsUndoEntry](removerowsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:587](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L587)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveRowsUndoEntry](removerowsundoentry.md) |

**Returns:** *void*

___

### undoRemoveSheet 

▸ **undoRemoveSheet**(`operation`: [RemoveSheetUndoEntry](removesheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:690](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L690)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RemoveSheetUndoEntry](removesheetundoentry.md) |

**Returns:** *void*

___

### undoRenameSheet 

▸ **undoRenameSheet**(`operation`: [RenameSheetUndoEntry](renamesheetundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:708](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L708)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [RenameSheetUndoEntry](renamesheetundoentry.md) |

**Returns:** *void*

___

### undoSetCellContents 

▸ **undoSetCellContents**(`operation`: [SetCellContentsUndoEntry](setcellcontentsundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:635](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L635)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetCellContentsUndoEntry](setcellcontentsundoentry.md) |

**Returns:** *void*

___

### undoSetColumnOrder 

▸ **undoSetColumnOrder**(`operation`: [SetColumnOrderUndoEntry](setcolumnorderundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:759](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L759)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetColumnOrderUndoEntry](setcolumnorderundoentry.md) |

**Returns:** *void*

___

### undoSetRowOrder 

▸ **undoSetRowOrder**(`operation`: [SetRowOrderUndoEntry](setroworderundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:755](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L755)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetRowOrderUndoEntry](setroworderundoentry.md) |

**Returns:** *void*

___

### undoSetSheetContent 

▸ **undoSetSheetContent**(`operation`: [SetSheetContentUndoEntry](setsheetcontentundoentry.md)): *void*

*Defined in [src/UndoRedo.ts:730](https://github.com/handsontable/hyperformula/blob/e410ed0/src/UndoRedo.ts#L730)*

**Parameters:**

Name | Type |
------ | ------ |
`operation` | [SetSheetContentUndoEntry](setsheetcontentundoentry.md) |

**Returns:** *void*