-
Notifications
You must be signed in to change notification settings - Fork 222
/
databook.go
173 lines (143 loc) · 4.01 KB
/
databook.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
// Copyright 2015 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"os"
tablib "github.com/agrison/go-tablib"
)
type Databook struct {
XDatabook *tablib.Databook
}
func NewDatabook() *Databook {
db := &Databook{XDatabook: tablib.NewDatabook()}
return db
}
func NewDatabookWithData(sheetName map[string]string, data interface{}, mustMatch bool, headers ...map[string][]string) (*Databook, error) {
s := len(sheetName)
switch data.(type) {
case map[string]*tablib.Dataset:
dataModel1 := data.(map[string]*tablib.Dataset)
d1 := len(dataModel1)
if s != d1 && mustMatch {
return nil, ErrParamsType
}
databook := tablib.NewDatabook()
for k, _ := range dataModel1 {
if _, ok := sheetName[k]; !ok {
return nil, ErrParamsType
}
databook.AddSheet(sheetName[k], dataModel1[k])
}
db := &Databook{XDatabook: databook}
return db, nil
case map[string][]map[string]interface{}:
dataModel2 := data.(map[string][]map[string]interface{})
d2 := len(dataModel2)
if len(headers) != 1 {
return nil, ErrParamsType
}
h := len(headers[0])
if (s != h || s != d2) && mustMatch {
return nil, ErrParamsType
}
databook := tablib.NewDatabook()
for k, _ := range dataModel2 {
if _, ok := sheetName[k]; !ok {
return nil, ErrParamsType
}
if _, ok := headers[0][k]; !ok {
return nil, ErrParamsType
}
dataset, err := NewDatasetWithData(headers[0][k], dataModel2[k], mustMatch)
if err != nil {
return nil, err
}
databook.AddSheet(sheetName[k], dataset)
}
db := &Databook{XDatabook: databook}
return db, nil
default:
return nil, ErrParamsType
}
}
func (databook *Databook) AddSheet(title string, data interface{}, mustMatch bool, headers ...[]string) error {
switch data.(type) {
case *tablib.Dataset:
dataset := data.(*tablib.Dataset)
databook.XDatabook.AddSheet(title, dataset)
return nil
case []map[string]interface{}:
dataSlice := data.([]map[string]interface{})
if len(headers) != 1 {
return ErrParamsType
}
dataset, err := NewDatasetWithData(headers[0], dataSlice, mustMatch)
if err != nil {
return err
}
databook.XDatabook.AddSheet(title, dataset)
return nil
default:
return ErrParamsType
}
}
func (databook *Databook) HTML() *tablib.Exportable {
return databook.XDatabook.HTML()
}
func (databook *Databook) SaveAsHTML(filename string, perm os.FileMode) error {
html := databook.XDatabook.HTML()
return html.WriteFile(filename, perm)
}
func (databook *Databook) JSON() (*tablib.Exportable, error) {
return databook.XDatabook.JSON()
}
func (databook *Databook) SaveAsJSON(filename string, perm os.FileMode) error {
json, err := databook.XDatabook.JSON()
if err != nil {
return err
}
return json.WriteFile(filename, perm)
}
func (databook *Databook) XLSX() (*tablib.Exportable, error) {
return databook.XDatabook.XLSX()
}
func (databook *Databook) SaveAsXLSX(filename string, perm os.FileMode) error {
xlsx, err := databook.XDatabook.XLSX()
if err != nil {
return err
}
return xlsx.WriteFile(filename, perm)
}
func (databook *Databook) XML() (*tablib.Exportable, error) {
return databook.XDatabook.XML()
}
func (databook *Databook) SaveAsXML(filename string, perm os.FileMode) error {
xml, err := databook.XDatabook.XML()
if err != nil {
return err
}
return xml.WriteFile(filename, perm)
}
func (databook *Databook) YAML() (*tablib.Exportable, error) {
return databook.XDatabook.YAML()
}
func (databook *Databook) SaveAsYAML(filename string, perm os.FileMode) error {
yaml, err := databook.XDatabook.YAML()
if err != nil {
return err
}
return yaml.WriteFile(filename, perm)
}
func (databook *Databook) Sheet(title string) tablib.Sheet {
return databook.XDatabook.Sheet(title)
}
func (databook *Databook) Sheets() map[string]tablib.Sheet {
return databook.XDatabook.Sheets()
}
func (databook *Databook) Size() int {
return databook.XDatabook.Size()
}
func (databook *Databook) Wipe() {
databook.XDatabook.Wipe()
}