-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon_set.go
127 lines (105 loc) · 2.98 KB
/
icon_set.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
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rule
import (
"fmt"
"github.com/roboninc/xlsx/internal/ml"
"github.com/roboninc/xlsx/internal/ml/primitives"
)
type iconSetRule struct {
baseRule
}
// IconSet is helper object to set specific options for rule
var IconSet iconSetRule
func (x iconSetRule) initIfRequired(r *Info) {
if !r.initialized {
r.initialized = true
r.validator = IconSet
r.rule = &ml.ConditionalRule{
Type: primitives.ConditionTypeIconSet,
IconSet: &ml.IconSet{
//Percent: true //default is true, but never used?!
Type: IconSetType3TrafficLights1,
},
}
x.initValues(r)
}
}
func (x iconSetRule) Default(r *Info) {
x.initIfRequired(r)
}
func (x iconSetRule) initValues(r *Info) {
var total int
if r.rule.IconSet.Type > fromIconSetType3Icons && r.rule.IconSet.Type < fromIconSetType4Icons {
total = 3
} else if r.rule.IconSet.Type > fromIconSetType4Icons && r.rule.IconSet.Type < fromIconSetType5Icons {
total = 4
} else if r.rule.IconSet.Type > fromIconSetType5Icons {
total = 5
}
// The values should start with the highest value and with each subsequent one being lower.
// The lowest number value in an icon set has properties defined by Excel.
// Therefore in a n icon set, there are n-1 values.
// The default value is: (n * 100) / total
values := make([]*ml.ConditionValue, total)
for i := 0; i < total; i++ {
v := ml.ConditionValue{}
v.Type = ValueTypePercent
v.Value = fmt.Sprintf("%d", (i*100)/total)
values[i] = &v
}
r.rule.IconSet.Values = values
}
func (x iconSetRule) Type(t primitives.IconSetType) Option {
return func(r *Info) {
x.initIfRequired(r)
r.rule.IconSet.Type = t
x.initValues(r)
}
}
func (x iconSetRule) ReverseIcons(r *Info) {
x.initIfRequired(r)
r.rule.IconSet.Reverse = true
}
func (x iconSetRule) IconsOnly(r *Info) {
x.initIfRequired(r)
r.rule.IconSet.ShowValue = primitives.OptionalBool(false)
}
func (x iconSetRule) setValue(r *Info, idx int, value string, settings []interface{}) {
x.initIfRequired(r)
total := len(r.rule.IconSet.Values) - 1
if idx < 0 || idx >= total {
return
}
idx = total - idx
for _, p := range settings {
switch pv := p.(type) {
case string:
if pv == ">" {
r.rule.IconSet.Values[idx].GreaterThanEqual = primitives.OptionalBool(false)
}
case primitives.ConditionValueType:
r.rule.IconSet.Values[idx].Type = pv
}
}
r.rule.IconSet.Values[idx].Value = value
}
func (x iconSetRule) Value(index int, value string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, index, value, settings)
}
}
func (x iconSetRule) Validate(r *Info) error {
for i, v := range r.rule.IconSet.Values {
if !v.Type.IsAllowed(
ValueTypeNumber,
ValueTypePercent,
ValueTypeFormula,
ValueTypePercentile,
) {
return fmt.Errorf("iconSet: Not allowed type '%s' for value at index %d", v.Type, i)
}
}
return nil
}