This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
/
worksheet.go
236 lines (188 loc) · 6.03 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package goxlsxwriter
import (
"errors"
)
/*
#cgo LDFLAGS: -L. -lxlsxwriter
#include <xlsxwriter.h>
*/
import "C"
import "unsafe"
// Worksheet represents an Excel worksheet.
type Worksheet struct {
CWorksheet *C.struct_lxw_worksheet
Workbook *Workbook
}
// ImageOptions contains options to be set when inserting an image into a
// worksheet.
type ImageOptions struct {
XOffset int
YOffset int
XScale float64
YScale float64
}
// NewWorksheet creates and returns a new instance of Worksheet.
func NewWorksheet(workbook *Workbook, sheetName string) *Worksheet {
cSheetName := C.CString(sheetName)
defer C.free(unsafe.Pointer(cSheetName))
cWorksheet := C.workbook_add_worksheet(workbook.CWorkbook, cSheetName)
worksheet := &Worksheet{
CWorksheet: cWorksheet,
Workbook: workbook,
}
return worksheet
}
// WriteString writes a string value at the specified row and column and applies
// an optional format.
func (w *Worksheet) WriteString(row int, col int, value string, format *Format) error {
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_string(w.CWorksheet, cRow, cCol, cValue, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// WriteFloat writes a float64 value at the specified row and column and applies
// an optional format.
func (w *Worksheet) WriteFloat(row int, col int, value float64, format *Format) error {
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
cValue := (C.double)(value)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_number(w.CWorksheet, cRow, cCol, cValue, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// WriteInt writes an integer value at the specified row and column and applies
// an optional format.
func (w *Worksheet) WriteInt(row int, col int, value int, format *Format) error {
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
cValue := (C.double)(value)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_number(w.CWorksheet, cRow, cCol, cValue, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// WriteFormula writes a formula value at the specified row and column and
// applies an optional format.
func (w *Worksheet) WriteFormula(row int, col int, formula string, format *Format) error {
cValue := C.CString(formula)
defer C.free(unsafe.Pointer(cValue))
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_formula(w.CWorksheet, cRow, cCol, cValue, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// WriteUrl writes a URL and a display string at the specified row and column
// and applies an optional format to the display string. If the specified
// display string is empty, the URL will be used.
func (w *Worksheet) WriteUrl(row int, col int, url string, display string, format *Format) error {
cUrl := C.CString(url)
defer C.free(unsafe.Pointer(cUrl))
cDisplay := C.CString(display)
defer C.free(unsafe.Pointer(cDisplay))
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_url(w.CWorksheet, cRow, cCol, cUrl, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
// If the display string is not empty, write it to the same row and
// column as the URL.
if len(display) > 0 {
err := C.worksheet_write_string(w.CWorksheet, cRow, cCol, cDisplay, nil)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
}
return nil
}
// WriteBool writes a boolean value at the specified row and column and
// applies an optional format.
func (w *Worksheet) WriteBool(row int, col int, value bool, format *Format) error {
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
// Get the int value from the specified boolean.
var intValue int
if value {
intValue = 1
}
cInt := (C.int)(intValue)
err := C.worksheet_write_boolean(w.CWorksheet, cRow, cCol, cInt, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// WriteBlank writes a "blank" cell at the specified row and column and
// applies an optional format. Excel differentiates between an empty cell
// and a blank cell. An empty cell is a cell which doesn't contain data or
// formatting. A blank cell doesn't contain data but does contain formatting.
func (w *Worksheet) WriteBlank(row int, col int, format *Format) error {
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
var cFormat *C.struct_lxw_format
if format != nil {
cFormat = format.CFormat
}
err := C.worksheet_write_blank(w.CWorksheet, cRow, cCol, cFormat)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}
// InsertImage inserts an image at the specified row and column and applies
// options.
func (w *Worksheet) InsertImage(row int, col int, filename string, options *ImageOptions) error {
cRow := (C.lxw_row_t)(row)
cCol := (C.lxw_col_t)(col)
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
var cOptions *C.lxw_image_options
if options != nil {
cOptions = &C.lxw_image_options{
x_offset: (C.int32_t)(options.XOffset),
y_offset: (C.int32_t)(options.YOffset),
x_scale: (C.double)(options.XScale),
y_scale: (C.double)(options.YScale),
}
}
err := C.worksheet_insert_image_opt(w.CWorksheet, cRow, cCol, cFilename, cOptions)
if err != C.LXW_NO_ERROR {
return errors.New(C.GoString(C.lxw_strerror(err)))
}
return nil
}