-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.go
220 lines (188 loc) · 5.4 KB
/
table.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
package herschel
import (
"fmt"
"image/color"
"log"
"reflect"
)
// Table represents 2 dimension cells.
type Table struct {
cols int
rows int
values map[int]map[int]interface{}
backgroundColors map[int]map[int]color.Color
numberFormats map[int]map[int]string
numberFormatTypes map[int]map[int]string
FrozenRowCount int64
FrozenColumnCount int64
}
func (t Table) String() string {
return fmt.Sprintf("{Table %d rows x %d cols}", t.rows, t.cols)
}
// NewTable returns instance of Table.
func NewTable(rows int, cols int) *Table {
instance := &Table{cols: cols, rows: rows}
values := map[int]map[int]interface{}{}
backgroundColors := map[int]map[int]color.Color{}
numberFormats := map[int]map[int]string{}
numberFormatTypes := map[int]map[int]string{}
for i := 0; i < rows; i++ {
values[i] = map[int]interface{}{}
backgroundColors[i] = map[int]color.Color{}
numberFormats[i] = map[int]string{}
numberFormatTypes[i] = map[int]string{}
}
instance.values = values
instance.backgroundColors = backgroundColors
instance.numberFormats = numberFormats
instance.numberFormatTypes = numberFormatTypes
instance.FrozenRowCount = 0
instance.FrozenColumnCount = 0
return instance
}
// GetRows returns number of rows
func (t *Table) GetRows() int {
return t.rows
}
// GetCols returns numfer of cols
func (t *Table) GetCols() int {
return t.cols
}
// PutValue updates value of cell.
func (t *Table) PutValue(row int, col int, value interface{}) {
if row >= t.rows {
panic(fmt.Sprintf("row out of order: %d in %d", row, t.rows))
}
if col >= t.cols {
panic(fmt.Sprintf("col out of order: %d in %d", col, t.cols))
}
t.values[row][col] = value
}
// GetValue returns value of cell.
func (t *Table) GetValue(row int, col int) interface{} {
return t.values[row][col]
}
// GetValuesAtRow returns a slice containing value of cells at row
func (t *Table) GetValuesAtRow(row int) []interface{} {
cells := []interface{}{}
for i := 0; i < t.cols; i++ {
cells = append(cells, t.GetValue(row, i))
}
return cells
}
// PutValuesAtRow sets values of cells at row
func (t *Table) PutValuesAtRow(row int, values ...interface{}) {
for i := 0; i < len(values); i++ {
t.PutValue(row, i, values[i])
}
}
// IndexOfRowWithPrefix returns index of row which contains values as prefix. Returns -1 when there is no row matches.
func (t *Table) IndexOfRowWithPrefix(values ...interface{}) int {
if len(values) == 0 {
return -1
}
for i, v := range values {
if reflect.TypeOf(v).Kind() == reflect.Slice {
log.Printf("Unsupported type (slice) in args[%d] (%+v)\n", i, v)
}
}
L:
for i := 0; i < t.rows; i++ {
valuesAtRow := t.GetValuesAtRow(i)
if len(valuesAtRow) < len(values) {
continue L
}
for j := 0; j < len(values); j++ {
if values[j] != valuesAtRow[j] {
continue L
}
}
return i
}
return -1
}
// Values returns slice of cell values
func (t *Table) Values() [][]interface{} {
values := [][]interface{}{}
for row := 0; row < t.rows; row++ {
rowValues := []interface{}{}
for col := 0; col < t.cols; col++ {
rowValues = append(rowValues, t.GetValue(row, col))
}
values = append(values, rowValues)
}
return values
}
// SetBackgroundColor sets background color of cell at (row, col)
func (t *Table) SetBackgroundColor(row int, col int, c color.Color) {
t.backgroundColors[row][col] = c
}
func (t *Table) getBackgroundColor(row int, col int) color.Color {
if c, exists := t.backgroundColors[row][col]; exists {
return c
}
return color.Transparent
}
// SetNumberFormatPattern sets number format pettern of cell at (row, col)
func (t *Table) SetNumberFormatPattern(row int, col int, pattern string) {
t.numberFormats[row][col] = pattern
}
func (t *Table) getNumberFormatPattern(row int, col int) string {
if f, exists := t.numberFormats[row][col]; exists {
return f
}
return ""
}
// SetNumberFormatType sets number format type of cell at (row, col)
func (t *Table) SetNumberFormatType(row int, col int, formatType string) {
t.numberFormatTypes[row][col] = formatType
}
func (t *Table) getNumberFormatType(row int, col int) string {
if f, exists := t.numberFormatTypes[row][col]; exists {
return f
}
return ""
}
// PutCommaSeparatedInt64 set value of cell at (row, col) as comma separated integer.
func (t *Table) PutCommaSeparatedInt64(row int, col int, value int64) {
t.PutValue(row, col, value)
t.SetNumberFormatPattern(row, col, "#,##0")
}
func (t *Table) clearCell(row int, col int) {
delete(t.values[row], col)
delete(t.backgroundColors[row], col)
delete(t.numberFormats[row], col)
delete(t.numberFormatTypes[row], col)
}
// ToMap creates map from table. First column value as key, second column value as value.
func (t *Table) ToMap() map[string]interface{} {
m := map[string]interface{}{}
if t.cols >= 2 {
for row := 0; row < t.rows; row++ {
k := t.GetStringValue(row, 0)
if len(k) > 0 {
m[k] = t.GetValue(row, 1)
}
}
}
return m
}
// ToMapSlice returns a slice of map from table values.
func (t *Table) ToMapSlice() []map[string]interface{} {
if t.rows <= 1 || t.cols == 0 {
return nil
}
header := []string{}
for col := 0; col < t.cols; col++ {
header = append(header, t.GetStringValue(0, col))
}
objects := []map[string]interface{}{}
for row := 1; row < t.rows; row++ {
m := map[string]interface{}{}
for col := 0; col < t.cols; col++ {
m[header[col]] = t.GetValue(row, col)
}
objects = append(objects, m)
}
return objects
}