-
Notifications
You must be signed in to change notification settings - Fork 2
/
read.go
279 lines (249 loc) · 5.07 KB
/
read.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// This package implements rudimentary support
// for reading Open Document Spreadsheet files. At current
// stage table data can be accessed.
package ods
import (
"bytes"
"encoding/xml"
"errors"
"github.com/knieriem/odf"
"io"
"strconv"
"strings"
)
type Doc struct {
XMLName xml.Name `xml:"document-content"`
Table []Table `xml:"body>spreadsheet>table"`
}
type Table struct {
Name string `xml:"name,attr"`
Column []string `xml:"table-column"`
Row []Row `xml:"table-row"`
}
type Row struct {
RepeatedRows int `xml:"number-rows-repeated,attr"`
Cell []Cell `xml:",any"` // use ",any" to match table-cell and covered-table-cell
}
func (r *Row) IsEmpty() bool {
for _, c := range r.Cell {
if !c.IsEmpty() {
return false
}
}
return true
}
// Return the contents of a row as a slice of strings. Cells that are
// covered by other cells will appear as empty strings.
func (r *Row) Strings(b *bytes.Buffer) (row []string) {
n := len(r.Cell)
if n == 0 {
return
}
// remove trailing empty cells
for i := n - 1; i >= 0; i-- {
if !r.Cell[i].IsEmpty() {
break
}
n--
}
r.Cell = r.Cell[:n]
n = 0
// calculate the real number of cells (including repeated)
for _, c := range r.Cell {
switch {
case c.RepeatedCols != 0:
n += c.RepeatedCols
default:
n++
}
}
row = make([]string, n)
w := 0
for _, c := range r.Cell {
cs := ""
if c.XMLName.Local != "covered-table-cell" {
cs = c.PlainText(b)
}
row[w] = cs
w++
switch {
case c.RepeatedCols != 0:
for j := 1; j < c.RepeatedCols; j++ {
row[w] = cs
w++
}
}
}
return
}
type Cell struct {
XMLName xml.Name
// attributes
ValueType string `xml:"value-type,attr"`
Value string `xml:"value,attr"`
Formula string `xml:"formula,attr"`
RepeatedCols int `xml:"number-columns-repeated,attr"`
ColSpan int `xml:"number-columns-spanned,attr"`
P []Par `xml:"p"`
}
func (c *Cell) IsEmpty() (empty bool) {
switch len(c.P) {
case 0:
empty = true
case 1:
if c.P[0].XML == "" {
empty = true
}
}
return
}
// PlainText extracts the text from a cell. Space tags (<text:s text:c="#">)
// are recognized. Inline elements (like span) are ignored, but the
// text they contain is preserved
func (c *Cell) PlainText(b *bytes.Buffer) string {
n := len(c.P)
if n == 1 {
return c.P[0].PlainText(b)
}
b.Reset()
for i := range c.P {
if i != n-1 {
c.P[i].writePlainText(b)
b.WriteByte('\n')
} else {
c.P[i].writePlainText(b)
}
}
return b.String()
}
type Par struct {
XML string `xml:",innerxml"`
}
func (p *Par) PlainText(b *bytes.Buffer) string {
for i := range p.XML {
if p.XML[i] == '<' || p.XML[i] == '&' {
b.Reset()
p.writePlainText(b)
return b.String()
}
}
return p.XML
}
func (p *Par) writePlainText(b *bytes.Buffer) {
for i := range p.XML {
if p.XML[i] == '<' || p.XML[i] == '&' {
goto decode
}
}
b.WriteString(p.XML)
return
decode:
d := xml.NewDecoder(strings.NewReader(p.XML))
for {
t, _ := d.Token()
if t == nil {
break
}
switch el := t.(type) {
case xml.StartElement:
switch el.Name.Local {
case "s":
n := 1
for _, a := range el.Attr {
if a.Name.Local == "c" {
n, _ = strconv.Atoi(a.Value)
}
}
for i := 0; i < n; i++ {
b.WriteByte(' ')
}
}
case xml.CharData:
b.Write(el)
}
}
}
func (t *Table) Width() int {
return len(t.Column)
}
func (t *Table) Height() int {
return len(t.Row)
}
func (t *Table) Strings() (s [][]string) {
var b bytes.Buffer
n := len(t.Row)
if n == 0 {
return
}
// remove trailing empty rows
for i := n - 1; i >= 0; i-- {
if !t.Row[i].IsEmpty() {
break
}
n--
}
t.Row = t.Row[:n]
n = 0
// calculate the real number of rows (including repeated rows)
for _, r := range t.Row {
switch {
case r.RepeatedRows != 0:
n += r.RepeatedRows
default:
n++
}
}
s = make([][]string, n)
w := 0
for _, r := range t.Row {
row := r.Strings(&b)
s[w] = row
w++
for j := 1; j < r.RepeatedRows; j++ {
s[w] = row
w++
}
}
return
}
type File struct {
*odf.File
}
// Open an ODS file. If the file doesn't exist or doesn't look
// like a spreadsheet file, an error is returned.
func Open(fileName string) (*File, error) {
f, err := odf.Open(fileName)
if err != nil {
return nil, err
}
return newFile(f)
}
// NewReader initializes a File struct with an already opened
// ODS file, and checks the spreadsheet's media type.
func NewReader(r io.ReaderAt, size int64) (*File, error) {
f, err := odf.NewReader(r, size)
if err != nil {
return nil, err
}
return newFile(f)
}
func newFile(f *odf.File) (*File, error) {
if f.MimeType != odf.MimeTypePfx+"spreadsheet" {
f.Close()
return nil, errors.New("not a spreadsheet")
}
return &File{f}, nil
}
// Parse the content.xml part of an ODS file. On Success
// the returned Doc will contain the data of the rows and cells
// of the table(s) contained in the ODS file.
func (f *File) ParseContent(doc *Doc) (err error) {
content, err := f.Open("content.xml")
if err != nil {
return
}
defer content.Close()
d := xml.NewDecoder(content)
err = d.Decode(doc)
return
}