TX Sheet Control

TypeScript

Topic

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.
  • options has the type SpreadsheetOptions, so the compiler validates its keys: locale is en-US, height is 640 and fontCatalogURL is the URL of the deployed font catalog. SpreadsheetOptions is an interface, and the import marks it with type.
  • new Spreadsheet(host, options) creates the editor, a Spreadsheet, and await editor.ready waits until ready resolves.
  • workbook, sheets and at return the first worksheet.
  • cell and setText write the literal text Quarterly revenue into 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 a Uint8Array.

How Runtime Values and Types Differ

Not every name that the package exports exists at run time:

  • Runtime exports. Spreadsheet, Workbook, StreamType and the collection classes are runtime exports.
  • Interfaces. Interfaces such as SpreadsheetOptions, DashboardWidget and Format describe API values. Import interfaces with import type when 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-server and 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 SaveOptions of 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

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.