Skip to content

Sheets and cycles

A chain is one formula. A sheet is a set of named columns, and the formula of a column can read the other columns. This is the most familiar part of a spreadsheet: the column total reads the column price and the column count.

root.sheet() starts a sheet. column(name, formula) adds a column, and gives a new sheet. The formula gets a root with one more method: cell(column, at) reads a column at the focus, at a key, or at an address. A move of the address can be an expression, so cell("total", [index(e)]) reads the row at the position that e gives.

const s = root
.sheet()
.column("corner", (r) => r.from("position")._.add("size"))
.column("gapToB", (r) => r.start(r.of("B", "position"))._.subtract(r.cell("corner")));
s.at("A", "corner"); // => some(new Vec2(7, 6))
s.at("A", "gapToB"); // => some(new Vec2(-1, -1))
s.at("A", "gapToB"); // : Optional<Vec2>

The types of the earlier columns go into the later formulas. Thus r.cell("corner") has the type Vec2, and ._ lists the ops of Vec2Domain.

A cell reference is a node of the IR: ext("vex.cell", { column, at }). Thus a column is JSON data, and explain shows each cell read in its trace.

A column can read itself at another key. In an array space, the move offset(-1) goes to the row above. This is a running total, like =B2+C1 filled down a column:

const rows = vex(NumDomain).over(space.array([{ v: 4 }, { v: 1 }, { v: 3 }]));
const total = rows
.sheet()
.declare<{ total: number }>()
.column("total", (r) => r.from("v")._.add(r.start(r.cell("total", [offset(-1)])).ifError(0)));
total.table().map((row) => row.cells.total); // => [ok(4), ok(5), ok(8)]

The first row has no row above, so the read gives #REF!, and ifError(0) changes it to 0. declare gives the type of a column before its formula, so a column can read itself or a later column with that type. The formula of a declared column must give that type. Without declare, cell<T> gives the type.

A sheet evaluates the earlier keys first. Thus a running total over thousands of rows reads finished cells. A sheet does not depend on the call stack of the engine: a chain of 100000 cells gives its value in each order (rule SHEET.DEEP).

A cell can have its own formula. That formula replaces the formula of the column at the key of the cell, and the other cells keep the formula of the column (rule SHEET.CELL.FORMULA). The first row of a running total is the usual case:

const rows = vex(NumDomain).over(space.array([{ v: 4 }, { v: 1 }, { v: 3 }]));
const total = rows
.sheet()
.declare<{ total: number }>()
.column("total", (r) => r.start(r.cell("total", [offset(-1)]))._.add("v"))
.cellFormula("0", "total", (r) => r.from("v"));
total.table().map((row) => row.cells.total); // => [ok(4), ok(5), ok(8)]

The column reads the row above, and the first cell starts the total with its own formula. Thus the column needs no ifError. An error in the formula of the column then shows as an error, and no fallback hides it. The formula of a cell must give a value of the type of its column.

A cell that reads itself, directly or through other cells, has no value. Each cell on such a cycle gives #CYCLE!:

const loop = root
.sheet()
.column("a", (r) => r.start(r.cell<number>("b"))._.add(1))
.column("b", (r) => r.start(r.cell<number>("a")).ifError(0));
loop.result("A", "a"); // => { ok: false, error: { code: "#CYCLE!", ... } }
loop.result("A", "b"); // => { ok: false, error: { code: "#CYCLE!", ... } }

The column b has ifError(0), but it gives #CYCLE! too. If ifError hides a cycle, the result of a cell depends on the cell that the sheet evaluates first. Vex finds the cycles with the algorithm of Tarjan for strongly connected components. Each cell of a component with two or more cells gives #CYCLE!, in each order of evaluation. A property test checks this on random sheets.

A cell that only reads a cycle is not on the cycle. It gets #CYCLE! as a value, and ifError can change it.

A SheetRun evaluates a cell when a read needs it. For a large table, fill() first evaluates each cell in a static order. Each column comes after the columns that it reads, and a running total goes down the rows in key order. Thus each read finds a finished cell, and the results do not change (rule SHEET.FILL).

With the option compile, a run evaluates the formula of each column with generated JavaScript. The compiler tiered of @mark1russell7/vex-compiler gives the results of the interpreter (rule SHEET.COMPILE).

import { app, cell, lit, offset, ref, SheetRun, space } from "@mark1russell7/vex";
import { tiered } from "@mark1russell7/vex-compiler";
import { NumDomain } from "@mark1russell7/vex-domains";
const rows = space.array([{ v: 4 }, { v: 1 }, { v: 3 }]);
const total = app("add", app("ifError", cell("total", [offset(-1)]), lit(0)), ref("v"));
const run = new SheetRun(new Map([["total", total]]), { space: rows, domains: [NumDomain], compile: tiered });
run.fill();
run.cell("total", "2"); // => ok(8)

The sheet app and table() of a sheet use fill(). A fill of 20000 rows takes 13 to 26 milliseconds.

Each column adds its own number and the cells that it reads. Select the reads in the grid. A red column is on a cycle. Select “ifError” for a column to catch the errors of its reads, and see which cells change.

Draw the reads, find the cycles
abcd
readsabcdifError
a
b
c
d
a1 + b#CYCLE!
b2 + a#CYCLE!
c3 + ifError(a, 0)3
d44

In the start state, a and b read each other, so both give #CYCLE!. The column c reads a with ifError, and it is not on the cycle, so it gives 3 + 0. Select “c reads c” to put c on a cycle of its own.