-
Notifications
You must be signed in to change notification settings - Fork 2
/
pull_down.go
88 lines (71 loc) · 1.64 KB
/
pull_down.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
package excelizex
import (
"reflect"
)
type SetOptions struct {
HeadOrColName string
Options any
}
func NewOptions(headOrColName string, options any) SetOptions {
return SetOptions{
HeadOrColName: headOrColName,
Options: options,
}
}
// 下拉结构,包含对应的列和具体的选项
type pullDown struct {
target map[string]*PullDownData
options []*PullDownData
}
type PullDownData struct {
col string
data []any
}
func newPullDown() *pullDown {
return &pullDown{target: make(map[string]*PullDownData)}
}
// Merge 合并一个下拉对象至目标下拉对象中
func (p *pullDown) merge(pd *pullDown) *pullDown {
for axis, pullDownData := range pd.target {
p.addOptions(axis, pullDownData.data)
}
return p
}
func (p *pullDown) addOptions(col string, options any) *pullDown {
var (
typ = reflect.TypeOf(options)
val = reflect.ValueOf(options)
list []any
)
if typ.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
list = append(list, val.Index(i).Interface())
}
} else {
list = append(list, val.Interface())
}
if pd, ok := p.target[col]; ok {
pd.data = append(pd.data, list...)
} else {
pdd := &PullDownData{col: col, data: list}
p.options = append(p.options, pdd)
p.target[col] = pdd
}
return p
}
func (p *pullDown) sheet(name string) *Sheet {
s := &Sheet{name: name + OptionsSaveTable}
sData := make([][]any, 0, len(p.data()))
for _, pd := range p.data() {
sData = append(sData, getRowData(pd))
}
s.data = &sData
return s
}
func (p *pullDown) data() [][]any {
d := make([][]any, 0, len(p.options))
for _, o := range p.options {
d = append(d, o.data)
}
return d
}