oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package xlsx_test
2
3import (
4 "strconv"
5 "strings"
6 "testing"
7
8 "amadan.net/oficina/sheets/internal/sheets/calc"
9 "amadan.net/oficina/sheets/internal/sheets/store"
10 "amadan.net/oficina/sheets/internal/sheets/xlsx"
11)
12
13// googleBook is a first worksheet in the shapes a Google Sheets export
14// writes, with a second worksheet that must not come across.
15func googleBook() rawBook {
16 return rawBook{sheets: []string{strings.Join([]string{
17 row(1,
18 num("A1", "5"),
19 formula("B1", "", "SUM(A1,1)", ptr("6")),
20 `<c r="C1"><f t="shared" ref="C1:C2" si="0">$A$1+1</f><v>6</v></c>`,
21 // Trailing, with an empty saved string: excelize's row
22 // reader drops it, and it must still come across.
23 formula("D1", "str", `IF(TRUE,"","x")`, ptr("")),
24 ),
25 row(2,
26 inline("A2", "00123"),
27 formula("B2", "", "A1*2", ptr("11")),
28 `<c r="C2"><f t="shared" ref="C1:C2" si="0"/><v>6</v></c>`,
29 inline("E2", ""),
30 ),
31 row(3, inline("A3", ""), formula("B3", "", "A1/3", ptr("1.66666666666667"))),
32 row(4, inline("A4", "TRUE"), formula("B4", "", "Sheet2!A1", ptr("42"))),
33 row(5, inline("A5", "=SUM(A1)"),
34 formula("B5", "str", `IFERROR(__xludf.DUMMYFUNCTION("SPLIT(""a,007"","","")"),"a")`, ptr("a"))),
35 row(6, inline("A6", "NaN"),
36 formula("B6", "str", `IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),"007")`, ptr("007"))),
37 row(7, inline("A7", "hello"), `<c r="B7"><f t="array" ref="B7">SUM(A1*2)</f><v>10</v></c>`),
38 row(8, `<c r="A8" t="b"><v>1</v></c>`, formula("B8", "str", "FOO(1)", ptr("x"))),
39 row(9, formula("B9", "str", `IF(A1>0,"","x")`, ptr(""))),
40 row(10, formula("B10", "b", "TaxRate*2", ptr("1"))),
41 row(11, formula("B11", "", "NOW()", ptr("45000"))),
42 row(12, formula("B12", "", "A1+1", nil)),
43 row(13, formula("B13", "e", "1/0", ptr("#DIV/0!"))),
44 row(14, formula("B14", "", "A99", ptr("0"))),
45 }, ""), row(1, num("A1", "42"))}}
46}
47
48func TestImportKeepsTypedValuesAndFreezesWhatItCannotCalculate(t *testing.T) {
49 h, rep, err := importRaw(t, googleBook())
50 if err != nil {
51 t.Fatalf("Import: %v", err)
52 }
53 str := func(s string) calc.Value { return calc.Value{Kind: calc.KindStr, Str: s} }
54 n := func(v float64) calc.Value { return calc.Value{Kind: calc.KindNum, Num: v} }
55 cases := []struct {
56 ref string
57 raw string
58 value calc.Value
59 }{
60 {"A1", "5", n(5)},
61 {"A2", `="00123"`, str("00123")},
62 {"A3", `=""`, str("")},
63 {"A4", `="TRUE"`, str("TRUE")},
64 {"A5", `="=SUM(A1)"`, str("=SUM(A1)")},
65 {"A6", `="NaN"`, str("NaN")},
66 {"A7", "hello", str("hello")},
67 {"A8", "TRUE", calc.Value{Kind: calc.KindBool, Bool: true}},
68 {"B1", "=SUM(A1,1)", n(6)},
69 {"B2", "=A1*2", n(10)},
70 {"B4", "42", n(42)},
71 {"B5", "a", str("a")},
72 {"B6", `="007"`, str("007")},
73 {"B7", "10", n(10)},
74 {"B8", "x", str("x")},
75 {"B9", `=IF(A1>0,"","x")`, str("")},
76 {"B10", "TRUE", calc.Value{Kind: calc.KindBool, Bool: true}},
77 {"B12", "=A1+1", n(6)},
78 {"B13", "=1/0", calc.Value{Kind: calc.KindErr, Err: calc.ErrDiv0}},
79 {"C1", "=$A$1+1", n(6)},
80 {"C2", "=$A$1+1", n(6)},
81 {"D1", `=IF(TRUE,"","x")`, str("")},
82 {"E2", `=""`, str("")},
83 }
84 for _, c := range cases {
85 raw, value := cellByRef(t, h, c.ref)
86 if raw != c.raw || value != c.value {
87 t.Errorf("%s = %q %+v, want %q %+v", c.ref, raw, value, c.raw, c.value)
88 }
89 }
90
91 if rep.Worksheets != 2 || rep.OmittedWorksheets() != 1 {
92 t.Errorf("worksheets = %d (omitted %d), want 2 (1)", rep.Worksheets, rep.OmittedWorksheets())
93 }
94 wantCells := map[xlsx.Code][]string{
95 xlsx.TextKept: {"A2", "E2", "A3", "A4", "A5", "A6", "B6"},
96 xlsx.FormulaDiffers: {"B2", "B14"},
97 xlsx.FormulaOtherSheet: {"B4"},
98 xlsx.FormulaGoogleOnly: {"B5", "B6"},
99 xlsx.FormulaArray: {"B7"},
100 xlsx.FormulaUnsupported: {"B8", "B10"},
101 xlsx.FormulaUnchecked: {"B11", "B12"},
102 }
103 for code, want := range wantCells {
104 got := entry(rep, code)
105 if got.Count != len(want) || !sameStrings(got.Cells, want) {
106 t.Errorf("%s = %d %v, want %v", code, got.Count, got.Cells, want)
107 }
108 }
109}
110
111// cellByRef reads one cell's raw input and value by its A1 reference.
112func cellByRef(t *testing.T, h *store.Handle, ref string) (string, calc.Value) {
113 t.Helper()
114 for _, rp := range h.GridRows() {
115 for _, cp := range h.GridCols() {
116 if calc.A1(rp.Pos, cp.Pos) == ref {
117 raw, _ := h.RawInput(rp.ID, cp.ID)
118 return raw, h.Value(rp.ID, cp.ID)
119 }
120 }
121 }
122 return "", calc.Value{}
123}
124
125func sameStrings(a, b []string) bool {
126 if len(a) != len(b) {
127 return false
128 }
129 for i := range a {
130 if a[i] != b[i] {
131 return false
132 }
133 }
134 return true
135}
136
137// A write whose dependents pass calc.MaxDirtyPerPass is not
138// recalculated until the tab next opens, so no value read during the
139// import is final. Then nothing is compared: a stale value could match
140// a saved one by accident and hide a difference.
141func TestImportComparesNothingAfterACappedRecalculation(t *testing.T) {
142 if testing.Short() {
143 t.Skip("writes over calc.MaxDirtyPerPass cells")
144 }
145 n := calc.MaxDirtyPerPass + 1
146 rows := make([]string, 0, n+1)
147 for i := 1; i <= n; i++ {
148 rows = append(rows, row(i, formula("B"+strconv.Itoa(i), "", "$A$"+strconv.Itoa(n+1), ptr("1"))))
149 }
150 rows = append(rows, row(n+1, num("A"+strconv.Itoa(n+1), "1")))
151 _, rep, err := importRaw(t, rawBook{sheets: []string{strings.Join(rows, "")}})
152 if err != nil {
153 t.Fatalf("Import: %v", err)
154 }
155 if got := entry(rep, xlsx.FormulaUnchecked).Count; got != n {
156 t.Errorf("unchecked = %d, want all %d", got, n)
157 }
158 if got := entry(rep, xlsx.FormulaDiffers).Count; got != 0 {
159 t.Errorf("differs = %d, want none: nothing final was compared", got)
160 }
161}
162