API
The editor's and the sheet's methods, and the grid, read from the source.
Read from the library's source when this site was built, so it says what the code says. Rows and
columns count from zero: editor.value(0, 0) is A1.
Editor
new Editor({ records?, locale? }). One sheet as one person works on it:
the sheet (its store and history), the selection and the entry being typed. What changes is held
in signals: .get() reads and is reactive in Svelte, .peek() reads
without tracking, .set() writes, .listen() calls you after a change.
From @klementine/svelte-sheet, and from @klementine/svelte-sheet/core where there
is no Svelte.
State
readonly sheet: Sheet- The document: the cells and columns as records, and the rules for changing them.
readonly store: Store<SheetRecord>- Every record of the sheet (
sheet.store): what saving and syncing listen to. readonly history: History<SheetRecord>- Undo and redo of this user's changes (
sheet.history). readonly anchor = atom<Pos>({ row: 0, col: 0 })- The cell that takes what is typed.
readonly focus = atom<Pos>({ row: 0, col: 0 })- The far corner of the selection; the anchor's place when one cell is selected.
readonly editing = atom<Entry | null>(null)- The entry being typed, if a cell is open.
readonly peers = atom<readonly Peer[]>([])- The other people in the sheet right now, for a grid to draw. The app sets them.
Reading
get locale(): string- The reader's language: how numbers read and are typed.
value(row: number, col: number): Value- What a cell works out to.
input(row: number, col: number): string- What is typed into a cell; '' for an empty one.
styleOf(row: number, col: number): CellStyle | undefined- How a cell is shown, if it differs from the usual.
width(col: number): number- A column's width in px.
shown(row: number, col: number): Shown- A cell as it is drawn, in the reader's language.
get empty(): boolean- True when nothing is typed anywhere.
get range(): Range- The selection as a range.
get rangeLabel(): string- The selection by name: "B3" or "B3:D8".
get rows(): number- How many rows a grid shows: what is used and some room below, never past the sheet's edge.
get cols(): number- How many columns a grid shows: what is used and some room beside it.
drawn(from: number, to: number, left = 0, right = Infinity): { row: number; cells: DrawnCell[] }[]- The cells with something to show in rows
from(included) toto(not), row by row, left to right. Withleftandright, only the cells in those columns (leftincluded,rightnot): what a grid draws when it shows a part of a wide sheet. One cell left of them may come along, first in its row: words that run on over the empty cells to their right reach into view from there.
Document
getDocument(): DocumentFile- The whole sheet, to save or to hand to another editor.
loadDocument(records: readonly SheetRecord[], source: 'user' | 'remote' = 'remote')- Replaces the sheet with
records. What is loaded is not an undo step. Asremote(what storage held) nothing is saved or sent for it; asuser(a file this user opened) it is announced like any other change of theirs.
Selecting
select(pos: Pos, extend = false)- Selects a cell; with
extend, stretches the selection from the anchor to it. move(dRow: number, dCol: number, extend = false)- Steps the selection (or, with
extend, its far corner) by rows and columns. selectAll()- Selects everything that is used.
Typing
startEditing(from: Entry['from'], replaceWith?: string)- Opens the anchor cell for typing: with what is in it, or, with
replaceWith, starting over with those characters (the key that was pressed). type(text: string, from: Entry['from'] = 'cell')- Text arrived where an entry is typed (
from: the cell, or a bar above the grid). It is what the open entry reads now; with no entry open, it opens the anchor cell with it. An entry open in the other place is left alone. commit(): boolean- Keeps the entry being typed. True when there was one. An entry that still reads as the cell did when it was opened writes nothing: looking into a cell must not put its old words back over what someone else (an agent, a colleague) has written there in the meantime.
cancel()- Drops the entry being typed.
key({ key, shift = false, mod = false, page }: KeyInfo): boolean- A key was pressed on the grid. True when it was the editor's, so the renderer keeps it from doing anything else; false when it is text on its way into the entry, or a key the editor has no use for.
- While no entry is open: arrows move (with Shift, stretch the selection), PageUp and PageDown step a screen, Home goes to column A (with Ctrl or ⌘, to A1), Tab and Shift+Tab step sideways, Enter or F2 opens the cell with what is in it, Delete and Backspace empty the selection, Escape makes the selection one cell, Ctrl or ⌘ with A selects everything used, with Z, Shift+Z or Y undoes and redoes, with B and I sets bold and italic. While an entry is open: Enter keeps it and goes down (with Shift, up), Tab keeps it and goes right (with Shift, left), Escape drops it.
Changing
clearSelection()- Empties what is typed in the selection; how the cells are shown stays.
setStyle(patch: StylePatch)- Changes how the selected cells are shown.
toggle(key: 'bold' | 'italic')- Bold or italic, on or off, going by the anchor cell.
changeDecimals(delta: 1 | -1)- One decimal place more or fewer, counting from what the anchor cell shows now.
setWidth(col: number, width: number | null)- Sets a column's width in px, as one step;
nullputs it back to the usual. undo()- Takes back this user's last step. An entry being typed is dropped.
redo()
Copying
copy(): string- The selection as text for the clipboard: what the cells show, tab separated. What is typed and how it is shown is remembered too, so a paste within the sheet brings formulas and styles along.
cut(): string- Copies the selection and empties it, as one step.
paste(text: string)- Puts clipboard text down at the selection's top-left corner. Text that is what was copied here last is pasted as cells (formulas moved along, styles kept); anything else is typed in, tab by tab and line by line.
Sheet
new Sheet(records?). The document: the cells and columns as records, and the one
place they are changed. An editor has one (editor.sheet); a server or a script
makes its own. A write through the sheet is not marked in the undo history: the editor does that
for what a person does, and a script calls history.mark() around its own steps.
State
readonly store: Store<SheetRecord>readonly history: History<SheetRecord>- Undo and redo of what this user did. Call
history.mark()between two actions.
Reading
cell(row: number, col: number): CellRecord | undefinedinput(row: number, col: number): string- What is typed into the cell; '' for an empty one.
style(row: number, col: number): CellStyle | undefinedwidth(col: number): number- The column's width in px.
cells(): CellRecord[]- Every cell record, in no particular order.
used(): Range | null- The smallest range that holds every cell with something typed into it; null when there is none.
evaluator(): { value(row: number, col: number): Value }- The sheet's values, worked out from what is typed now (each cell once, on first asking).
Writing
setInput(row: number, col: number, input: string)- Types
inputinto a cell; its style stays. setInputs(top: number, left: number, rows: readonly (readonly (string | undefined)[])[], clip = false)- Types a block of inputs, its top-left corner at (top, left), as one change. An
undefinedin the block leaves that cell as it is. Withclip, what does not fit on the sheet is left out (a paste); without it, a block that does not fit is refused. clearInputs(range: Range)- Empties what is typed in the range; styles stay (what the Delete key does).
clear(range: Range)- Empties the range: what is typed and how it is shown.
setStyle(range: Range, patch: StylePatch)- Changes how every cell of the range is shown; a cell with nothing typed keeps the style too.
setWidth(col: number, width: number | null)- Sets a column's width in px;
nullputs it back to the usual. paste(top: number, left: number, block: readonly (readonly PasteCell[])[])- Puts a copied block down with its top-left corner at (top, left), as one change. What does not fit on the sheet is left out.
Components
From @klementine/svelte-sheet. The classes above also come from @klementine/svelte-sheet/core, the engine alone; the components do not.
Grid- the surface an editor's sheet is shown and worked on. Column letters across the top, row numbers down the side, the cells between them. One cell (or a range) is selected; typing goes into the selected cell. Only the rows and the columns in view are drawn, and of those only the cells with something in them: the lines of the grid are a background, so an empty sheet costs nothing and a large one costs a screenful.
- The grid draws what the editor holds and tells it what happened: a press, a key, text that arrived. What each of them means is the editor's (
editor.key,editor.select,editor.type), so a script does the same without a grid. - One entry field sits on the selected cell and holds the focus the whole time. While the cell is closed it is invisible and empty; whatever is typed into it (by keys, an input method, dictation) opens the cell with those characters. So typing never depends on which key was pressed, only on what text arrived.
- Keys while the cell is closed: arrows move (with Shift, stretch the selection), Tab and Shift+Tab step sideways, Enter or F2 opens the cell with what is in it, Delete empties, ⌘/Ctrl+C, X, V copy, cut and paste, ⌘/Ctrl+Z and Shift+Z (or Y) undo and redo, ⌘/Ctrl+B and I set bold and italic. While it is open: Enter keeps the entry and goes down, Tab keeps it and goes right, Escape drops it.
- The others in the sheet (
editor.peers) show where they are in their colour: their range washed, their cell ringed, their name on a flag that shows when they move and fades after a moment (it stays while they type there). - Fills its parent: give the parent a size. The controls are the app's: the grid brings none. A control that changes the sheet keeps the keys with the grid by not taking the focus (or by calling
focus()on the grid when it is done).
Functions
The engine's functions (addresses, the calculation, number formats, files, edits from code) are listed by what they are for in the package's README, and each has its documentation comment in the source.