-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader.go
258 lines (225 loc) · 4.75 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package xlsx
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"os"
"strings"
xml "github.com/dgrr/quickxml"
)
// XLSX ...
type XLSX struct {
sharedStrings []string
zr *zip.Reader
Sheets []*Sheet
closer io.Closer
}
// sheetData
// row: r="1"
// c: r="A1" t="inlineStr"|"n" s="1"
// is:
// t:
// v:
// xlsxIndex represents an index of the sharedStrings file and
// the spreadsheets files.
type xlsxIndex struct {
sharedStr string
files []string
}
// Close closes all the buffers and readers.
func (xlsx *XLSX) Close() error {
if xlsx.zr == nil {
return nil
}
if xlsx.closer != nil {
return xlsx.closer.Close()
}
return nil
}
// Open just opens the file for reading.
func Open(filename string) (*XLSX, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
st, err := file.Stat()
if err != nil {
return nil, err
}
return OpenReader(file, st.Size())
}
// OpenReader opens the reader as XLSX file.
func OpenReader(r io.ReaderAt, size int64) (*XLSX, error) {
zr, err := zip.NewReader(r, size)
if err == nil {
var xlsx *XLSX
for _, zFile := range zr.File {
// read where the worksheets are
if zFile.Name == "[Content_Types].xml" {
index, err := parseContentType(zFile)
if err != nil {
return nil, fmt.Errorf("parseContentType: %s", err)
}
// read the worksheets
xlsx, err = extractWorksheets(zr, &index)
if err == nil {
closer, ok := r.(io.Closer)
if ok {
xlsx.closer = closer
}
}
}
}
return xlsx, err
}
return nil, errors.New("rels file not found")
}
func getPartName(e *xml.StartElement) (partName string, err error) {
kv := e.Attrs().GetBytes(partNameString)
if kv != nil {
partName = kv.Value()
}
if partName == "" {
err = errors.New("PartName parameter not found")
}
return
}
func parseContentType(zFile *zip.File) (index xlsxIndex, err error) {
var (
zfr io.ReadCloser
)
zfr, err = zFile.Open()
if err != nil {
return
}
defer zfr.Close()
r := xml.NewReader(zfr)
for err == nil && r.Next() {
switch e := r.Element().(type) {
case *xml.StartElement:
if !bytes.Equal(e.NameBytes(), overrideString) {
continue
}
var partName string
kv := e.Attrs().GetBytes(contentTypeString)
if kv != nil {
switch {
case bytes.Equal(kv.ValueBytes(), workSheetURIString):
partName, err = getPartName(e)
if err == nil {
index.files = append(index.files, partName)
}
case bytes.Equal(kv.ValueBytes(), sharedStringsURIString):
partName, err = getPartName(e)
if err == nil {
index.sharedStr = partName
}
}
}
}
}
if err == nil {
if r.Error() != nil && r.Error() != io.EOF {
err = r.Error()
} else if len(index.files) == 0 {
err = errors.New("no data files found")
}
}
return
}
func extractWorksheets(zr *zip.Reader, index *xlsxIndex) (*XLSX, error) {
var (
err error
shared []string
)
sharedFile := index.sharedStr
if len(sharedFile) > 0 {
shared, err = readShared(zr, sharedFile)
if err != nil {
return nil, fmt.Errorf("error reading shared strings: %s", err)
}
}
xs := new(XLSX)
xs.sharedStrings = shared
for _, filename := range index.files {
zFile, err := getZipFile(zr, filename)
if err != nil {
xs = nil
return nil, err
}
xs.Sheets = append(xs.Sheets, &Sheet{
parent: xs,
zFile: zFile,
})
}
return xs, err
}
func findNameIn(name, where string) bool {
if name[0] == '/' {
return name[1:] == where
}
return strings.Contains(where, name)
}
func getZipFile(zr *zip.Reader, filename string) (zFile *zip.File, err error) {
var found = false
for _, zFile = range zr.File {
found = findNameIn(filename, zFile.Name)
if found {
break
}
}
if !found {
err = fmt.Errorf("%s not found", filename)
}
return zFile, err
}
func readShared(zr *zip.Reader, filename string) ([]string, error) {
var (
rc io.ReadCloser
found bool
err error
)
for _, zFile := range zr.File {
found = findNameIn(filename, zFile.Name)
if found {
rc, err = zFile.Open()
break
}
}
if !found {
err = fmt.Errorf("%s not found", filename)
}
if err != nil {
return nil, err
}
defer rc.Close()
return parseShared(rc)
}
func parseShared(rc io.Reader) ([]string, error) {
var err error
ss := make([]string, 0)
r := xml.NewReader(rc)
T := false
loop:
for r.Next() {
switch e := r.Element().(type) {
case *xml.StartElement:
T = bytes.Equal(e.NameBytes(), tString)
if T && e.HasEnd() {
// shared strings sometimes contains empty strings. Don't know why
ss = append(ss, "")
}
case *xml.TextElement:
if T {
ss = append(ss, string(*e))
}
case *xml.EndElement:
if bytes.Equal(e.NameBytes(), sstString) {
break loop
}
}
}
return ss, err
}