TypeScript
TypeScript applications use the same editor npm package as JavaScript applications, @txtextcontrol/tx-sheet-control. It includes declarations for the browser, workbook, dashboard and font APIs and for import and export, so the compiler checks how you call the editor.
Read on to create the editor in a module with typed options, to tell the exports that exist at run time from the types and to use the server package from backend TypeScript.
Install the Package
Install the same package as JavaScript users, as described in Install the npm Packages. The type declarations do not copy assets into your application's public folder: deploy the font and language assets; editor styles are bundled and installed inside Shadow DOM as described in JavaScript.
Write the Code
The following module creates the editor in the element with the ID sheet, fills a column with three revenue values and saves the workbook as XLSX:
import { Spreadsheet, StreamType, type SpreadsheetOptions } from '@txtextcontrol/tx-sheet-control';
const host = document.querySelector<HTMLElement>('#sheet');
if (!host) throw new Error('The spreadsheet host is missing.');
const options: SpreadsheetOptions = { license: 'YOUR_SIGNED_LICENSE', locale: 'en-US', height: 640, fontCatalogURL: '/assets/sheet/fonts/catalog.json' };
const editor = new Spreadsheet(host, options);
await editor.ready;
const sheet = editor.workbook.sheets.at(0);
sheet.cell('A1').setText('Quarterly revenue');
sheet.range('B2:B4').setValues([[12500], [18200], [15400]]);
sheet.cell('B5').setFormula('=SUM(B2:B4)');
const bytes: Uint8Array = await editor.save(StreamType.SpreadsheetML);
The code works as follows:
document.querySelector<HTMLElement>()finds the host element, and the code throws an error when it is missing.optionshas the type SpreadsheetOptions, so the compiler validates its keys: locale isen-US, height is 640 and fontCatalogURL is the URL of the deployed font catalog.SpreadsheetOptionsis an interface, and the import marks it withtype.new Spreadsheet(host, options)creates the editor, a Spreadsheet, andawait editor.readywaits until ready resolves.- workbook, sheets and at return the first worksheet.
- cell and setText write the literal text
Quarterly revenueinto A1. Literal text keeps leading zeros and leading equals signs. - range and setValues write the three values into B2:B4, one value per row.
- setFormula puts the formula
=SUM(B2:B4)into B5. - save with
StreamType.SpreadsheetML, a value of StreamType, returns the workbook as XLSX bytes in aUint8Array.
How Runtime Values and Types Differ
Not every name that the package exports exists at run time:
- Runtime exports.
Spreadsheet, Workbook,StreamTypeand the collection classes are runtime exports. - Interfaces. Interfaces such as
SpreadsheetOptions, DashboardWidget and Format describe API values. Import interfaces withimport typewhen you use verbatim module syntax. Do not call an interface as a constructor. - Handles. Obtain worksheet, table, chart and range handles from their owning collections, the way the code above gets the worksheet from
sheets.
The TypeScript API reference includes the types and the runtime APIs. The JavaScript API reference focuses on the callable runtime exports.
Use TypeScript in a Backend
Backend jobs run without the editor. The rules for backend TypeScript:
- Package. Import the backend controls from
@txtextcontrol/tx-sheet-control-serverand use that package's declarations. The public editor package is not required. - Module settings. Choose module settings compatible with your Node.js application, such as NodeNext for an ES module application.
- Compiler checks. Let the compiler validate option keys, string unions, event names and return types.
- Save options. Use the
SaveOptionsof the package that performs the save: SaveOptions of the editor package in the browser, SaveOptions of the server package in a backend job. - Browser-only parts. Browser host elements and EditorUi, the editor-only state and commands, do not belong in backend jobs.
Node.js shows a first backend job with the server package.
Next Steps
- Events and Cancellation: react to workbook changes and file operations, and cancel them.
- Lifetime and Concurrency: find out who owns a document and when its handles become invalid.
- Node.js: load, change and save a workbook with the server package.
- Static Assets and Offline Hosting: deploy font and language assets, which the declarations do not copy. Editor styles are bundled and installed inside Shadow DOM.
Before running the example, replace YOUR_SIGNED_LICENSE with the contents of your license file, tx-sheet-control.license, which the Text Control Account issues for your Trial, Developer or OEM serial number.