forked from shakinm/xlsReader
/
label.go
141 lines (115 loc) · 2.83 KB
/
label.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
package record
import (
"github.com/Alliera/xlsReader/helpers"
"golang.org/x/text/encoding/charmap"
"reflect"
"strings"
"unicode/utf16"
)
//LABEL: Cell Value, String Constant (204h)
var LabelRecord = []byte{0x04, 0x02} //(204h)
/*
A LABEL record describes a cell that contains a pre-BIFF8 string constant. Note:
this was replaced in BIFF8 by LABELSST .
Record Data 8
Offset Field Name Size Contents
------------------------------------------------
4 rw 2 Row (0-based)
6 col 2 Column (0-based)
8 ixfe 2 Index to the XF record
10 cch 2 Length of the string (must be <= 255)
12 grbit 1 Option flags
13 rgb var Array of string characters
*/
type LabelBIFF8 struct {
rw [2]byte
col [2]byte
ixfe [2]byte
cch [2]byte
grbit [1]byte
rgb []byte
}
type LabelBIFF5 struct {
rw [2]byte
col [2]byte
ixfe [2]byte
cch [2]byte
grbit [1]byte
rgb []byte
}
func (r *LabelBIFF8) GetRow() [2]byte {
return r.rw
}
func (r *LabelBIFF8) GetCol() [2]byte {
return r.col
}
func (r *LabelBIFF8) GetString() string {
if int(r.grbit[0]) == 1 {
name := helpers.BytesToUints16(r.rgb[:])
runes := utf16.Decode(name)
return string(runes)
} else {
return string(decodeWindows1251(r.rgb[:]))
}
}
func (r *LabelBIFF8) GetFloat64() (fl float64) {
return fl
}
func (r *LabelBIFF8) GetInt64() (in int64) {
return in
}
func (r *LabelBIFF8) GetType() string {
return reflect.TypeOf(r).String()
}
func (r *LabelBIFF8) GetXFIndex() int {
return int(helpers.BytesToUint16(r.ixfe[:]))
}
func (r *LabelBIFF8) Read(stream []byte) {
copy(r.rw[:], stream[:2])
copy(r.col[:], stream[2:4])
copy(r.ixfe[:], stream[4:6])
copy(r.cch[:], stream[6:8])
copy(r.grbit[:], stream[8:9])
if int(r.grbit[0]) == 1 {
r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:])*2)
} else {
r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:]))
}
copy(r.rgb[:], stream[9:])
}
func (r *LabelBIFF5) GetRow() [2]byte {
return r.rw
}
func (r *LabelBIFF5) GetCol() [2]byte {
return r.col
}
func (r *LabelBIFF5) GetString() string {
strLen := helpers.BytesToUint16(r.cch[:])
return strings.TrimSpace(string(decodeWindows1251(r.rgb[:int(strLen)])))
}
func (r *LabelBIFF5) GetFloat64() (fl float64) {
return fl
}
func (r *LabelBIFF5) GetInt64() (in int64) {
return in
}
func (r *LabelBIFF5) GetType() string {
return reflect.TypeOf(r).String()
}
func (r *LabelBIFF5) GetXFIndex() int {
return int(helpers.BytesToUint16(r.ixfe[:]))
}
func (r *LabelBIFF5) Read(stream []byte) {
copy(r.rw[:], stream[:2])
copy(r.col[:], stream[2:4])
copy(r.ixfe[:], stream[4:6])
copy(r.cch[:], stream[6:8])
//copy(r.grbit[:], stream[8:9])
r.rgb = make([]byte, helpers.BytesToUint16(r.cch[:]))
copy(r.rgb[:], stream[8:])
}
func decodeWindows1251(ba []uint8) []uint8 {
dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(ba)
return out
}