-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
merged_cells.go
67 lines (55 loc) · 1.89 KB
/
merged_cells.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
// 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 xlsx
import (
"fmt"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/types"
)
type mergedCells struct {
sheet *sheetInfo
}
//newMergedCells creates an object that implements merged cells functionality
func newMergedCells(sheet *sheetInfo) *mergedCells {
return &mergedCells{sheet: sheet}
}
//Resolve check if requested cIdx and rIdx related to merged range and if so, then translate indexes to valid values
func (m *mergedCells) Resolve(cIdx, rIdx int) (int, int, bool) {
merged := false
mergedCells := m.sheet.ml.MergeCells.Items
for _, mc := range mergedCells {
if merged = mc.Bounds.Contains(cIdx, rIdx); merged {
cIdx, rIdx = mc.Bounds.FromCol, mc.Bounds.FromRow
break
}
}
return cIdx, rIdx, merged
}
//Merge adds a merged cells info for bounds
func (m *mergedCells) Add(bounds types.Bounds) error {
//let's check existing merged cells for overlapping
for _, mc := range m.sheet.ml.MergeCells.Items {
if mc.Bounds.Overlaps(bounds) {
return fmt.Errorf("intersection of different merged ranges is not allowed, %s intersects with %s", mc.Bounds, bounds)
}
}
//looks like there are no any merged cells in that area, so let's add it
m.sheet.ml.MergeCells.Items = append(m.sheet.ml.MergeCells.Items, &ml.MergeCell{
Bounds: bounds,
})
return nil
}
//Remove removes merged cells info for bounds
func (m *mergedCells) Remove(bounds types.Bounds) {
if len(m.sheet.ml.MergeCells.Items) > 0 {
newMergedCells := make([]*ml.MergeCell, 0, len(m.sheet.ml.MergeCells.Items))
for _, mc := range m.sheet.ml.MergeCells.Items {
if !mc.Bounds.Overlaps(bounds) {
//copy only non overlapping bounds
newMergedCells = append(newMergedCells, mc)
}
}
m.sheet.ml.MergeCells.Items = newMergedCells
}
}