JavaScript
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:
<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
divwith the IDsheetis the host element of the editor. The editor needs a visible host with a nonzero width and height, so thedivis 720 pixels high. - The first
scriptelement loads the editor,tx-sheet-control.min.js. The second loadsmain.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.TXSheetControlobject of the standalone script. new Spreadsheet()creates the editor in thesheetelement. 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 isen-USand currency isUSD.await editor.readywaits 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
ItemandAmountand 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'] = editorstores the editor in the global variablesalesEditor.start().catch()writes any error, such as a rejectedready, 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.htmlas a file, with afile://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:
- Import
Spreadsheetand StreamType from@txtextcontrol/tx-sheet-control.StreamTypeselects the file format when you load and save a workbook. - The imported editor installs its bundled styles inside Shadow DOM automatically; no global editor stylesheet is required.
- Copy
dist/fontsanddist/languagesto your public assets, and set fontBaseURL orfontCatalogURLfor 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
- TypeScript: create the editor in a TypeScript module with typed options and save the workbook as XLSX.
- Loading and Saving Workbooks: load and save XLSX, TXWB, PDF, CSV, XML and JSON with
StreamType. - Font Handling: find out how fonts affect the layout and the PDF output, and how the included fonts are deployed.
- Static Assets and Offline Hosting: serve the script, fonts and languages from your own paths.
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.