Getting started

From installing it to a sheet on your own page, and where its controls come from.

Install

npm install @klementine/svelte-sheet @klementine/svelte-draw

svelte-sheet needs Svelte 5.57 or newer. It needs no CSS framework and no component kit: the styles are plain CSS. The second package is where the cells live: a sheet keeps its records in the store of svelte-draw. Only that library’s engine is used, so an app’s bundle gets the store, not the canvas.

The engine alone, for a server or a script without Svelte, is @klementine/svelte-sheet/core: plain TypeScript that runs in Node as well as in a browser.

A sheet on a page

The grid shows an editor’s sheet and tells the editor what the pointer and the keys did. It fills its parent, so give the parent a size.

<script lang="ts">
	import { Editor, Grid } from '@klementine/svelte-sheet';
	import '@klementine/svelte-sheet/sheet.css';

	const editor = new Editor();
</script>

<div style="height: 100dvh">
	<Grid {editor} label="Budget" />
</div>

That is a spreadsheet already. Click a cell and type; =SUM(A1:A9) works out; the arrows move, Enter goes down and Tab goes right; Ctrl Z takes a step back; Ctrl C and Ctrl V copy and paste. The grid takes the page’s typeface.

Start with something in it

An editor starts empty, or with the records you give it. applySheetOps makes them from typed values and formats by address, the way a script or an import would:

import { applySheetOps, Editor, Sheet } from '@klementine/svelte-sheet';

const sheet = new Sheet();
applySheetOps(sheet, {
	write: [
		{
			at: 'A1',
			rows: [
				['Item', 'Price'],
				['Flour', 1.5],
				['Sugar', 0.9],
				['Total', '=SUM(B2:B3)']
			]
		}
	],
	format: [
		{ range: 'A1:B1', bold: true },
		{ range: 'B2:B4', format: 'eur' }
	]
});

const editor = new Editor({ records: sheet.store.snapshot() });

What an editor starts with is not an undo step.

Controls

The library brings no toolbar and no formula bar. They belong to your app and should look like it. There are two ways to get them.

Build your own on the editor. Everything a control does is a call on the editor, and what a control shows is read from it. What is read is reactive in a template, so a control stays in step by itself:

<div class="tools">
	<button
		disabled={!editor.history.canUndo.get()}
		onmousedown={(event) => event.preventDefault()}
		onclick={() => editor.undo()}
	>
		Undo
	</button>
	<button onmousedown={(event) => event.preventDefault()} onclick={() => editor.toggle('bold')}>
		Bold
	</button>
	<output>{editor.rangeLabel}</output>
</div>
<Grid {editor} label="Budget" />

The prevented mousedown keeps the keys with the grid: a press on a button does not take the focus from the cell, so typing goes on after a click. Driving the editor lists what else a control can call: number formats, alignment, column widths, the clipboard.

Copy the example’s. The spreadsheet example has a toolbar with undo and redo, bold and italic, alignment, number formats and decimal places; a formula bar; a File menu that opens CSV, TSV and sheet files and downloads the sheet; and Spreadsheet, which puts them above the grid. The demo is that example. They are Svelte components on bits-ui (menus, hints and bars that work with a keyboard) and one plain stylesheet whose colours are a handful of properties. Copy the folder and make it yours. Look and controls shows the seams.

Keep the sheet

A sheet lasts as long as the page. Keeping it is listening to the editor’s store, where every change arrives, whoever made it:

import { Editor, parseSheetFile, toSheetFile } from '@klementine/svelte-sheet';

const kept = localStorage.getItem('my-sheet');
const editor = new Editor({ records: kept ? parseSheetFile(kept).records : [] });

editor.store.listen(() => {
	const file = toSheetFile('Budget', editor.store.snapshot());
	localStorage.setItem('my-sheet', JSON.stringify(file));
});

Saving and syncing has the rest: single records instead of the whole sheet, your own server, files, other people.

What Grid takes

PropWhat it does
editorthe editor it shows and works on
labelthe sheet’s name for a screen reader
labelsthe grid’s own words in another language; what is left out stays English
schemelight (the default), dark, or auto to follow the system
autofocuswhether the grid takes the keys when it appears; true unless set to false

bind:this gives you the grid’s focus(), which hands the keys back to it after a control of yours had them (a menu that closed, a field above the grid).