Driving the editor

Everything a person does on the grid, done from code.

The editor has a method for everything a person does on the grid, and the grid uses the same methods. So what you can do with the pointer and the keys, a script can do too: fill a sheet from your data, react to a button outside the grid, or test your own controls without a browser.

A1
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Write cells

The sheet is the editor's document. setInputs types a block, exactly as it would be typed: a number, words, or a formula starting with =. Run it, and the sheet above fills.

import type { Editor } from '@klementine/svelte-sheet';

/** A small table with its totals, as one undo step. */
export default function run(editor: Editor) {
	// Everything between two marks undoes together.
	editor.history.mark();
	editor.sheet.setInputs(0, 0, [
		['Item', 'Units', 'Price', 'Total'],
		['Notebooks', '120', '4.5', '=B2*C2'],
		['Pens', '300', '1.2', '=B3*C3'],
		['Total', '=SUM(B2:B3)', '', '=SUM(D2:D3)']
	]);
	editor.sheet.setStyle({ top: 0, left: 0, bottom: 0, right: 3 }, { bold: true });
	editor.sheet.setStyle({ top: 0, left: 1, bottom: 0, right: 3 }, { align: 'right' });
	editor.sheet.setStyle({ top: 1, left: 2, bottom: 3, right: 3 }, { format: 'eur' });
	editor.history.mark();
	editor.select({ row: 0, col: 0 });
}

Do what a person does

select, type and key are what the grid calls for a press, for text that arrives and for a key. The totals follow, because a cell's value is worked out from what is typed now.

import type { Editor } from '@klementine/svelte-sheet';

/** Types a number into B2 the way a person does: select the cell, type, press Enter. */
export default function run(editor: Editor) {
	editor.select({ row: 1, col: 1 });
	editor.type('240');
	editor.key({ key: 'Enter' });
	return `B2 is ${editor.input(1, 1)}, and the selection went on to ${editor.rangeLabel}.`;
}

Work on the selection

What a toolbar offers is a method too: a number format, decimal places, bold and italic, alignment. Each is one undo step.

import type { Editor } from '@klementine/svelte-sheet';

/** Shows the totals in bold US dollars: a selection, then what a toolbar would call. */
export default function run(editor: Editor) {
	editor.select({ row: 1, col: 3 });
	editor.select({ row: 3, col: 3 }, true);
	editor.setStyle({ format: 'usd', decimals: 0 });
	editor.toggle('bold');
	return `${editor.rangeLabel} is selected.`;
}

Take an edit as data

applySheetOps takes an edit that nobody has checked yet, by address and with typed values, and carries it out or refuses it with a code. It answers the records it changed, to save or to send.

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

/** An edit as plain data, by address: the way an agent's tool or an import hands one over. */
export default function run(editor: Editor) {
	editor.history.mark();
	const { put, remove } = applySheetOps(editor.sheet, {
		write: [{ at: 'A6', rows: [['Average price', null, null, '=ROUND(D4/B4, 2)']] }],
		format: [{ range: 'D6', format: 'eur' }],
		widths: { A: 150 }
	});
	editor.history.mark();
	return `${put.length} records written, ${remove.length} removed.`;
}

Read the sheet

readSheet gives what is typed and what it works out to. The whole sheet is a list of plain records: this is what is saved, sent to others and written to a file.

import { readSheet, type Editor } from '@klementine/svelte-sheet';

/** Reads the sheet: its used range, what it works out to, and the records it is made of. */
export default function run(editor: Editor) {
	const { range, values } = readSheet(editor.sheet);
	if (!range) return 'The sheet is empty.';
	const { records } = editor.getDocument();
	return `${range}: ${values.length} rows, kept as ${records.length} records.`;
}

Undo

Everything done through the editor is one step. The first and the fourth example write to the sheet directly, so they mark the history themselves.

import type { Editor } from '@klementine/svelte-sheet';

/** Takes back the last step, as Ctrl Z would. */
export default function run(editor: Editor) {
	editor.undo();
}

Listen

editor.store.listen calls you after every change with the diff and where it came from. Saving and syncing are built this way, and so is the undo history.

const stop = editor.store.listen((diff, source) => {
	if (source === 'user') send(diff);
});

The selection and the entry being typed are signals with a listen of their own: editor.anchor.listen(() => …).

Without Svelte

A script under Node or a server has no grid. It takes the editor from @klementine/svelte-sheet/core, the engine alone, and calls the same methods.

import { Editor } from '@klementine/svelte-sheet/core';

const editor = new Editor({ locale: 'en' });
editor.type('=6*7');
editor.key({ key: 'Enter' });
editor.value(0, 0); // 42

The methods are listed, with what each does, in the API reference.