-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
range.go
178 lines (148 loc) · 4.57 KB
/
range.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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/styles"
"github.com/plandem/xlsx/types"
)
//Range is a object that provides some functionality for cells inside of range. E.g.: A1:D12
type Range struct {
//we don't want to pollute Range with bound's public properties
bounds types.Bounds
sheet Sheet
}
//newRangeFromRef create and returns Range for requested ref
func newRangeFromRef(sheet Sheet, ref types.Ref) *Range {
return &Range{
ref.ToBounds(),
sheet,
}
}
//newRange create and returns Range for requested 0-based indexes
func newRange(sheet Sheet, fromCol, toCol, fromRow, toRow int) *Range {
return &Range{
types.BoundsFromIndexes(fromCol, fromRow, toCol, toRow),
sheet,
}
}
//Bounds returns bounds of range
func (r *Range) Bounds() types.Bounds {
return r.bounds
}
//Reset resets each cell data into zero state
func (r *Range) Reset() {
r.Walk(func(idx, cIdx, rIdx int, c *Cell) { c.Reset() })
}
//Clear clears each cell value in range
func (r *Range) Clear() {
r.Walk(func(idx, cIdx, rIdx int, c *Cell) { c.Clear() })
}
//Cells returns iterator for all cells in range
func (r *Range) Cells() RangeIterator {
return newRangeIterator(r)
}
//Values returns values for all cells in range
func (r *Range) Values() []string {
width, height := r.bounds.Dimension()
values := make([]string, 0, width*height)
r.Walk(func(idx, cIdx, rIdx int, c *Cell) {
values = append(values, c.Value())
})
return values
}
//Walk calls callback cb for each Cell in range
func (r *Range) Walk(cb func(idx, cIdx, rIdx int, c *Cell)) {
for idx, cells := 0, r.Cells(); cells.HasNext(); idx++ {
iCol, iRow, cell := cells.Next()
cb(idx, iCol, iRow, cell)
}
}
//SetStyles sets style format to all cells in range
func (r *Range) SetStyles(styleID styles.DirectStyleID) {
r.Walk(func(idx, cIdx, rIdx int, c *Cell) {
c.SetStyles(styleID)
})
}
func (r *Range) ensureNotStream() {
//result is unpredictable in stream mode
if mode := r.sheet.mode(); (mode & SheetModeStream) != 0 {
panic(errorNotSupportedStream)
}
}
//CopyToRef copies range cells into another range starting with ref.
//N.B.: Merged cells are not supported
func (r *Range) CopyToRef(ref types.Ref) {
target := ref.ToBounds()
r.CopyTo(target.ToCol, target.ToRow)
}
//CopyTo copies range cells into another range starting indexes cIdx and rIdx
//N.B.: Merged cells are not supported
func (r *Range) CopyTo(cIdx, rIdx int) {
//stream is not supported for copying cell's info
r.ensureNotStream()
//ignore self-copying
if cIdx != r.bounds.FromCol || rIdx != r.bounds.FromRow {
cOffset, rOffset := cIdx-r.bounds.FromCol, rIdx-r.bounds.FromRow
r.Walk(func(idx, cIdxSource, rIdxSource int, source *Cell) {
//process only non empty cells
if !isCellEmpty(source.ml) {
//ignore target cells with negative indexes
cIdxTarget, rIdxTarget := cIdxSource+cOffset, rIdxSource+rOffset
if cIdxTarget >= 0 && rIdxTarget >= 0 {
target := r.sheet.Cell(cIdxTarget, rIdxTarget)
//copy data
*target.ml = *source.ml
//refresh ref
target.ml.Ref = types.CellRefFromIndexes(cIdxTarget, rIdxTarget)
}
}
})
}
}
//Merge merges range
func (r *Range) Merge() error {
//stream is not supported for copying cell's info
r.ensureNotStream()
if err := r.sheet.info().mergedCells.Add(r.bounds); err != nil {
return err
}
//we should reset cells and copy first cell with value into the first cell of that range (Excel behavior)
copied := false
r.Walk(func(idx, cIdx, rIdx int, c *Cell) {
//if there is a value and it was not copied yet, then do it
if !copied && len(c.ml.Value) > 0 {
if idx > 0 {
target := r.sheet.Cell(r.bounds.FromCol, r.bounds.FromRow)
*target.ml = *c.ml
target.ml.Ref = types.CellRefFromIndexes(r.bounds.FromCol, r.bounds.FromRow)
}
copied = true
} else {
//cleanup rest info
c.Reset()
}
})
return nil
}
//Split splits cells in range
func (r *Range) Split() {
r.sheet.info().mergedCells.Remove(r.bounds)
}
//SetHyperlink sets hyperlink for range, where link can be string or HyperlinkInfo
func (r *Range) SetHyperlink(link interface{}) error {
styleID, err := r.sheet.info().hyperlinks.Add(r.bounds, link)
if err != nil {
return err
}
r.Walk(func(idx, cIdx, rIdx int, c *Cell) {
c.SetStyles(styleID)
})
return nil
}
//RemoveHyperlink removes hyperlink from cell
func (r *Range) RemoveHyperlink() {
r.Walk(func(idx, cIdx, rIdx int, c *Cell) {
r.sheet.info().hyperlinks.Remove(r.bounds)
})
}