# Batch Updates, Undo and Redo

> Group workbook edits into one undo step with Workbook.batch, undo and redo them with Workbook.history and keep history, saving and change events apart.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Editor > Batch Updates, Undo and Redo
- **845 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/llms-full.txt

---

Edits that belong together, such as filling a small table and adding its total, should be one step for the user. Group related application edits in a batch, so that users can undo a logical action and the editor can process a coherent change. Make small independent changes individually when that better matches the interaction.

### Group Edits in a Batch

The following code writes a small table, its total and a bold heading row in one batch, then undoes and redoes it. `editor` is the [Spreadsheet](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/spreadsheet/llms-full.txt) of the browser editor, created as in [JavaScript](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/javascript/llms-full.txt):

```javascript
const book = editor.workbook;
const sheet = book.sheets.at(0);
book.batch(() => {
    sheet.range('A1:B3').setValues([
        ['Item', 'Amount'], ['Hardware', 120], ['Software', 80]
    ]);

    sheet.cell('B4').setFormula('=SUM(B2:B3)');
    sheet.range('A1:B1').format({ font: { bold: true } });
});

book.history.undo();
book.history.redo();
```

The code works as follows:

- [Workbook.batch](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/batch-method/llms-full.txt) runs the callback as one group of document changes. Batched edits emit one change notification.
- [Range.setValues](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/setvalues-method/llms-full.txt) writes the headings and the items as a typed matrix to `A1:B3`.
- [Cell.setFormula](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/cell/setformula-method/llms-full.txt) writes the total `=SUM(B2:B3)` to `B4`.
- [Range.format](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/format-method/llms-full.txt) makes the heading row bold.
- [Workbook.history](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/history-property/llms-full.txt) offers `undo()` and `redo()`, which undo and redo the batch.

> Important
>
> **Synchronous Callback.** The batch callback performs synchronous document changes. Fetch external data before you enter the callback, and do not assume that an asynchronous callback is awaited as a document transaction.

Validate the intended input matrix and the shapes of the options before the mutation.

### How History Differs From Storage

Undo and redo are authoring features. They do not save a file, update a database or restore a data service that your application owns.

Loading another document changes the document state and can invalidate handles. In batch report generation, do not use the history as a replacement for a clean template.

### React to Changes

Observe the [workbook events](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt) for successful changes and for the supported cancelable preflight events. Avoid reentrant changes from notification handlers.

If your application maintains a dirty indicator, distinguish user and document edits from a successful save and from view changes that only concern your application.

### Write Large Amounts of Data

For large writes, use rectangular matrices and respect the limits of each operation. Break workloads into meaningful chunks when appropriate. The number of JavaScript statements is not an indication of the total cost of calculation or rendering.

### Next Steps

- [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt): subscribe to workbook changes and cancel operations.
- [Measuring Performance](https://docs.textcontrol.com/tx-sheet-control/document-processing/performance/llms-full.txt): measure the complete workflow of your application.
- [Lifetime and Concurrency](https://docs.textcontrol.com/tx-sheet-control/document-processing/lifetime/llms-full.txt): understand who owns a document and when handles become invalid.

---

## Nearby

- **Previous:** [Freeze Panes and Selection](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/freeze-panes/llms-full.txt)
- **Next:** [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt)
