-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
conditional.go
106 lines (86 loc) · 2.89 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
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"github.com/plandem/xlsx/format/conditional"
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/types"
"strings"
// to link unexported
_ "unsafe"
)
//go:linkname fromConditionalFormat github.com/plandem/xlsx/format/conditional.from
func fromConditionalFormat(f *conditional.Info) (*ml.ConditionalFormatting, []*styles.Info)
type conditionals struct {
sheet *sheetInfo
}
//newConditionals creates an object that implements conditional formatting functionality
func newConditionals(sheet *sheetInfo) *conditionals {
return &conditionals{sheet: sheet}
}
func (c *conditionals) initIfRequired() {
//attach conditionals if required
if c.sheet.ml.ConditionalFormatting == nil {
var conditionals []*ml.ConditionalFormatting
c.sheet.ml.ConditionalFormatting = &conditionals
}
}
//Add adds a conditional formatting with attaching additional refs if required
func (c *conditionals) Add(ci *conditional.Info, refs []types.Ref) error {
c.initIfRequired()
//attach additional refs, if required
if len(refs) > 0 {
ci.Set(conditional.Refs(refs...))
}
if err := ci.Validate(); err != nil {
return err
}
info, formats := fromConditionalFormat(ci)
if info != nil {
var startCol, startRow int
//some rules require starting CellRef in formula
for i, b := range info.Bounds {
if i == 0 {
startCol, startRow = b.FromCol, b.FromRow
} else if b.FromCol < startCol {
startCol = b.FromCol
} else if b.FromRow < startRow {
startRow = b.FromRow
}
}
startCellRef := types.CellRefFromIndexes(startCol, startRow)
for i, styleInfo := range formats {
if styleInfo != nil {
//add a new diff styles
styleID := c.sheet.workbook.doc.styleSheet.addDiffStyle(styleInfo)
info.Rules[i].Style = &styleID
}
//finalize rules
for _, ruleInfo := range info.Rules {
for i, formula := range ruleInfo.Formula {
ruleInfo.Formula[i] = ml.Formula(strings.ReplaceAll(string(formula), ":cell:", string(startCellRef)))
}
}
//add a new conditional
*c.sheet.ml.ConditionalFormatting = append(*c.sheet.ml.ConditionalFormatting, info)
}
}
return nil
}
//Remove deletes a conditional formatting from refs
func (c *conditionals) Remove(refs []types.Ref) {
panic(errorNotSupported)
}
//Resolve checks if requested cIdx and rIdx related to any conditionals formatting and returns it
func (c *conditionals) Resolve(cIdx, rIdx int) *conditional.Info {
//TODO: Populate format.Info with required information
panic(errorNotSupported)
}
func (c *conditionals) pack() {
//conditionals must have at least one object
if c.sheet.ml.ConditionalFormatting != nil && len(*c.sheet.ml.ConditionalFormatting) == 0 {
c.sheet.ml.ConditionalFormatting = nil
}
}