-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcell.go
180 lines (140 loc) · 3.11 KB
/
cell.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
package goxcel
import (
"github.com/devlights/goxcel/constants"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
type (
Cell struct {
goxcelObj GoxcelObject
comObj *ole.IDispatch
}
)
func newCell(goxcelObj GoxcelObject, comObj *ole.IDispatch) *Cell {
cell := &Cell{
goxcelObj: goxcelObj,
comObj: comObj,
}
cell.Releaser().Add(func() error {
cell.ComObject().Release()
return nil
})
return cell
}
func NewCell(ws *Worksheet, c *ole.IDispatch) *Cell {
return newCell(ws, c)
}
func NewCellFromRange(ra *XlRange, c *ole.IDispatch) *Cell {
return newCell(ra, c)
}
func (c *Cell) ComObject() *ole.IDispatch {
return c.comObj
}
func (c *Cell) Goxcel() *Goxcel {
return c.goxcelObj.Goxcel()
}
func (c *Cell) Releaser() *Releaser {
return c.Goxcel().Releaser()
}
func (c *Cell) Row() (int32, error) {
v, err := oleutil.GetProperty(c.ComObject(), "Row")
if err != nil {
return -1, err
}
row, ok := v.Value().(int32)
if !ok {
return 0, ValueCantConvertToInt
}
return row, nil
}
func (c *Cell) Column() (int32, error) {
v, err := oleutil.GetProperty(c.ComObject(), "Column")
if err != nil {
return -1, err
}
column, ok := v.Value().(int32)
if !ok {
return 0, ValueCantConvertToInt
}
return column, nil
}
func (c *Cell) Value() (interface{}, error) {
v, err := oleutil.GetProperty(c.ComObject(), "Value")
if err != nil {
return nil, err
}
return v.Value(), nil
}
func (c *Cell) MustValue() interface{} {
v, err := c.Value()
if err != nil {
panic(err)
}
return v
}
func (c *Cell) SetValue(value interface{}) error {
_, err := oleutil.PutProperty(c.ComObject(), "Value", value)
if err != nil {
return err
}
return nil
}
func (c *Cell) MustSetValue(value interface{}) {
err := c.SetValue(value)
if err != nil {
panic(err)
}
}
func (c *Cell) End(direction constants.XlDirection) (*XlRange, error) {
ra, err := oleutil.GetProperty(c.ComObject(), "End", int(direction))
if err != nil {
return nil, err
}
r := NewRangeFromCell(c, ra.ToIDispatch())
return r, nil
}
func (c *Cell) Select() error {
_, err := oleutil.CallMethod(c.ComObject(), "Select")
return err
}
func (c *Cell) String() (string, error) {
value, err := c.Value()
if err != nil {
return "", err
}
s, ok := value.(string)
if !ok {
return "", ValueCantConvertToString
}
return s, nil
}
func (c *Cell) Font() (*Font, error) {
v, err := oleutil.GetProperty(c.ComObject(), "Font")
if err != nil {
return nil, err
}
font := NewFont(c, v.ToIDispatch())
return font, nil
}
func (c *Cell) Interior() (*Interior, error) {
v, err := oleutil.GetProperty(c.ComObject(), "Interior")
if err != nil {
return nil, err
}
interior := NewInterior(c, v.ToIDispatch())
return interior, nil
}
func (c *Cell) SetNumberFormatLocal(format constants.NumberFormatLocal) error {
_, err := oleutil.PutProperty(c.ComObject(), "NumberFormatLocal", string(format))
if err != nil {
return err
}
return nil
}
func (c *Cell) PageBreak(pageBreakType constants.XlPageBreak) error {
_, err := oleutil.PutProperty(c.ComObject(), "PageBreak", int(pageBreakType))
if err != nil {
return err
}
return err
}