forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional.go
120 lines (96 loc) · 2.71 KB
/
conditional.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
// 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 conditional
import (
"fmt"
"github.com/miscoler/xlsx/format/conditional/rule"
"github.com/miscoler/xlsx/format/styles"
"github.com/miscoler/xlsx/internal/ml"
"github.com/miscoler/xlsx/internal/ml/primitives"
// to link unexported
_ "unsafe"
)
//
////go:linkname fromRule github.com/miscoler/xlsx/format/conditional/rule.fromRule
//func fromRule(info *rule.Info) (*ml.ConditionalRule, *styles.Info)
//Info is objects that holds combined information about cell conditional format
type Info struct {
info *ml.ConditionalFormatting
rules []*rule.Info
}
//Option is helper type to set options for conditional formatting
type Option func(o *Info)
//New creates and returns Info object with requested options
func New(options ...Option) *Info {
f := &Info{
info: &ml.ConditionalFormatting{},
rules: []*rule.Info{},
}
f.Set(options...)
return f
}
//Set sets new options for conditional
func (f *Info) Set(options ...Option) {
for _, o := range options {
o(f)
}
}
//Validate the conditional formatting information
func (f *Info) Validate() error {
if len(f.info.Bounds) == 0 {
return fmt.Errorf("no any refs for conditional formatting")
}
if len(f.rules) == 0 {
return fmt.Errorf("no any rules for conditional formatting")
}
for i, r := range f.rules {
rInfo, _ := rule.FromRule(r)
if rInfo.Type == 0 {
return fmt.Errorf("conditional rule#%d: no type", i)
}
if rInfo.Priority < 1 {
return fmt.Errorf("conditional rule#%d: priority(%d) can't be higher thatn 1", i, rInfo.Priority)
}
if err := r.Validate(); err != nil {
return err
}
}
return nil
}
//Pivot sets pivot flag of conditional formatting
func Pivot(cf *Info) {
cf.info.Pivot = true
}
//Refs sets references that will be used for this conditional formatting
func Refs(refs ...primitives.Ref) Option {
return func(cf *Info) {
for _, ref := range refs {
cf.info.Bounds.Add(ref)
}
}
}
//AddRule adds another rule to conditional formatting
func AddRule(options ...rule.Option) Option {
return func(cf *Info) {
r := rule.New(options...)
rInfo, _ := rule.FromRule(r)
rInfo.Priority = len(cf.rules) + 1
cf.rules = append(cf.rules, r)
}
}
//private method used to unpack Info
func From(f *Info) (*ml.ConditionalFormatting, []*styles.Info) {
if len(f.rules) == 0 {
return nil, nil
}
allRules := make([]*ml.ConditionalRule, len(f.rules))
allStyles := make([]*styles.Info, len(f.rules))
for i, r := range f.rules {
rInfo, sInfo := rule.FromRule(r)
allRules[i] = rInfo
allStyles[i] = sInfo
}
f.info.Rules = allRules
return f.info, allStyles
}