forked from extrame/xls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worksheet.go
212 lines (197 loc) · 5.01 KB
/
worksheet.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
package xls
import (
"encoding/binary"
"fmt"
"io"
"unicode/utf16"
)
type boundsheet struct {
Filepos uint32
Type byte
Visible byte
Name byte
}
//WorkSheet in one WorkBook
type WorkSheet struct {
bs *boundsheet
wb *WorkBook
Name string
rows map[uint16]*Row
//NOTICE: this is the max row number of the sheet, so it should be count -1
MaxRow uint16
parsed bool
}
func (w *WorkSheet) Row(i int) *Row {
row := w.rows[uint16(i)]
if row != nil {
row.wb = w.wb
}
return row
}
func (w *WorkSheet) parse(buf io.ReadSeeker) {
w.rows = make(map[uint16]*Row)
b := new(bof)
var bof_pre *bof
for {
if err := binary.Read(buf, binary.LittleEndian, b); err == nil {
bof_pre = w.parseBof(buf, b, bof_pre)
if b.Id == 0xa {
break
}
} else {
fmt.Println(err)
break
}
}
w.parsed = true
}
func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
var col interface{}
switch b.Id {
// case 0x0E5: //MERGEDCELLS
// ws.mergedCells(buf)
case 0x208: //ROW
r := new(rowInfo)
binary.Read(buf, binary.LittleEndian, r)
w.addRow(r)
case 0x0BD: //MULRK
mc := new(MulrkCol)
size := (b.Size - 6) / 6
binary.Read(buf, binary.LittleEndian, &mc.Col)
mc.Xfrks = make([]XfRk, size)
for i := uint16(0); i < size; i++ {
binary.Read(buf, binary.LittleEndian, &mc.Xfrks[i])
}
binary.Read(buf, binary.LittleEndian, &mc.LastColB)
col = mc
case 0x0BE: //MULBLANK
mc := new(MulBlankCol)
size := (b.Size - 6) / 2
binary.Read(buf, binary.LittleEndian, &mc.Col)
mc.Xfs = make([]uint16, size)
for i := uint16(0); i < size; i++ {
binary.Read(buf, binary.LittleEndian, &mc.Xfs[i])
}
binary.Read(buf, binary.LittleEndian, &mc.LastColB)
col = mc
case 0x203: //NUMBER
col = new(NumberCol)
binary.Read(buf, binary.LittleEndian, col)
case 0x06: //FORMULA
c := new(FormulaCol)
binary.Read(buf, binary.LittleEndian, &c.Header)
c.Bts = make([]byte, b.Size-20)
binary.Read(buf, binary.LittleEndian, &c.Bts)
col = c
case 0x27e: //RK
col = new(RkCol)
binary.Read(buf, binary.LittleEndian, col)
case 0xFD: //LABELSST
col = new(LabelsstCol)
binary.Read(buf, binary.LittleEndian, col)
case 0x204:
c := new(labelCol)
binary.Read(buf, binary.LittleEndian, &c.BlankCol)
var count uint16
binary.Read(buf, binary.LittleEndian, &count)
c.Str, _ = w.wb.get_string(buf, count)
col = c
case 0x201: //BLANK
col = new(BlankCol)
binary.Read(buf, binary.LittleEndian, col)
case 0x1b8: //HYPERLINK
var hy HyperLink
binary.Read(buf, binary.LittleEndian, &hy.CellRange)
buf.Seek(20, 1)
var flag uint32
binary.Read(buf, binary.LittleEndian, &flag)
var count uint32
if flag&0x14 != 0 {
binary.Read(buf, binary.LittleEndian, &count)
hy.Description = b.utf16String(buf, count)
}
if flag&0x80 != 0 {
binary.Read(buf, binary.LittleEndian, &count)
hy.TargetFrame = b.utf16String(buf, count)
}
if flag&0x1 != 0 {
var guid [2]uint64
binary.Read(buf, binary.BigEndian, &guid)
if guid[0] == 0xE0C9EA79F9BACE11 && guid[1] == 0x8C8200AA004BA90B { //URL
hy.IsUrl = true
binary.Read(buf, binary.LittleEndian, &count)
hy.Url = b.utf16String(buf, count/2)
} else if guid[0] == 0x303000000000000 && guid[1] == 0xC000000000000046 { //URL{
var upCount uint16
binary.Read(buf, binary.LittleEndian, &upCount)
binary.Read(buf, binary.LittleEndian, &count)
bts := make([]byte, count)
binary.Read(buf, binary.LittleEndian, &bts)
hy.ShortedFilePath = string(bts)
buf.Seek(24, 1)
binary.Read(buf, binary.LittleEndian, &count)
if count > 0 {
binary.Read(buf, binary.LittleEndian, &count)
buf.Seek(2, 1)
hy.ExtendedFilePath = b.utf16String(buf, count/2+1)
}
}
}
if flag&0x8 != 0 {
binary.Read(buf, binary.LittleEndian, &count)
var bts = make([]uint16, count)
binary.Read(buf, binary.LittleEndian, &bts)
runes := utf16.Decode(bts[:len(bts)-1])
hy.TextMark = string(runes)
}
w.addRange(&hy.CellRange, &hy)
case 0x809:
buf.Seek(int64(b.Size), 1)
case 0xa:
default:
// log.Printf("Unknow %X,%d\n", b.Id, b.Size)
buf.Seek(int64(b.Size), 1)
}
if col != nil {
w.add(col)
}
return b
}
func (w *WorkSheet) add(content interface{}) {
if ch, ok := content.(contentHandler); ok {
if col, ok := content.(Coler); ok {
w.addCell(col, ch)
}
}
}
func (w *WorkSheet) addCell(col Coler, ch contentHandler) {
w.addContent(col.Row(), ch)
}
func (w *WorkSheet) addRange(rang Ranger, ch contentHandler) {
for i := rang.FirstRow(); i <= rang.LastRow(); i++ {
w.addContent(i, ch)
}
}
func (w *WorkSheet) addContent(row_num uint16, ch contentHandler) {
var row *Row
var ok bool
if row, ok = w.rows[row_num]; !ok {
info := new(rowInfo)
info.Index = row_num
row = w.addRow(info)
}
row.cols[ch.FirstCol()] = ch
}
func (w *WorkSheet) addRow(info *rowInfo) (row *Row) {
if info.Index > w.MaxRow {
w.MaxRow = info.Index
}
var ok bool
if row, ok = w.rows[info.Index]; ok {
row.info = info
} else {
row = &Row{info: info, cols: make(map[uint16]contentHandler)}
w.rows[info.Index] = row
}
return
}