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 "amadan.net/oficina/sheets/internal/sheets/calc"
4
5// Code names one kind of thing an import changed or left behind.
6//
7// A report is codes, coordinates and counts, never content: no cell
8// text, no formula, no sheet or file name. That is what lets it be
9// shown, stored sealed and carried around without becoming a second
10// copy of the workbook.
11type Code string
12
13// Per-cell codes: each carries the coordinates it applies to.
14const (
15 // A formula calling a function Google Sheets has and Excel does
16 // not; Google exports it wrapped in __xludf.DUMMYFUNCTION. Kept as
17 // its saved value.
18 FormulaGoogleOnly Code = "formula-google-only"
19 // An array formula (one formula filling a range). Kept as its
20 // saved value.
21 FormulaArray Code = "formula-array"
22 // A reference to another worksheet or another workbook. Kept as
23 // its saved value: only the first worksheet comes across.
24 FormulaOtherSheet Code = "formula-other-sheet"
25 // Any other formula this app cannot calculate. Kept as its saved
26 // value.
27 FormulaUnsupported Code = "formula-unsupported"
28 // A formula this app calculates, whose result here differs from
29 // the value the workbook saved. Kept as a formula.
30 FormulaDiffers Code = "formula-differs"
31 // A formula kept as a formula with no saved value to check it
32 // against.
33 FormulaUnchecked Code = "formula-unchecked"
34 // Text that would otherwise read as a number, a boolean, a formula
35 // or a blank, kept as text by a constant string formula ="...".
36 TextKept Code = "text-kept"
37 // A cell whose style used something this app does not carry:
38 // fonts, sizes, borders, wrapping, underline and the like.
39 FormatPartial Code = "format-partial"
40)
41
42// Count-only codes: what the worksheet or workbook had, none of it
43// imported.
44const (
45 OmittedMerges Code = "merges"
46 OmittedComments Code = "comments"
47 OmittedValidation Code = "validation"
48 OmittedConditional Code = "conditional-formats"
49 OmittedHyperlinks Code = "hyperlinks"
50 OmittedDefinedNames Code = "defined-names"
51 OmittedHiddenRows Code = "hidden-rows"
52 OmittedHiddenCols Code = "hidden-columns"
53 OmittedFrozenPanes Code = "frozen-panes"
54 OmittedFilter Code = "filter"
55 OmittedPictures Code = "pictures"
56 OmittedWorkbookChart Code = "workbook-charts"
57 OmittedWorkbookImage Code = "workbook-images"
58 // The detectors that could not read their part of the workbook,
59 // so a report says it did not look rather than imply there was
60 // nothing to find.
61 OmittedUndetected Code = "undetected"
62)
63
64// MaxListedCells is how many coordinates one entry names; Count keeps
65// the true total past it.
66const MaxListedCells = 200
67
68// Entry is one code and where it applies.
69type Entry struct {
70 Code Code `json:"code"`
71 Count int `json:"count"`
72 Cells []string `json:"cells,omitempty"`
73}
74
75// Report is what one import did beyond copying values across.
76//
77// It never claims completeness. An empty Entries says nothing was
78// detected, which is all it can say; the page that shows it states
79// the supported subset beside it.
80type Report struct {
81 // Worksheets is how many the workbook held. Only the first comes
82 // across, so Worksheets-1 were omitted.
83 Worksheets int `json:"worksheets"`
84 Entries []Entry `json:"entries,omitempty"`
85}
86
87// OmittedWorksheets is how many worksheets did not come across.
88func (r Report) OmittedWorksheets() int {
89 if r.Worksheets <= 1 {
90 return 0
91 }
92 return r.Worksheets - 1
93}
94
95func (r *Report) entry(code Code) *Entry {
96 for i := range r.Entries {
97 if r.Entries[i].Code == code {
98 return &r.Entries[i]
99 }
100 }
101 r.Entries = append(r.Entries, Entry{Code: code})
102 return &r.Entries[len(r.Entries)-1]
103}
104
105// cell records code at a 0-based position.
106func (r *Report) cell(code Code, rowPos, colPos int) {
107 e := r.entry(code)
108 e.Count++
109 if len(e.Cells) < MaxListedCells {
110 e.Cells = append(e.Cells, calc.A1(rowPos, colPos))
111 }
112}
113
114// add records n of a count-only code; zero records nothing.
115func (r *Report) add(code Code, n int) {
116 if n > 0 {
117 r.entry(code).Count += n
118 }
119}
120