# Workbooks and Worksheets

> Add, rename and address worksheets in JavaScript and .NET, resolve stale handles after a load, group edits with batch and undo them through the history.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Documents > Workbooks and Worksheets
- **1252 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/workbooks/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/workbooks/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/workbooks/llms-full.txt

---

A workbook is the document of TX Sheet Control. It owns the worksheets, named ranges, calculation, history, events and dashboards. The editor displays one active worksheet or dashboard view, while backend code can work with explicit worksheet handles without selecting a UI view.

What follows covers the worksheets and their names, the handles that point into the document, the transactions that group edits and the bounds of the document.

### Add and Rename Worksheets

The following code renames the first worksheet to `Sales`, adds a worksheet `Summary` and sums a column of `Sales` on 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 sales = book.sheets.at(0);
sales.rename('Sales');

const summary = book.sheets.add('Summary');
summary.cell('A1').setText('Revenue');
summary.cell('B1').setFormula("=SUM(Sales!F2:F25)");
```

The code works as follows:

- [`Spreadsheet.workbook`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/spreadsheet/workbook-property/llms-full.txt) returns the [`Workbook`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/llms-full.txt) of the editor.
- [`Workbook.sheets`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/sheets-property/llms-full.txt) returns the worksheets as a [`SheetCollection`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/sheetcollection/llms-full.txt), and [`SheetCollection.at`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/sheetcollection/at-method/llms-full.txt) finds a worksheet by its zero-based position.
- [`Worksheet.rename`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/worksheet/rename-method/llms-full.txt) renames the worksheet and preserves its identity and the qualified cell references to it.
- [`SheetCollection.add`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/sheetcollection/add-method/llms-full.txt) creates the worksheet `Summary` without changing the selection.
- [`Worksheet.cell`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/worksheet/cell-method/llms-full.txt) returns a handle to a cell. [`Cell.setText`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/cell/settext-method/llms-full.txt) writes the literal text `Revenue`, and [`Cell.setFormula`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/cell/setformula-method/llms-full.txt) writes a formula that sums `F2:F25` of the `Sales` worksheet.

Four rules apply to worksheets and their addresses:

- **Indices and addresses.** Collection indices are zero-based. A1 addresses are one-based labels.
- **IDs and handles.** Prefer stable worksheet IDs, such as [`Worksheet.id`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/worksheet/id-property/llms-full.txt), or handles when names can change.
- **Names.** Worksheet names must remain valid and distinct.
- **Tab colors.** A tab color, set with [`Worksheet.setTabColor`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/worksheet/settabcolor-method/llms-full.txt), affects the navigation, not the print or PDF content.

### Resolve Handles After the Document Is Replaced

[`load()`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/load-method/llms-full.txt) replaces the current document atomically. Existing worksheet, range, table, chart and drawing handles can become stale after a replacement or a deletion. Resolve them again after a successful load.

> Warning
>
> **Stale Handles.** Do not catch a stale-handle error and continue writing through the old handle.

### Group Edits Into One Transaction

Group synchronous related edits with `book.batch(() => { ... })`, the [`Workbook.batch`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/batch-method/llms-full.txt) method. A batch can form one undoable transaction and roll back failed changes. Do not use an asynchronous batch callback: fetch the application data first, then collect the synchronous mutations.

For the supported authoring history, call `book.history.undo()` or `redo()` of [`Workbook.history`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/history-property/llms-full.txt).

In .NET, use the worksheet handles returned by [`Workbook.Sheets`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/sheets-property/llms-full.txt) and await the operations. [`BatchAsync`](https://docs.textcontrol.com/tx-sheet-control/reference/dotnet-api/txtextcontrol-txsheetcontrol-server/workbook/batchasync-method/llms-full.txt) collects supported typed commands synchronously. It is not a callback for arbitrary awaited server operations.

### How the Document Bounds Work

Sparse data storage does not imply that every logical row or column is prepopulated. The initial rows and columns, the automatic growth and the maximum limits control the bounds of the document. Scrolling and resizing the host can reveal or grow capacity without stretching the cell dimensions.

Screen zoom differs from the row and column size and from the PDF print scaling.

### Next Steps

- [Cells and Ranges](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/cells/llms-full.txt): resolve cells and ranges on a worksheet and write values, text and formulas.
- [Batch Updates, Undo and Redo](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/llms-full.txt): group application edits into one undoable action.
- [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt): subscribe to workbook and file-operation notifications.
- [Lifetime and Concurrency](https://docs.textcontrol.com/tx-sheet-control/document-processing/lifetime/llms-full.txt): find out who owns a document and when its handles become invalid.
- [Loading and Saving Workbooks](https://docs.textcontrol.com/tx-sheet-control/document-processing/load-save/llms-full.txt): load and save XLSX, TXWB and the other formats with a stream type.

---

## Nearby

- **Previous:** [Spreadsheet Documents](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/llms-full.txt)
- **Next:** [Cells and Ranges](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/cells/llms-full.txt)
