oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package xlsx
2
3import (
4 "errors"
5 "math"
6 "strconv"
7
8 "amadan.net/oficina/sheets/internal/sheets/calc"
9 "amadan.net/oficina/sheets/internal/sheets/store"
10)
11
12// Why a formula's saved value cannot stand in for it. Each refuses the
13// import at the cell's coordinate: there is no trustworthy value to
14// keep, and import never manufactures one.
15var (
16 errNoSavedValue = errors.New("xlsx: formula has no saved value")
17 errSavedError = errors.New("xlsx: formula saved an error value")
18 errSavedUnreadable = errors.New("xlsx: formula saved a value of an unexpected type")
19)
20
21// savedValue reads the value a workbook saved with a formula, by the
22// rules a frozen cell is held to:
23//
24// - a <v> must be present: absent is not the empty string;
25// - t absent or "n": a finite number, parsed exactly;
26// - t "b": exactly "0" or "1";
27// - t "str": any text, the empty string included;
28// - t "e": refused, because no value in this app is an error that a
29// person did not compute, and error-to-text would change what the
30// cell means;
31// - anything else: refused.
32func savedValue(c scannedCell) (calc.Value, error) {
33 if !c.HasV {
34 return calc.Value{}, errNoSavedValue
35 }
36 switch c.T {
37 case "", "n":
38 n, err := strconv.ParseFloat(c.V, 64)
39 if err != nil || math.IsNaN(n) || math.IsInf(n, 0) {
40 return calc.Value{}, errSavedUnreadable
41 }
42 return calc.Value{Kind: calc.KindNum, Num: n}, nil
43 case "b":
44 switch c.V {
45 case "0":
46 return calc.Value{Kind: calc.KindBool}, nil
47 case "1":
48 return calc.Value{Kind: calc.KindBool, Bool: true}, nil
49 }
50 return calc.Value{}, errSavedUnreadable
51 case "str":
52 return calc.Value{Kind: calc.KindStr, Str: c.V}, nil
53 case "e":
54 return calc.Value{}, errSavedError
55 }
56 return calc.Value{}, errSavedUnreadable
57}
58
59// frozenInput is the raw input that holds v as a literal, and whether
60// holding it took the ="..." wrapper.
61func frozenInput(v calc.Value) (raw string, wrapped bool) {
62 switch v.Kind {
63 case calc.KindNum:
64 return strconv.FormatFloat(v.Num, 'g', -1, 64), false
65 case calc.KindBool:
66 if v.Bool {
67 return "TRUE", false
68 }
69 return "FALSE", false
70 default:
71 return store.TextInput(v.Str)
72 }
73}
74
75// savedErrors are the error values a workbook can save that this
76// engine also produces. Any other (#SPILL!, #CALC!, Google's #ERROR!)
77// compares unequal to everything, which is the truth.
78var savedErrors = map[string]calc.ErrKind{
79 "#DIV/0!": calc.ErrDiv0, "#VALUE!": calc.ErrValue, "#REF!": calc.ErrRef,
80 "#NAME?": calc.ErrName, "#NUM!": calc.ErrNum, "#N/A": calc.ErrNA,
81}
82
83// comparableSaved is a kept formula's saved value, to check this
84// engine's result against. Unlike savedValue it accepts an error,
85// because a kept formula is not being replaced by what it saved.
86func comparableSaved(c scannedCell) (calc.Value, bool) {
87 if c.HasV && c.T == "e" {
88 kind, ok := savedErrors[c.V]
89 if !ok {
90 return calc.Value{Kind: calc.KindErr, Err: calc.ErrNone}, true
91 }
92 return calc.Value{Kind: calc.KindErr, Err: kind}, true
93 }
94 v, err := savedValue(c)
95 return v, err == nil
96}
97
98// Numbers compare within a relative tolerance. Google writes 15
99// significant digits and Excel computes in its own order, so exact
100// comparison would flag nearly every division; anything past one part
101// in a billion is a different answer, not a rounding.
102const (
103 relTolerance = 1e-9
104 absTolerance = 1e-12
105)
106
107// sameValue reports whether this engine's result matches a saved one.
108func sameValue(ours, saved calc.Value) bool {
109 // Kinds compare strictly. A formula that computes to nothing here
110 // where the workbook saved 0 or "" is a real difference: blank,
111 // zero and the empty string answer ISBLANK, ISNUMBER and = apart.
112 if ours.Kind != saved.Kind {
113 return false
114 }
115 switch ours.Kind {
116 case calc.KindNum:
117 diff := math.Abs(ours.Num - saved.Num)
118 scale := math.Max(math.Abs(ours.Num), math.Abs(saved.Num))
119 return diff <= absTolerance || diff <= relTolerance*scale
120 case calc.KindStr:
121 return ours.Str == saved.Str
122 case calc.KindBool:
123 return ours.Bool == saved.Bool
124 case calc.KindErr:
125 return saved.Err != calc.ErrNone && ours.Err == saved.Err
126 }
127 return false
128}
129