-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_xlsx.go
317 lines (259 loc) · 6.04 KB
/
io_xlsx.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package gandalff
import (
"fmt"
"io"
"os"
"time"
"github.com/caerbannogwhite/preludiometa"
"github.com/tealeg/xlsx"
)
type XlsxReader struct {
path string
sheet string
header int
rows int
guessDataTypeLen int
nullValues bool
schema *preludiometa.Schema
ctx *Context
}
func NewXlsxReader(ctx *Context) *XlsxReader {
return &XlsxReader{
path: "",
sheet: "",
header: 0,
rows: -1,
guessDataTypeLen: XLSX_READER_DEFAULT_GUESS_DATA_TYPE_LEN,
nullValues: false,
schema: nil,
ctx: ctx,
}
}
func (r *XlsxReader) SetPath(path string) *XlsxReader {
r.path = path
return r
}
func (r *XlsxReader) SetSheet(sheet string) *XlsxReader {
r.sheet = sheet
return r
}
func (r *XlsxReader) SetHeader(header int) *XlsxReader {
r.header = header
return r
}
func (r *XlsxReader) SetRows(rows int) *XlsxReader {
r.rows = rows
return r
}
func (r *XlsxReader) SetGuessDataTypeLen(guessDataTypeLen int) *XlsxReader {
r.guessDataTypeLen = guessDataTypeLen
return r
}
func (r *XlsxReader) SetNullValues(nullValues bool) *XlsxReader {
r.nullValues = nullValues
return r
}
func (r *XlsxReader) SetSchema(schema *preludiometa.Schema) *XlsxReader {
r.schema = schema
return r
}
func (r *XlsxReader) Read() DataFrame {
if r.ctx == nil {
return BaseDataFrame{err: fmt.Errorf("XlsxReader: no context specified"), ctx: r.ctx}
}
names, series, err := readXlsx(r.path, r.sheet, r.header, r.rows, r.nullValues, r.guessDataTypeLen, r.schema, r.ctx)
if err != nil {
return BaseDataFrame{err: err, ctx: r.ctx}
}
df := NewBaseDataFrame(r.ctx)
for i, name := range names {
df = df.AddSeries(name, series[i])
}
return df
}
type xlsxRowReader struct {
sh *xlsx.Sheet
row int
cols int
cells []*xlsx.Cell
}
func (r *xlsxRowReader) Read() ([]string, error) {
if r.row >= r.sh.MaxRow {
return nil, io.EOF
}
row := r.sh.Row(r.row)
r.cells = row.Cells
r.row++
values := make([]string, len(r.cells))
for i, cell := range r.cells {
values[i] = cell.String()
}
if len(values) < r.cols {
for i := len(values); i < r.cols; i++ {
values = append(values, "")
}
}
return values, nil
}
func readXlsx(
path string, sheet string, header, rows int, nullValues bool,
guessDataTypeLen int, schema *preludiometa.Schema, ctx *Context,
) ([]string, []Series, error) {
wb, err := xlsx.OpenFile(path)
if err != nil {
return nil, nil, err
}
sh, ok := wb.Sheet[sheet]
if !ok {
return nil, nil, fmt.Errorf("Sheet %s not found", sheet)
}
if rows < 0 {
rows = sh.MaxRow
}
names := make([]string, len(sh.Row(header).Cells))
for i, cell := range sh.Row(header).Cells {
names[i] = cell.String()
}
xlsxRowReader := &xlsxRowReader{
sh: sh,
row: header + 1,
cols: len(names),
cells: nil,
}
series, err := readRowData(xlsxRowReader, nullValues, guessDataTypeLen, sh.MaxRow, schema, ctx)
if err != nil {
return nil, nil, err
}
return names, series, nil
}
type XlsxWriter struct {
path string
sheet string
naText string
writer io.Writer
dataframe DataFrame
}
func NewXlsxWriter() *XlsxWriter {
return &XlsxWriter{
path: "",
sheet: "Sheet1",
writer: nil,
dataframe: nil,
}
}
func (w *XlsxWriter) SetPath(path string) *XlsxWriter {
w.path = path
return w
}
func (w *XlsxWriter) SetSheet(sheet string) *XlsxWriter {
w.sheet = sheet
return w
}
func (w *XlsxWriter) SetNaText(naText string) *XlsxWriter {
w.naText = naText
return w
}
func (w *XlsxWriter) SetWriter(writer io.Writer) *XlsxWriter {
w.writer = writer
return w
}
func (w *XlsxWriter) SetDataFrame(dataframe DataFrame) *XlsxWriter {
w.dataframe = dataframe
return w
}
func (w *XlsxWriter) Write() DataFrame {
if w.dataframe == nil {
return BaseDataFrame{err: fmt.Errorf("XlsxWriter: no dataframe specified"), ctx: w.dataframe.GetContext()}
}
if w.dataframe.IsErrored() {
return w.dataframe
}
if w.path != "" {
// make sure os.O_WRONLY arg is supplies so that file is not opened in read-only mode
file, err := os.OpenFile(w.path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
defer file.Close()
w.writer = file
}
if w.writer == nil {
return BaseDataFrame{err: fmt.Errorf("XlsxWriter: no writer specified"), ctx: w.dataframe.GetContext()}
}
err := writeXlsx(w.dataframe, w.writer, w.sheet, w.naText)
if err != nil {
w.dataframe = BaseDataFrame{err: err, ctx: w.dataframe.GetContext()}
}
return w.dataframe
}
func writeXlsx(dataframe DataFrame, writer io.Writer, sheetName string, naText string) error {
file := xlsx.NewFile()
sheet, err := file.AddSheet(sheetName)
if err != nil {
return err
}
// write header
row := sheet.AddRow()
for _, name := range dataframe.Names() {
cell := row.AddCell()
cell.Value = name
}
// write data
for i := 0; i < dataframe.NRows(); i++ {
row := sheet.AddRow()
for j := range dataframe.Names() {
cell := row.AddCell()
switch s := dataframe.At(j).(type) {
case SeriesBool:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetBool(s.Get(i).(bool))
case SeriesInt:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetInt(s.Get(i).(int))
case SeriesInt64:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetInt64(s.Get(i).(int64))
case SeriesFloat64:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetFloat(s.Get(i).(float64))
case SeriesString:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.Value = s.Get(i).(string)
case SeriesTime:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetDateTime(s.Get(i).(time.Time))
case SeriesDuration:
if s.IsNull(i) {
cell.Value = naText
continue
}
cell.SetInt(int(s.Get(i).(time.Duration).Nanoseconds()))
default:
cell.Value = naText
}
}
}
err = file.Write(writer)
if err != nil {
return err
}
return nil
}