-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitle.go
More file actions
407 lines (349 loc) · 9.47 KB
/
title.go
File metadata and controls
407 lines (349 loc) · 9.47 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package excelstruct
import (
"fmt"
"reflect"
"unicode/utf8"
"github.com/xuri/excelize/v2"
)
var defaultTileConfig = titleConfig{
tag: defaultTag,
sheetName: DefaultSheetName,
conv: defaultTitleConv,
rowIndex: 1,
}
type titleConfig struct {
tag string
rowIndex int
sheetName string
name []string
conv TitleConv
maxWidth TitleMaxWidth
scaleAutoWidth ScaleAutoWidth
dataValidation DataValidation
validationOverRow int
orient Orientation
numFmt map[excelize.CellType]int
titleNumFmt map[string]int
titleStyle map[string]string
}
// A title is the title of Excel.
type title struct {
file *excelize.File
name []titleName
idx map[string]int
numFmt map[string]int
config titleConfig
}
type titleName struct {
Name string
Column []int
Width map[int]float64 // map[indexColumn]width, using index-column because column may be shifted
// RowData is a row where data not empty
RowData map[int]int // map[indexColumn]row
}
// newTitleFromFile returns the title after reading the Excel.
func newTitleFromFile(config titleConfig, rows *excelize.Rows) (*title, error) {
i := 1
// <= because Next() have to put the pointer to the index row
for ; i <= config.rowIndex; i++ {
if !rows.Next() {
return nil, fmt.Errorf("title row index out of range")
}
}
column, err := rows.Columns()
if err != nil {
return nil, fmt.Errorf("get title columns: %w", err)
}
title := &title{
name: make([]titleName, 0, len(column)),
idx: make(map[string]int, len(column)),
config: config,
}
for i, name := range column {
name = title.config.conv(name)
idx, ok := title.idx[name]
if ok {
title.name[idx].Column = append(title.name[idx].Column, i+1)
continue
}
title.name = append(title.name, titleName{
Name: name,
Column: []int{i + 1},
})
title.idx[name] = len(title.name) - 1
}
return title, nil
}
// newTitleFromStruct returns the title after reading the struct.
func newTitleFromStruct[T any](config titleConfig, file *excelize.File) (*title, error) {
var (
inputName []string
numFmt map[string]int
)
v := reflect.ValueOf(new(T)).Elem()
switch v.Kind() {
case reflect.Struct:
ff := cachedTypeFields(v.Type(), typeOpts{structTag: config.tag}).list
inputName = make([]string, 0, len(ff))
nameIndex := make(map[string]int, len(ff))
for i, v := range ff {
inputName = append(inputName, v.name)
nameIndex[v.name] = i
}
// struct must have a fields in specific title
if len(config.name) > 0 {
exist := make(map[string]struct{}, len(inputName))
for _, v := range inputName {
exist[v] = struct{}{}
}
for _, v := range config.name {
if _, ok := exist[v]; !ok {
return nil, fmt.Errorf("title %q not found in struct tag %q", v, config.tag)
}
}
inputName = config.name
}
// numFmt
numFmt = make(map[string]int, len(inputName))
for _, name := range inputName {
// support only time.Time
switch ff[nameIndex[name]].typ.Kind() {
case reflect.Struct:
if ff[nameIndex[name]].typ != timeType {
continue
}
nf, ok := config.numFmt[excelize.CellTypeDate]
if !ok {
return nil, fmt.Errorf("numFmt date not found")
}
numFmt[name] = nf
if nf, ok := config.titleNumFmt[name]; ok {
numFmt[name] = nf
}
}
}
case reflect.Map:
if len(config.name) == 0 {
return nil, fmt.Errorf("type %q not support without title name", v.Type().String())
}
inputName = config.name
numFmt = make(map[string]int, len(inputName))
default:
return nil, fmt.Errorf("type %q not support", v.Type().String())
}
name := make([]titleName, 0, len(inputName))
idx := make(map[string]int, len(inputName))
for i, f := range inputName {
name = append(name, titleName{
Name: f,
Column: []int{i + 1},
Width: map[int]float64{},
RowData: map[int]int{0: config.rowIndex}, // pointer to the header
})
idx[f] = i
}
return &title{
file: file,
name: name,
idx: idx,
config: config,
numFmt: numFmt,
}, nil
}
// sqrefByRow returns the first range data by the row.
func (t *title) sqrefByRow(name string) (string, error) {
idx, ok := t.idx[name]
if !ok {
return "", fmt.Errorf("title %q not found", name)
}
title := t.name[idx]
col := title.Column[0]
start, err := excelize.CoordinatesToCellName(col, t.config.rowIndex+1, true)
if err != nil {
return "", fmt.Errorf("title %q coordinates to start name: %w", name, err)
}
end, err := excelize.CoordinatesToCellName(col, title.RowData[0], true)
if err != nil {
return "", fmt.Errorf("title %q coordinates to end name: %w", name, err)
}
return fmt.Sprintf("%s!%s:%s", t.config.sheetName, start, end), nil
}
// columnIndex returns the column index of the title.
func (t *title) columnIndex(name string) ([]int, bool) {
if idx, ok := t.idx[name]; ok {
return t.name[idx].Column, true
}
return nil, false
}
// resizeColumnIndex adds the column index of the title.
func (t *title) resizeColumnIndex(name string, lenSlice int) ([]int, error) {
idx, ok := t.idx[name]
if !ok {
return nil, fmt.Errorf("title %q not found", name)
}
col := t.name[idx].Column
if len(col) >= lenSlice {
return col, nil
}
// add column index
endCol := col[len(col)-1]
diff := lenSlice - len(col)
for i := 1; i <= diff; i++ {
col = append(col, col[len(col)-1]+1)
}
if err := t.insertCols(endCol, diff); err != nil {
return nil, fmt.Errorf("title %q insert column: %w", name, err)
}
t.name[idx].Column = col
if err := t.writeTitle(name); err != nil {
return nil, fmt.Errorf("title %q write: %w", name, err)
}
// shift another column index for right-hand side
for i := idx + 1; i < len(t.name); i++ {
for j := range t.name[i].Column {
t.name[i].Column[j] += diff
}
}
return col, nil
}
// rowData returns the row data of the title.
func (t *title) rowData(name string) (int, bool) {
if idx, ok := t.idx[name]; ok {
return t.name[idx].RowData[0], true
}
return 0, false
}
// incRowData increments the row data of the title.
func (t *title) incRowData(name string, col int) {
if idx, ok := t.idx[name]; ok {
for i, v := range t.name[idx].Column {
if v != col {
continue
}
t.name[idx].RowData[i]++
return
}
}
}
// setWidth sets the width of column.
func (t *title) setWidth(name string, col int, cell string) error {
if t.config.scaleAutoWidth == nil {
return nil
}
// cell width
maxWidth := t.config.maxWidth(name)
if maxWidth == 0 {
return nil
}
value, err := t.file.GetCellValue(t.config.sheetName, cell)
if err != nil {
return fmt.Errorf("cell %q get value: %w", cell, err)
}
cellWidth := t.config.scaleAutoWidth(utf8.RuneCountInString(value))
if maxWidth != -1 && cellWidth > maxWidth {
cellWidth = maxWidth
}
// compare prev
titleIdx := t.idx[name]
for idx, v := range t.name[titleIdx].Column {
if v != col {
continue
}
prev := t.name[titleIdx].Width[idx]
if prev < cellWidth {
t.name[titleIdx].Width[idx] = cellWidth
}
break
}
return nil
}
// insertCols inserts the columns.
func (t *title) insertCols(idx, count int) error {
col, _ := excelize.ColumnNumberToName(idx)
if err := t.file.InsertCols(t.config.sheetName, col, count); err != nil {
return err
}
return nil
}
// writeTitle writes the title.
func (t *title) writeTitle(name string) error {
idx, ok := t.idx[name]
if !ok {
return fmt.Errorf("title %q not found", name)
}
for _, c := range t.name[idx].Column {
cell, _ := excelize.CoordinatesToCellName(c, t.config.rowIndex)
if err := t.file.SetCellStr(t.config.sheetName, cell, t.config.conv(name)); err != nil {
return fmt.Errorf("cell %q write: %w", cell, err)
}
}
return nil
}
func (t *title) maxRowData() int {
maxRow := t.config.rowIndex
for _, n := range t.name {
for _, v := range n.RowData {
maxRow = max(maxRow, v)
}
}
return maxRow
}
// writeDataValidation writes the data validation.
func (t *title) writeDataValidation() error {
if t.config.dataValidation == nil {
return nil
}
maxRow := t.maxRowData()
if t.config.validationOverRow > 0 {
maxRow += t.config.validationOverRow
}
// no needs a data validation because there is no written data
if maxRow == t.config.rowIndex {
return nil
}
for _, n := range t.name {
dv, err := t.config.dataValidation(n.Name)
if err != nil {
return fmt.Errorf("title %q data validation func has error: %w", n.Name, err)
}
if dv == nil {
continue
}
startCol, endCol := n.Column[0], n.Column[len(n.Column)-1]
from, _ := excelize.CoordinatesToCellName(startCol, t.config.rowIndex+1) // row: +1 for header
to, _ := excelize.CoordinatesToCellName(endCol, maxRow)
cell := from + ":" + to
dv.Sqref = cell
if err := t.file.AddDataValidation(t.config.sheetName, dv); err != nil {
return fmt.Errorf("title %q %q data validation: %w", n.Name, cell, err)
}
}
return nil
}
// writeWidth writes the width of column.
func (t *title) writeWidth() error {
if t.config.scaleAutoWidth == nil {
return nil
}
for _, n := range t.name {
if t.config.maxWidth(n.Name) == 0 {
continue
}
for idx, c := range n.Column {
col, _ := excelize.ColumnNumberToName(c)
if err := t.file.SetColWidth(t.config.sheetName, col, col, n.Width[idx]); err != nil {
return fmt.Errorf("title %q column %q width: %w", n.Name, col, err)
}
}
}
return nil
}
// write writes the titles.
func (t *title) write() error {
for _, n := range t.name {
if err := t.writeTitle(n.Name); err != nil {
return fmt.Errorf("title %q write: %w", n.Name, err)
}
}
return nil
}