/
sheet.go
282 lines (228 loc) · 6.21 KB
/
sheet.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
280
281
282
package xls
import (
"bytes"
"fmt"
"github.com/f2xb/xls/helpers"
"github.com/f2xb/xls/xls/record"
"github.com/f2xb/xls/xls/structure"
)
type rw struct {
cols map[int]structure.CellData
}
type Sheet struct {
boundSheet *record.BoundSheet
rows map[int]*rw
wb *Workbook
maxCol int // maxCol index, countCol=maxCol+1
maxRow int // maxRow index, countRow=maxRow+1
hasAutofilter bool
}
func (s *Sheet) GetName() string {
return s.boundSheet.GetName()
}
// Get row by index
func (s *Sheet) GetRow(index int) (row *rw, err error) {
if row, ok := s.rows[index]; ok {
return row, err
} else {
r := new(rw)
r.cols = make(map[int]structure.CellData)
return r, nil
}
}
func (rw *rw) GetCol(index int) (c structure.CellData, err error) {
if col, ok := rw.cols[index]; ok {
return col, err
} else {
c = new(record.FakeBlank)
return c, nil
}
}
func (rw *rw) GetCols() (cols []structure.CellData) {
var maxColKey int
for k, _ := range rw.cols {
if k > maxColKey {
maxColKey = k
}
}
for i := 0; i <= maxColKey; i++ {
if rw.cols[i] == nil {
cols = append(cols, new(record.FakeBlank))
} else {
cols = append(cols, rw.cols[i])
}
}
return cols
}
// Get all rows
func (s *Sheet) GetRows() (rows []*rw) {
for i := 0; i <= s.GetNumberRows()-1; i++ {
if s.rows[i] == nil {
r := new(rw)
r.cols = make(map[int]structure.CellData)
rows = append(rows, r)
} else {
rows = append(rows, s.rows[i])
}
}
return rows
}
// Get number of rows
func (s *Sheet) GetNumberRows() (n int) {
var maxRowKey int
for k, _ := range s.rows {
if k > maxRowKey {
maxRowKey = k
}
}
return maxRowKey + 1
}
func (s *Sheet) read(stream []byte) (err error) { // nolint: gocyclo
var point int64
point = int64(helpers.BytesToUint32(s.boundSheet.LbPlyPos[:]))
var sPoint int64
eof := false
records := make(map[string]string)
Next:
recordNumber := stream[point : point+2]
recordDataLength := int64(helpers.BytesToUint16(stream[point+2 : point+4]))
sPoint = point + 4
records[fmt.Sprintf("%x", recordNumber)] = fmt.Sprintf("%x", recordNumber)
if bytes.Compare(recordNumber, record.AutofilterInfoRecord[:]) == 0 {
c := new(record.AutofilterInfo)
c.Read(stream[sPoint : sPoint+recordDataLength])
if c.GetCountEntries() > 0 {
s.hasAutofilter = true
} else {
s.hasAutofilter = false
}
goto EIF
}
// LABELSST - String constant that uses BIFF8 shared string table (new to BIFF8)
if bytes.Compare(recordNumber, record.LabelSStRecord[:]) == 0 {
c := new(record.LabelSSt)
c.Read(stream[sPoint:sPoint+recordDataLength], &s.wb.sst)
s.addCell(c, c.GetRow(), c.GetCol())
goto EIF
}
// LABEL - Cell Value, String Constant
if bytes.Compare(recordNumber, record.LabelRecord[:]) == 0 {
if bytes.Compare(s.wb.vers[:], record.FlagBIFF8) == 0 {
c := new(record.LabelBIFF8)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
} else {
c := new(record.LabelBIFF5)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
}
goto EIF
}
if bytes.Compare(recordNumber, []byte{0xFD, 0x00}) == 0 {
// todo: сделать
goto EIF
}
// ARRAY - An array-entered formula
if bytes.Compare(recordNumber, record.ArrayRecord[:]) == 0 {
// todo: сделать
goto EIF
}
// BLANK - An empty col
if bytes.Compare(recordNumber, record.BlankRecord[:]) == 0 {
c := new(record.Blank)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
goto EIF
}
// BOOLERR - A Boolean or error value
if bytes.Compare(recordNumber, record.BoolErrRecord[:]) == 0 {
c := new(record.BoolErr)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
goto EIF
}
// FORMULA - A col formula, stored as parse tokens
if bytes.Compare(recordNumber, record.FormulaRecord[:]) == 0 {
// todo: сделать
goto EIF
}
// NUMBER - An IEEE floating-point number
if bytes.Compare(recordNumber, record.NumberRecord[:]) == 0 {
c := new(record.Number)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
goto EIF
}
// MULBLANK - Multiple empty rows (new to BIFF5)
if bytes.Compare(recordNumber, record.MulBlankRecord[:]) == 0 {
c := new(record.MulBlank)
c.Read(stream[sPoint : sPoint+recordDataLength])
blRecords := c.GetArrayBlRecord()
for i := 0; i <= len(blRecords)-1; i++ {
s.addCell(blRecords[i].Get(), blRecords[i].GetRow(), blRecords[i].GetCol())
}
goto EIF
}
// RK - An RK number
if bytes.Compare(recordNumber, record.RkRecord[:]) == 0 {
c := new(record.Rk)
c.Read(stream[sPoint : sPoint+recordDataLength])
s.addCell(c, c.GetRow(), c.GetCol())
goto EIF
}
// MULRK - Multiple RK numbers (new to BIFF5)
if bytes.Compare(recordNumber, record.MulRKRecord[:]) == 0 {
c := new(record.MulRk)
c.Read(stream[sPoint : sPoint+recordDataLength])
rkRecords := c.GetArrayRKRecord()
for i := 0; i <= len(rkRecords)-1; i++ {
s.addCell(rkRecords[i].Get(), rkRecords[i].GetRow(), rkRecords[i].GetCol())
}
goto EIF
}
// RSTRING - Cell with character formatting
if bytes.Compare(recordNumber, record.RStringRecord[:]) == 0 {
// todo: сделать
goto EIF
}
// SHRFMLA - A shared formula (new to BIFF5)
if bytes.Compare(recordNumber, record.SharedFormulaRecord[:]) == 0 {
// todo: сделать
goto EIF
}
// STRING - A string that represents the result of a formula
if bytes.Compare(recordNumber, record.StringRecord[:]) == 0 {
// todo: сделать
goto EIF
}
if bytes.Compare(recordNumber, record.RowRecord[:]) == 0 {
// todo: сделать
goto EIF
}
// EOF
if bytes.Compare(recordNumber, record.EOFRecord[:]) == 0 && recordDataLength == 0 {
eof = true
}
EIF:
point = point + recordDataLength + 4
if !eof {
goto Next
}
return
}
func (s *Sheet) addCell(cd structure.CellData, row [2]byte, column [2]byte) {
r := int(helpers.BytesToUint16(row[:]))
c := int(helpers.BytesToUint16(column[:]))
if s.rows == nil {
s.rows = map[int]*rw{}
}
if _, ok := s.rows[r]; !ok {
s.rows[r] = new(rw)
if _, ok := s.rows[r].cols[c]; !ok {
colVal := map[int]structure.CellData{}
colVal[c] = cd
s.rows[r].cols = colVal
}
}
s.rows[r].cols[c] = cd
}