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 "archive/zip"
5 "bytes"
6 "fmt"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/sheets/internal/sheets/store"
11 "amadan.net/oficina/sheets/internal/sheets/xlsx"
12)
13
14// rawBook writes a workbook part by part, so a test controls the exact
15// XML a cell is written with: whether a formula has a <v>, what t it
16// declares, how Google wraps a function Excel lacks. excelize cannot
17// write most of these shapes, which is the point of reading them.
18//
19// sheets are each worksheet's <sheetData> rows; extra is any further
20// worksheet markup (after sheetData) for the first sheet.
21type rawBook struct {
22 sheets []string
23 extra string
24 parts map[string]string // replaces or adds a part by name
25}
26
27const rawStyles = `<?xml version="1.0" encoding="UTF-8"?>
28<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
29<fonts count="1"><font><sz val="10"/><name val="Arial"/></font></fonts>
30<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>
31<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>
32<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>
33<cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs>
34</styleSheet>`
35
36func (b rawBook) bytes(t *testing.T) []byte {
37 t.Helper()
38 parts := map[string]string{
39 "[Content_Types].xml": `<?xml version="1.0" encoding="UTF-8"?>
40<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
41<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
42<Default Extension="xml" ContentType="application/xml"/>
43<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
44</Types>`,
45 "_rels/.rels": `<?xml version="1.0" encoding="UTF-8"?>
46<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
47<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
48</Relationships>`,
49 "xl/styles.xml": rawStyles,
50 }
51 var sheets, rels strings.Builder
52 for i, data := range b.sheets {
53 n := i + 1
54 fmt.Fprintf(&sheets, `<sheet name="Sheet%d" sheetId="%d" r:id="rId%d"/>`, n, n, n)
55 fmt.Fprintf(&rels, `<Relationship Id="rId%d" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet%d.xml"/>`, n, n)
56 extra := ""
57 if i == 0 {
58 extra = b.extra
59 }
60 parts[fmt.Sprintf("xl/worksheets/sheet%d.xml", n)] = `<?xml version="1.0" encoding="UTF-8"?>
61<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData>` +
62 data + `</sheetData>` + extra + `</worksheet>`
63 }
64 fmt.Fprintf(&rels, `<Relationship Id="rIdS" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>`)
65 parts["xl/workbook.xml"] = `<?xml version="1.0" encoding="UTF-8"?>
66<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets>` +
67 sheets.String() + `</sheets></workbook>`
68 parts["xl/_rels/workbook.xml.rels"] = `<?xml version="1.0" encoding="UTF-8"?>
69<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` + rels.String() + `</Relationships>`
70 for name, body := range b.parts {
71 parts[name] = body
72 }
73
74 var buf bytes.Buffer
75 zw := zip.NewWriter(&buf)
76 for name, body := range parts {
77 w, err := zw.Create(name)
78 if err != nil {
79 t.Fatalf("zip create %s: %v", name, err)
80 }
81 if _, err := w.Write([]byte(body)); err != nil {
82 t.Fatalf("zip write %s: %v", name, err)
83 }
84 }
85 if err := zw.Close(); err != nil {
86 t.Fatalf("zip close: %v", err)
87 }
88 return buf.Bytes()
89}
90
91// importRaw imports b into a fresh tab and returns the handle, the
92// report and Import's error.
93func importRaw(t *testing.T, b rawBook) (*store.Handle, xlsx.Report, error) {
94 t.Helper()
95 h := newTab(t, openTestDB(t), testSealer(t))
96 rep, err := xlsx.Import(bytes.NewReader(b.bytes(t)), h)
97 return h, rep, err
98}
99
100// row wraps cells in a <row r="n">.
101func row(n int, cells ...string) string {
102 return fmt.Sprintf(`<row r="%d">%s</row>`, n, strings.Join(cells, ""))
103}
104
105// inline is an inline-string cell; num a number cell.
106func inline(ref, text string) string {
107 return fmt.Sprintf(`<c r="%s" t="inlineStr"><is><t xml:space="preserve">%s</t></is></c>`, ref, xmlEscape(text))
108}
109
110func num(ref, v string) string { return fmt.Sprintf(`<c r="%s"><v>%s</v></c>`, ref, v) }
111
112// formula is a formula cell; t is its declared type and v its saved
113// value, with v == nil meaning no <v> element at all.
114func formula(ref, t, f string, v *string) string {
115 attrs := fmt.Sprintf(`r="%s"`, ref)
116 if t != "" {
117 attrs += fmt.Sprintf(` t="%s"`, t)
118 }
119 body := "<f>" + xmlEscape(f) + "</f>"
120 if v != nil {
121 body += "<v>" + xmlEscape(*v) + "</v>"
122 }
123 return "<c " + attrs + ">" + body + "</c>"
124}
125
126func ptr(s string) *string { return &s }
127
128func xmlEscape(s string) string {
129 return strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;", `"`, "&quot;").Replace(s)
130}
131
132// entry finds code in rep, or a zero Entry.
133func entry(rep xlsx.Report, code xlsx.Code) xlsx.Entry {
134 for _, e := range rep.Entries {
135 if e.Code == code {
136 return e
137 }
138 }
139 return xlsx.Entry{}
140}
141