Workbooks and Worksheets
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 of the browser editor, created as in 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.workbookreturns theWorkbookof the editor.Workbook.sheetsreturns the worksheets as aSheetCollection, andSheetCollection.atfinds a worksheet by its zero-based position.Worksheet.renamerenames the worksheet and preserves its identity and the qualified cell references to it.SheetCollection.addcreates the worksheetSummarywithout changing the selection.Worksheet.cellreturns a handle to a cell.Cell.setTextwrites the literal textRevenue, andCell.setFormulawrites a formula that sumsF2:F25of theSalesworksheet.
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, or handles when names can change. - Names. Worksheet names must remain valid and distinct.
- Tab colors. A tab color, set with
Worksheet.setTabColor, affects the navigation, not the print or PDF content.
Resolve Handles After the Document Is Replaced
load() 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 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.
In .NET, use the worksheet handles returned by Workbook.Sheets and await the operations. BatchAsync 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: resolve cells and ranges on a worksheet and write values, text and formulas.
- Batch Updates, Undo and Redo: group application edits into one undoable action.
- Events and Cancellation: subscribe to workbook and file-operation notifications.
- Lifetime and Concurrency: find out who owns a document and when its handles become invalid.
- Loading and Saving Workbooks: load and save XLSX, TXWB and the other formats with a stream type.