forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.go
50 lines (39 loc) · 1.21 KB
/
ref.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
// 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 primitives
import (
"strings"
)
//Ref is a type to encode XSD ST_Ref, a reference that identifies a cell or a range of cells. E.g.: N28 or B5:N10
type Ref string
//ToCellRefs returns from/to CellRef of Ref
func (r Ref) ToCellRefs() (from CellRef, to CellRef) {
cellRefs := strings.Split(string(r), ":")
if len(cellRefs) == 1 {
from = CellRef(cellRefs[0])
to = CellRef(cellRefs[0])
} else {
from = CellRef(cellRefs[0])
to = CellRef(cellRefs[1])
}
return
}
//ToBounds returns related bounds of Ref
func (r Ref) ToBounds() Bounds {
from, to := r.ToCellRefs()
fromCol, fromRow := from.ToIndexes()
toCol, toRow := to.ToIndexes()
return BoundsFromIndexes(fromCol, fromRow, toCol, toRow)
}
//RefFromCellRefs returns Ref for from/to CellRefs
func RefFromCellRefs(from CellRef, to CellRef) Ref {
if from == to {
return Ref(from)
}
return Ref(string(from) + ":" + string(to))
}
//RefFromIndexes returns Ref for a CellRef of 0-based indexes
func RefFromIndexes(colIndex, rowIndex int) Ref {
return Ref(CellRefFromIndexes(colIndex, rowIndex))
}