-
Notifications
You must be signed in to change notification settings - Fork 21
/
shared_strings.go
203 lines (169 loc) · 4.52 KB
/
shared_strings.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
package xlsxreader
import (
"archive/zip"
"encoding/xml"
"errors"
"fmt"
"io"
"strconv"
"strings"
)
// sharedStringsValue is a struct that holds the value of the shared strings.
type sharedStringsValue struct {
Text string `xml:"t"`
RichText []string `xml:"r>t"`
}
func (sv *sharedStringsValue) unmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
tok, err := d.Token()
if err != nil {
return fmt.Errorf("error retrieving xml token: %w", err)
}
var se xml.StartElement
switch el := tok.(type) {
case xml.EndElement:
if el == start.End() {
return nil
}
continue
case xml.StartElement:
se = el
default:
continue
}
switch se.Name.Local {
case "t":
sv.Text, err = getCharData(d)
case "r":
err = sv.decodeRichText(d, se)
default:
continue
}
if err != nil {
return fmt.Errorf("unable to parse string: %w", err)
}
}
}
func (sv *sharedStringsValue) decodeRichText(d *xml.Decoder, start xml.StartElement) error {
for {
tok, err := d.Token()
if err != nil {
return fmt.Errorf("unable to get shared strings value token: %w", err)
}
var se xml.StartElement
switch el := tok.(type) {
case xml.StartElement:
se = el
case xml.EndElement:
if el == start.End() {
return nil
}
continue
default:
continue
}
if se.Name.Local != "t" {
continue
}
var s string
if s, err = getCharData(d); err != nil {
return fmt.Errorf("unable to parse string: %w", err)
}
sv.RichText = append(sv.RichText, s)
}
}
// String gets a string value from the raw sharedStringsValue struct.
// Since the values can appear in many different places in the xml structure, we need to normalise this.
// They can either be:
// <si> <t> value </t> </si>
// or
// <si> <r> <t> val </t> </r> <r> <t> ue </t> </r> </si>
func (sv *sharedStringsValue) String() string {
// fast path: no rich text, just return text
if len(sv.RichText) == 0 {
return sv.Text
}
var sb strings.Builder
for _, t := range sv.RichText {
sb.WriteString(t)
}
return sb.String()
}
// Reset zeroes data inside struct.
func (sv *sharedStringsValue) Reset() {
sv.Text = ""
sv.RichText = sv.RichText[:0]
}
// Sentinel error to indicate that no shared strings file can be found
var errNoSharedStrings = errors.New("no shared strings file exists")
// getSharedStringsFile attempts to find and return the zip.File struct associated with the
// shared strings section of an xlsx file. An error is returned if the sharedStrings file
// does not exist, or cannot be found.
func getSharedStringsFile(files []*zip.File) (*zip.File, error) {
for _, file := range files {
if file.Name == "xl/sharedStrings.xml" || file.Name == "xl/SharedStrings.xml" {
return file, nil
}
}
return nil, errNoSharedStrings
}
// getSharedStrings loads the contents of the shared string file into memory.
// This serves as a large lookup table of values, so we can efficiently parse rows.
func getSharedStrings(files []*zip.File) ([]string, error) {
ssFile, err := getSharedStringsFile(files)
if err != nil && errors.Is(err, errNoSharedStrings) {
// Valid to contain no shared strings
return []string{}, nil
}
if err != nil {
return nil, fmt.Errorf("unable to get shared strings file: %w", err)
}
f, err := ssFile.Open()
if err != nil {
return nil, fmt.Errorf("unable to open shared strings file: %w", err)
}
defer f.Close()
var (
sharedStrings []string
value sharedStringsValue
)
dec := xml.NewDecoder(f)
for {
token, err := dec.Token()
if err == io.EOF {
return sharedStrings, nil
}
if err != nil {
return nil, fmt.Errorf("error decoding token: %w", err)
}
startElement, ok := token.(xml.StartElement)
if !ok {
continue
}
if sharedStrings == nil { // don't use len() == 0 here!
sharedStrings = makeSharedStringsSlice(startElement)
continue
}
value.Reset()
if err := value.unmarshalXML(dec, startElement); err != nil {
return nil, fmt.Errorf("error unmarshaling shared strings value %+v: %w", startElement, err)
}
sharedStrings = append(sharedStrings, value.String())
}
}
// makeSharedStringsSlice allocates shared strings slice according to 'count' attribute of root tag
// absence of attribute doesn't break flow because make(..., 0) is valid
func makeSharedStringsSlice(rootElem xml.StartElement) []string {
var count int
for _, attr := range rootElem.Attr {
if attr.Name.Local != "count" {
continue
}
var err error
count, err = strconv.Atoi(attr.Value)
if err != nil {
return []string{}
}
}
return make([]string, 0, count)
}