Sheets import: name what a formula calls, and keep ambiguous text text
calc.Inspect reports whether a parsed formula calls a function no table dispatches (it would evaluate to #NAME?) and whether it reads the clock (NOW/TODAY), so import can freeze the first and skip comparing the second. A test pins that sheet-qualified and external references, defined names, _xlfn./__xludf. names, structured references, array constants, spill and implicit-intersection operators all fail to parse. store.TextInput returns raw input that reads back as exactly the given text: unchanged when contentNode already reads it as that text, otherwise a constant string expression ="..." with quotes doubled (empty strings, leading "=", anything ParseFloat accepts including NaN/Inf/hex, TRUE/FALSE). It asks contentNode rather than restating it, and changes nothing about how existing input reads. A fuzz test holds the round trip. store.MaxInputBytes is now the one 8 KiB input cap the grid handlers and import share.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+211
−2
internal/sheets/calc/inspect.go+49 −0internal/sheets/calc/inspect_test.go+53 −0internal/sheets/handlers_grid.go+3 −2internal/sheets/store/textinput.go+38 −0internal/sheets/store/textinput_test.go+68 −0
CI passed — run details
diff --git a/internal/sheets/calc/inspect.go b/internal/sheets/calc/inspect.go| new file mode 100644 |
| index 0000000..5e73725 |
| --- /dev/null |
| +++ b/internal/sheets/calc/inspect.go |
| @@ -0,0 +1,49 @@ |
| +package calc |
| + |
| +// Inspection is what a parsed formula asks of this engine: whether |
| +// every function it calls is one evalCall can dispatch, and whether |
| +// any of them reads the clock. |
| +// |
| +// Import needs both. A formula that parses but names a function this |
| +// engine does not have evaluates to #NAME?, so keeping it would replace |
| +// a workbook's value with an error nobody typed; it has to be told |
| +// apart before it is written, not discovered afterwards. And a formula |
| +// that reads the clock cannot be compared with the value a workbook |
| +// saved for it, because the two were computed at different moments. |
| +type Inspection struct { |
| + // Unknown is true when some call names a function no table has. |
| + Unknown bool |
| + // Volatile is true when some call reads the recalculation clock |
| + // (NOW, TODAY). |
| + Volatile bool |
| +} |
| + |
| +// Inspect walks n and every node beneath it. |
| +func Inspect(n Node) Inspection { |
| + var in Inspection |
| + inspect(n, &in) |
| + return in |
| +} |
| + |
| +func inspect(n Node, in *Inspection) { |
| + switch t := n.(type) { |
| + case *Call: |
| + _, ordinary := funcTable[t.Name] |
| + _, clock := clockFuncTable[t.Name] |
| + _, node := nodeFuncTable[t.Name] |
| + if !ordinary && !clock && !node { |
| + in.Unknown = true |
| + } |
| + if clock { |
| + in.Volatile = true |
| + } |
| + for _, a := range t.Args { |
| + inspect(a, in) |
| + } |
| + case *Binary: |
| + inspect(t.Left, in) |
| + inspect(t.Right, in) |
| + case *Unary: |
| + inspect(t.X, in) |
| + } |
| +} |
diff --git a/internal/sheets/calc/inspect_test.go b/internal/sheets/calc/inspect_test.go| new file mode 100644 |
| index 0000000..e450cf0 |
| --- /dev/null |
| +++ b/internal/sheets/calc/inspect_test.go |
| @@ -0,0 +1,53 @@ |
| +package calc |
| + |
| +import "testing" |
| + |
| +func TestInspectNamesUnknownAndVolatileCalls(t *testing.T) { |
| + cases := []struct { |
| + src string |
| + unknown bool |
| + volatile bool |
| + }{ |
| + {"1+2", false, false}, |
| + {"SUM(A1:A3)*2", false, false}, |
| + {"IF(A1>0,VLOOKUP(A1,B1:C9,2,FALSE),-A1)", false, false}, |
| + {"NOW()-TODAY()", false, true}, |
| + {"FOO(1)", true, false}, |
| + {"SUM(1,QUERY(A1))", true, false}, |
| + {"-NOTAFUNCTION(A1)", true, false}, |
| + {"IF(TRUE,1,BAR())+NOW()", true, true}, |
| + } |
| + for _, c := range cases { |
| + n, err := Parse(c.src) |
| + if err != nil { |
| + t.Fatalf("Parse(%q): %v", c.src, err) |
| + } |
| + got := Inspect(n) |
| + if got.Unknown != c.unknown || got.Volatile != c.volatile { |
| + t.Errorf("Inspect(%q) = %+v, want unknown=%v volatile=%v", c.src, got, c.unknown, c.volatile) |
| + } |
| + } |
| +} |
| + |
| +// The import converter treats a formula that fails to parse as |
| +// unsupported. These are the shapes Google and Excel write for things |
| +// this engine has no model of; each must fail here rather than parse |
| +// into something that evaluates. |
| +func TestParseRefusesShapesImportCannotKeep(t *testing.T) { |
| + for _, src := range []string{ |
| + "Sheet2!A1", |
| + "'My Sheet'!A1:B2", |
| + "[1]Sheet1!A1", |
| + "TaxRate*A1", |
| + "_xlfn.XLOOKUP(1,A1:A2,B1:B2)", |
| + `IFERROR(__xludf.DUMMYFUNCTION("QUERY(A1)"),3)`, |
| + "SUM(Table1[Col])", |
| + "{1,2}", |
| + "A1#", |
| + "@A1", |
| + } { |
| + if n, err := Parse(src); err == nil { |
| + t.Errorf("Parse(%q) = %#v, want an error", src, n) |
| + } |
| + } |
| +} |
diff --git a/internal/sheets/handlers_grid.go b/internal/sheets/handlers_grid.go| index b3c71d9..a4adda0 100644 |
| --- a/internal/sheets/handlers_grid.go |
| +++ b/internal/sheets/handlers_grid.go |
| @@ -15,8 +15,9 @@ import ( |
| // |
| // A formula or a label, not a document. The engine would happily parse |
| // a megabyte of nesting and spend a long time doing it, and the cell |
| -// that holds it is one nobody can read anyway. |
| -const maxRawBytes = 8 << 10 |
| +// that holds it is one nobody can read anyway. The number is |
| +// store.MaxInputBytes, which import holds normalised text to as well. |
| +const maxRawBytes = store.MaxInputBytes |
| // cellWrite is the POST body. |
| // |
diff --git a/internal/sheets/store/textinput.go b/internal/sheets/store/textinput.go| new file mode 100644 |
| index 0000000..ca427c5 |
| --- /dev/null |
| +++ b/internal/sheets/store/textinput.go |
| @@ -0,0 +1,38 @@ |
| +package store |
| + |
| +import ( |
| + "strings" |
| + |
| + "amadan.net/oficina/sheets/internal/sheets/calc" |
| +) |
| + |
| +// MaxInputBytes caps one cell's raw input: what a person may type into |
| +// a cell, and what import may write when it has to wrap text to keep |
| +// it text. A formula or a label, not a document. |
| +const MaxInputBytes = 8 << 10 |
| + |
| +// TextInput returns the raw input that stores s as the text s, and |
| +// whether that took a wrapper. |
| +// |
| +// Raw input has one channel. contentNode reads "" as blank, a leading |
| +// "=" as a formula, and anything strconv.ParseFloat or TRUE/FALSE |
| +// accepts as a number or a boolean, so text such as "00123", "TRUE", |
| +// "NaN", "=total" or "" cannot be written as itself without becoming |
| +// something else. Those become a constant string expression, ="...", |
| +// with every quote doubled: the existing parser reads it back as |
| +// exactly s, its value is the text s, and an editor shows the |
| +// expression rather than a number nobody typed. |
| +// |
| +// Nothing here changes how existing input is read. The wrapper is an |
| +// ordinary formula any person could have typed; the decision whether |
| +// a string needs one is contentNode's own, asked rather than restated, |
| +// so the two cannot drift apart. |
| +func TextInput(s string) (raw string, wrapped bool) { |
| + node, blank, err := contentNode(s) |
| + if err == nil && !blank { |
| + if str, ok := node.(*calc.Str); ok && str.V == s { |
| + return s, false |
| + } |
| + } |
| + return `="` + strings.ReplaceAll(s, `"`, `""`) + `"`, true |
| +} |
diff --git a/internal/sheets/store/textinput_test.go b/internal/sheets/store/textinput_test.go| new file mode 100644 |
| index 0000000..63b0ac4 |
| --- /dev/null |
| +++ b/internal/sheets/store/textinput_test.go |
| @@ -0,0 +1,68 @@ |
| +package store |
| + |
| +import ( |
| + "testing" |
| + |
| + "amadan.net/oficina/sheets/internal/sheets/calc" |
| +) |
| + |
| +// textOf is what raw input evaluates to when it holds no references: |
| +// a literal's node, or a constant formula's single node. |
| +func textOf(t *testing.T, raw string) (calc.Node, bool) { |
| + t.Helper() |
| + node, blank, err := contentNode(raw) |
| + if err != nil { |
| + t.Fatalf("contentNode(%q): %v", raw, err) |
| + } |
| + return node, blank |
| +} |
| + |
| +func TestTextInputWrapsOnlyAmbiguousText(t *testing.T) { |
| + cases := []struct { |
| + in string |
| + raw string |
| + wrapped bool |
| + }{ |
| + {"hello", "hello", false}, |
| + {" 123", " 123", false}, |
| + {"1,000", "1,000", false}, |
| + {"+353 1 234 5678", "+353 1 234 5678", false}, |
| + {"", `=""`, true}, |
| + {"00123", `="00123"`, true}, |
| + {"+5", `="+5"`, true}, |
| + {"1e5", `="1e5"`, true}, |
| + {"TRUE", `="TRUE"`, true}, |
| + {"false", `="false"`, true}, |
| + {"NaN", `="NaN"`, true}, |
| + {"Inf", `="Inf"`, true}, |
| + {"-infinity", `="-infinity"`, true}, |
| + {"0x1p-2", `="0x1p-2"`, true}, |
| + {"=SUM(A1)", `="=SUM(A1)"`, true}, |
| + {`="quoted"`, `="=""quoted"""`, true}, |
| + } |
| + for _, c := range cases { |
| + raw, wrapped := TextInput(c.in) |
| + if raw != c.raw || wrapped != c.wrapped { |
| + t.Errorf("TextInput(%q) = %q, %v; want %q, %v", c.in, raw, wrapped, c.raw, c.wrapped) |
| + } |
| + } |
| +} |
| + |
| +// Whatever the text, the raw input TextInput returns must read back |
| +// as exactly that text and nothing else. |
| +func FuzzTextInputReadsBackAsItself(f *testing.F) { |
| + for _, s := range []string{"", "0", "00123", "TRUE", "NaN", "=A1", `"`, `""`, "a\"b", "\n", "1e999", "é"} { |
| + f.Add(s) |
| + } |
| + f.Fuzz(func(t *testing.T, s string) { |
| + raw, _ := TextInput(s) |
| + node, blank := textOf(t, raw) |
| + if blank { |
| + t.Fatalf("TextInput(%q) = %q reads as blank", s, raw) |
| + } |
| + str, ok := node.(*calc.Str) |
| + if !ok || str.V != s { |
| + t.Fatalf("TextInput(%q) = %q reads as %#v", s, raw, node) |
| + } |
| + }) |
| +} |