forked from shakinm/xlsReader
/
workbook.go
181 lines (143 loc) · 3.92 KB
/
workbook.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
package xls
import (
"bytes"
"encoding/binary"
"errors"
"github.com/eidrisov/xlsReader/helpers"
"github.com/eidrisov/xlsReader/xls/record"
)
// Workbook struct
type Workbook struct {
sheets []Sheet
codepage record.CodePage
sst record.SST
xf []record.XF
formats map[int]record.Format
vers [2]byte
}
// GetNumberSheets - Number of sheets in the workbook
func (wb *Workbook) GetNumberSheets() int {
return len(wb.sheets)
}
// GetSheets - Get sheets in the workbook
func (wb *Workbook) GetSheets() []Sheet {
return wb.sheets
}
// GetSheet - Get Sheet by ID
func (wb *Workbook) GetSheet(sheetID int) (sheet *Sheet, err error) { // nolint: golint
if len(wb.sheets) >= 1 && len(wb.sheets) >= sheetID {
return &wb.sheets[sheetID], err
}
return nil, errors.New("error. Sheet not found")
}
// GetXF - Return Extended Format Record by index
func (wb *Workbook) GetXFbyIndex(index int) record.XF {
if len(wb.xf)-1 < index {
return wb.xf[15]
}
return wb.xf[index]
}
// GetXF - Return FORMAT record describes a number format in the workbook
func (wb *Workbook) GetFormatByIndex(index int) record.Format {
return wb.formats[index]
}
// GetCodePage - codepage
func (wb *Workbook) GetCodePage() []byte {
return wb.codepage.GetCv()
}
func (wb *Workbook) GetCodePageInt() int {
return int(binary.LittleEndian.Uint16(wb.codepage.GetCv()))
}
// GetVersionBIFF - version BIFF
func (wb *Workbook) GetVersionBIFF() []byte {
return wb.vers[:]
}
func (wb *Workbook) addSheet(bs *record.BoundSheet) (sheet Sheet) { // nolint: golint
sheet.boundSheet = bs
sheet.wb = wb
wb.sheets = append(wb.sheets, sheet)
return sheet
}
func (wb *Workbook) read(stream []byte) (err error) { // nolint: gocyclo
var point int32
var SSTContinue = false
var sPoint, prevLen int32
var readType string
var grbit byte
var grbitOffset int32
eof := false
Next:
recordNumber := stream[point : point+2]
recordDataLength := int32(helpers.BytesToUint16(stream[point+2 : point+4]))
sPoint = point + 4
if bytes.Compare(recordNumber, record.IndexRecord[:]) == 0 {
_ = new(record.LabelSSt)
goto EIF
}
//BoundSheet
if bytes.Compare(recordNumber, record.BoundSheetRecord[:]) == 0 {
var bs record.BoundSheet
bs.Read(stream[sPoint+grbitOffset:sPoint+recordDataLength], wb.vers[:])
_ = wb.addSheet(&bs)
goto EIF
}
//Continue
if bytes.Compare(recordNumber, record.ContinueRecord[:]) == 0 {
if SSTContinue {
readType = "continue"
if len(wb.sst.RgbSrc) == 0 {
grbitOffset = 0
} else {
grbitOffset = 1
}
grbit = stream[sPoint]
wb.sst.RgbSrc = append(wb.sst.RgbSrc, stream[sPoint+grbitOffset:sPoint+recordDataLength]...)
wb.sst.Read(readType, grbit, prevLen)
}
goto EIF
}
//SST
if bytes.Compare(recordNumber, record.SSTRecord[:]) == 0 {
wb.sst.NewSST(stream[sPoint : sPoint+recordDataLength])
wb.sst.Read(readType, grbit, prevLen)
totalSSt := helpers.BytesToUint32(wb.sst.CstTotal[:])
if recordDataLength >= 8224 || uint32(len(wb.sst.Rgb)) < totalSSt-1 {
SSTContinue = true
}
goto EIF
}
if bytes.Compare(recordNumber, record.XFRecord[:]) == 0 {
xf := new(record.XF)
xf.Read(stream[sPoint : sPoint+recordDataLength])
wb.xf = append(wb.xf, *xf)
goto EIF
}
if bytes.Compare(recordNumber, record.FormatRecord[:]) == 0 {
format := new(record.Format)
format.Read(stream[sPoint:sPoint+recordDataLength], wb.vers[:])
if wb.formats == nil {
wb.formats = make(map[int]record.Format, 0)
}
wb.formats[format.GetIndex()] = *format
goto EIF
}
//CodePage
if bytes.Compare(recordNumber, record.CodePageRecord[:]) == 0 {
wb.codepage.Read(stream[sPoint : sPoint+recordDataLength])
goto EIF
}
//EOF
if bytes.Compare(recordNumber, record.EOFRecord[:]) == 0 && recordDataLength == 0 {
eof = true
}
if bytes.Compare(recordNumber, record.BOFMARKS[:]) == 0 {
copy(wb.vers[:], stream[sPoint:sPoint+2])
goto EIF
}
EIF:
point = point + recordDataLength + 4
if !eof {
goto Next
}
return err
}