Sheet made by Base shows #NAME? in every running total cell
The sheet engine was missing ROW and COLUMN. Those are the functions a running-total grid like this one uses to work out "which day is this cell", so every grid cell showed #NAME?, which is the error for an unknown function name. I couldn't open your sheet's saved formulas from here, so I added the functions that formula would most likely use: ROW and COLUMN (they work with no argument for the formula's own cell, or with a cell or range given), and INT. MOD and MIN were already there, so a formula like "the smaller of 26,500 and 100 × day number" now gives 100 for Week 1 Day 1, 700 for Day 7, and stops at 26,500. The new tests rebuild your grid: they check the totals and the cap, check that changing the €100 updates the grid, and check that the totals stay right when the grid starts a row lower. The calculation tests pass. I couldn't run the storage-layer tests because a package they need wasn't available offline. If the sheet still shows an error after this change, it uses a function I didn't guess, so send me one of its formulas. Equipo task tk5usdu3hmr5, Engineering step attempt 1.
3 files changed,
+203
−1
internal/sheets/calc/fn_ref.go+84 −0internal/sheets/calc/fn_ref_test.go+106 −0internal/sheets/calc/graph_recalc.go+13 −1
CI passed — run details
CI log
amadan: .amadan/timeout: this job's budget is 15m0s (this runner allows up to 30m0s) === vet === CGO_ENABLED=0 go vet ./... CGO_ENABLED=0 go vet -tags browser ./internal/sheets/ === spacing === go tool fiesta spacing -skip 'vendor/*.css' -skip 'tokens.css' internal/sheets/static === fmt === === build === CGO_ENABLED=0 go build ./... === migrations === go tool rastrillo migration check rastrillo migration check: models and migrations agree === go-test === CGO_ENABLED=0 go test -count=1 ./... ? amadan.net/oficina/sheets/cmd/sheets [no test files] ? amadan.net/oficina/sheets/cmd/sheets-contract [no test files] ok amadan.net/oficina/sheets/internal/guardstest 0.177s ok amadan.net/oficina/sheets/internal/sheets 16.822s ok amadan.net/oficina/sheets/internal/sheets/calc 0.016s ok amadan.net/oficina/sheets/internal/sheets/format 0.007s ? amadan.net/oficina/sheets/internal/sheets/keymailtest [no test files] ok amadan.net/oficina/sheets/internal/sheets/live 0.053s ok amadan.net/oficina/sheets/internal/sheets/seal 0.003s ok amadan.net/oficina/sheets/internal/sheets/sealtest 0.003s ok amadan.net/oficina/sheets/internal/sheets/store 6.614s ok amadan.net/oficina/sheets/internal/sheets/teamstate 0.157s ok amadan.net/oficina/sheets/internal/sheets/xlsx 5.231s ? amadan.net/oficina/sheets/tools/gencorpus [no test files] === race === go test -race -count=1 ./internal/sheets/store/... ./internal/sheets/live/... ok amadan.net/oficina/sheets/internal/sheets/store 74.238s ok amadan.net/oficina/sheets/internal/sheets/live 1.064s
diff --git a/internal/sheets/calc/fn_ref.go b/internal/sheets/calc/fn_ref.go| new file mode 100644 |
| index 0000000..24c5a5c |
| --- /dev/null |
| +++ b/internal/sheets/calc/fn_ref.go |
| @@ -0,0 +1,84 @@ |
| +package calc |
| + |
| +import "math" |
| + |
| +// fn_ref.go: ROW, COLUMN and INT. ROW and COLUMN report a position, not a |
| +// value: with a reference argument they read its top-left cell's |
| +// position straight off the AST (never env.CellValue — the referenced |
| +// cell's content is irrelevant), and with no argument they need the cell |
| +// the formula itself lives in, which only the dependency graph knows (see |
| +// callerEnv). That is why they are nodeFuncs. INT sits here because the |
| +// positional formulas ROW/COLUMN exist for (week/day grids, "every Nth |
| +// row") are its usual company. |
| + |
| +func init() { |
| + registerNode("ROW", fnRow) |
| + registerNode("COLUMN", fnColumn) |
| + Register("INT", fnInt) |
| +} |
| + |
| +// callerEnv is the optional interface an Env implements when it knows |
| +// which cell is being evaluated. The graph's per-cell view (cellEnv, |
| +// graph_recalc.go) does; a bare Env evaluating a free-standing formula |
| +// does not, and ROW()/COLUMN() with no argument is #VALUE! there. |
| +type callerEnv interface { |
| + Caller() (CellKey, bool) |
| +} |
| + |
| +func fnRow(argNodes []Node, env Env) Value { |
| + return positionOf(argNodes, env, true) |
| +} |
| + |
| +func fnColumn(argNodes []Node, env Env) Value { |
| + return positionOf(argNodes, env, false) |
| +} |
| + |
| +// positionOf is ROW/COLUMN's shared body: the 1-based row (or column) of |
| +// the argument's top-left cell, or of the calling cell when there is no |
| +// argument. A non-reference argument is #VALUE!. |
| +func positionOf(argNodes []Node, env Env, row bool) Value { |
| + var r, c int |
| + switch len(argNodes) { |
| + case 0: |
| + ce, ok := env.(callerEnv) |
| + if !ok { |
| + return errValue(ErrValue) |
| + } |
| + k, ok := ce.Caller() |
| + if !ok { |
| + return errValue(ErrValue) |
| + } |
| + r, c = int(k.RowID)-1, int(k.ColID)-1 |
| + case 1: |
| + switch a := argNodes[0].(type) { |
| + case *Ref: |
| + r, c = a.RowPos, a.ColPos |
| + case *Range: |
| + r, _, c, _ = rangeBounds(a) |
| + default: |
| + return errValue(ErrValue) |
| + } |
| + default: |
| + return errValue(ErrValue) |
| + } |
| + if row { |
| + return Value{Kind: KindNum, Num: float64(r + 1)} |
| + } |
| + return Value{Kind: KindNum, Num: float64(c + 1)} |
| +} |
| + |
| +// fnInt implements INT(number): round down to the nearest integer, so |
| +// negative numbers move away from zero (INT(-1.5) = -2), as in Excel. |
| +func fnInt(args []Value) Value { |
| + if len(args) != 1 { |
| + return errValue(ErrValue) |
| + } |
| + if args[0].Kind == KindErr { |
| + return args[0] |
| + } |
| + x, ek := toNumber(args[0]) |
| + if ek != ErrNone { |
| + return errValue(ek) |
| + } |
| + return Value{Kind: KindNum, Num: math.Floor(x)} |
| +} |
diff --git a/internal/sheets/calc/fn_ref_test.go b/internal/sheets/calc/fn_ref_test.go| new file mode 100644 |
| index 0000000..8750d92 |
| --- /dev/null |
| +++ b/internal/sheets/calc/fn_ref_test.go |
| @@ -0,0 +1,106 @@ |
| +package calc |
| + |
| +import "testing" |
| + |
| +func TestRowColumnOfReference(t *testing.T) { |
| + cases := map[string]float64{ |
| + "ROW(C7)": 7, |
| + "COLUMN(C7)": 3, |
| + "ROW($B$12)": 12, |
| + "ROW(B4:D9)": 4, |
| + "COLUMN(D2:B9)": 2, |
| + } |
| + for src, want := range cases { |
| + if got := evalNum(t, src, emptyEnv{}); got != want { |
| + t.Errorf("%d-char formula = %v, want %v", len(src), got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestRowColumnBadArguments(t *testing.T) { |
| + for _, src := range []string{"ROW(5)", "COLUMN(A1,B1)", "ROW()", "COLUMN()"} { |
| + v := Eval(mustParse(t, src), emptyEnv{}) |
| + if v.Kind != KindErr || v.Err != ErrValue { |
| + t.Errorf("%d-char formula = %+v, want #VALUE!", len(src), v) |
| + } |
| + } |
| +} |
| + |
| +func TestRowColumnOfCallingCell(t *testing.T) { |
| + g := NewGraph() |
| + d7 := CellKey{RowID: 7, ColID: 4} |
| + g.SetFormula(d7, mustParse(t, "ROW()*100+COLUMN()")) |
| + if v := g.Value(d7, emptyEnv{}); v.Kind != KindNum || v.Num != 704 { |
| + t.Fatalf("D7 = %+v, want 704", v) |
| + } |
| + // A formula reading D7 sees D7's own position, not the reader's. |
| + e1 := CellKey{RowID: 1, ColID: 5} |
| + g.SetFormula(e1, mustParse(t, "D7+ROW()")) |
| + if v := g.Value(e1, emptyEnv{}); v.Kind != KindNum || v.Num != 705 { |
| + t.Fatalf("E1 = %+v, want 705", v) |
| + } |
| +} |
| + |
| +func TestInt(t *testing.T) { |
| + cases := map[string]float64{"INT(8.9)": 8, "INT(-8.9)": -9, "INT(3)": 3, "INT(\"2.5\")": 2} |
| + for src, want := range cases { |
| + if got := evalNum(t, src, emptyEnv{}); got != want { |
| + t.Errorf("%d-char formula = %v, want %v", len(src), got, want) |
| + } |
| + } |
| +} |
| + |
| +// runningTotalGrid builds the "€100 a day for 265 days" sheet: B1 daily |
| +// amount, B2 days, B3 =B1*B2, Day 1..7 across B..H, Week rows from |
| +// firstRow down, each cell the running total capped at B3. |
| +func runningTotalGrid(t *testing.T, firstRow, weeks int) (*Graph, mapEnv) { |
| + t.Helper() |
| + g := NewGraph() |
| + inputs := mapEnv{ |
| + {RowID: 1, ColID: 2}: {Kind: KindNum, Num: 100}, |
| + {RowID: 2, ColID: 2}: {Kind: KindNum, Num: 265}, |
| + } |
| + g.SetFormula(CellKey{RowID: 3, ColID: 2}, mustParse(t, "B1*B2")) |
| + src := "MIN($B$3,$B$1*((ROW()-ROW($B$" + itoa(firstRow) + "))*7+COLUMN()-1))" |
| + for w := 0; w < weeks; w++ { |
| + for d := 0; d < 7; d++ { |
| + g.SetFormula(CellKey{RowID: uint64(firstRow + w), ColID: uint64(2 + d)}, mustParse(t, src)) |
| + } |
| + } |
| + return g, inputs |
| +} |
| + |
| +func TestRunningTotalGrid(t *testing.T) { |
| + g, inputs := runningTotalGrid(t, 6, 40) |
| + check := func(row, col int, want float64) { |
| + t.Helper() |
| + v := g.Value(CellKey{RowID: uint64(row), ColID: uint64(col)}, inputs) |
| + if v.Kind != KindNum || v.Num != want { |
| + t.Errorf("R%dC%d = %+v, want %v", row, col, v, want) |
| + } |
| + } |
| + check(6, 2, 100) // Week 1 Day 1 |
| + check(6, 8, 700) // Week 1 Day 7 |
| + check(7, 2, 800) // Week 2 Day 1 |
| + check(43, 7, 26500) // day 265: Week 38 Day 6 |
| + check(43, 8, 26500) // day 266 capped |
| + check(45, 8, 26500) |
| + |
| + // Changing the daily amount recalculates the grid. |
| + inputs[CellKey{RowID: 1, ColID: 2}] = Value{Kind: KindNum, Num: 50} |
| + g.Recalc(CellKey{RowID: 1, ColID: 2}, inputs) |
| + check(6, 8, 350) |
| +} |
| + |
| +// After a row is inserted above the grid, the graph is rebuilt with every |
| +// formula at its new position (store's buildHandle; references shift the |
| +// same way). The positional formulas must give the same answers. |
| +func TestRunningTotalGridAfterRowInsert(t *testing.T) { |
| + g, inputs := runningTotalGrid(t, 7, 2) |
| + for _, c := range [][3]float64{{7, 2, 100}, {7, 8, 700}, {8, 2, 800}} { |
| + v := g.Value(CellKey{RowID: uint64(c[0]), ColID: uint64(c[1])}, inputs) |
| + if v.Kind != KindNum || v.Num != c[2] { |
| + t.Errorf("R%vC%v = %+v, want %v", c[0], c[1], v, c[2]) |
| + } |
| + } |
| +} |
diff --git a/internal/sheets/calc/graph_recalc.go b/internal/sheets/calc/graph_recalc.go| index 3966053..e05a36a 100644 |
| --- a/internal/sheets/calc/graph_recalc.go |
| +++ b/internal/sheets/calc/graph_recalc.go |
| @@ -76,9 +76,21 @@ func (e *graphEnv) CellValue(k CellKey) Value { |
| return Value{Kind: KindErr, Err: ErrCircular} |
| } |
| e.g.color[k] = grey |
| - v := Eval(n, e) |
| + v := Eval(n, cellEnv{graphEnv: e, self: k}) |
| e.g.color[k] = black |
| e.g.cache[k] = v |
| e.g.cached[k] = true |
| return v |
| } |
| + |
| +// cellEnv is graphEnv as seen by one formula cell's own evaluation: the |
| +// same lookups, plus the key of the cell being evaluated, which is what |
| +// ROW() and COLUMN() with no argument report (fn_ref.go's callerEnv). The |
| +// key is position-derived (see eval.go's refKey), so it is the cell's |
| +// current row and column. |
| +type cellEnv struct { |
| + *graphEnv |
| + self CellKey |
| +} |
| + |
| +func (e cellEnv) Caller() (CellKey, bool) { return e.self, true } |