TX Sheet Control

JavaScript

Topic

A plain HTML page and a few lines of JavaScript are enough to run the spreadsheet editor of TX Sheet Control in the browser. You copy the editor files into your application, create the editor in a div element and fill the first worksheet with values and a formula. No server package is needed.

The editor comes in two forms: the editor npm archive and the standalone files in the release's browser folder. Both include XLSX and PDF, charts, dashboards and fonts. The steps below use the standalone files, and Use npm With a Bundler describes what changes with the npm package.

Copy the Editor Files

Copy the supplied browser folder into your application's sheet static folder, with its fonts, languages and notices. Keep the relative asset folders intact, so that the font and language requests of the editor resolve.

To use the npm archive instead, install it as described in Install the npm Packages.

Create the HTML Page

Create index.html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Sales workbook</title>
</head>
<body>
    <div id="sheet" style="height:720px"></div>
    <script src="./sheet/tx-sheet-control.min.js"></script>
    <script src="./main.js"></script>
</body>
</html>

The page loads the editor and provides its host:

  • The div with the ID sheet is the host element of the editor. The editor needs a visible host with a nonzero width and height, so the div is 720 pixels high.
  • The first script element loads the editor, tx-sheet-control.min.js. The second loads main.js, your code.

Write the Code

In main.js:

async function start() {
    const { Spreadsheet } = TXTextControl.TXSheetControl;
    const editor = new Spreadsheet(document.getElementById('sheet'), {
    license: 'YOUR_SIGNED_LICENSE',
        fontCatalogURL: './sheet/fonts/catalog.json', locale: 'en-US', currency: 'USD'
    });

    await editor.ready;

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

    sheet.cell('B4').setFormula('=SUM(B2:B3)');
    window['salesEditor'] = editor;
}

start().catch(error => console.error(error));

The code works as follows:

  • The code takes the Spreadsheet class from the global TXTextControl.TXSheetControl object of the standalone script.
  • new Spreadsheet() creates the editor in the sheet element. The second argument holds the options, which the SpreadsheetOptions interface of the TypeScript reference describes. fontCatalogURL is the URL of the font catalog in the copied folder; without it, plain JavaScript looks for the catalog at /sheet-fonts/catalog.json. locale is en-US and currency is USD.
  • await editor.ready waits for ready, which resolves after discovery, font-picker initialization and the initial document fonts, and rejects on failure.
  • workbook, sheets and at return the first worksheet. The index is zero-based.
  • range and setValues write the headings Item and Amount and two items into A1:B3. The array has the dimensions of the range, one inner array per row.
  • cell and setFormula put the formula =SUM(B2:B3) into B4. The leading equals sign is required.
  • window['salesEditor'] = editor stores the editor in the global variable salesEditor.
  • start().catch() writes any error, such as a rejected ready, to the console.

Run the Page

Serve the page through HTTP and open it. The value of B4 is 200, the sum of 120 and 80.

Important

Serve the Page Through HTTP. Opening index.html as a file, with a file:// address, does not provide the normal asset-fetch behavior.

Configure license with the contents of your license file, tx-sheet-control.license, before creating the editor. See Licensing for the Trial, Developer and OEM options.

Use npm With a Bundler

With the editor npm package and a bundler, the steps differ from the script-based page:

  1. Import Spreadsheet and StreamType from @txtextcontrol/tx-sheet-control. StreamType selects the file format when you load and save a workbook.
  2. The imported editor installs its bundled styles inside Shadow DOM automatically; no global editor stylesheet is required.
  3. Copy dist/fonts and dist/languages to your public assets, and set fontBaseURL or fontCatalogURL for their deployed location.

Create one editor per host element, await ready before you use it and call destroy when you remove it. The call destroy() takes no arguments.

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.

Isolated Editor Styles

The editor mounts its complete UI in an open Shadow DOM and installs its bundled styles automatically. No global editor stylesheet is required. Bootstrap and other application styles cannot select internal dialogs, ribbon controls, menus or worksheet overlays. Give the host its size and use the public UI API for theme, language, ribbon and area settings.

Workbook APIs, editor APIs, ready and host events work as usual. DOM selectors on the page do not search editor internals. If an integration deliberately inspects internal elements, start at host.shadowRoot; internal markup is not the application API. Destroying the editor clears its owned shadow content and styles. The same host can then be initialized again.