-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.go
More file actions
69 lines (59 loc) · 1.14 KB
/
cell.go
File metadata and controls
69 lines (59 loc) · 1.14 KB
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
package cellwalker
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Cell represents a cell in Excel
type Cell struct {
column int
row int
}
func newCell(col, row int) *Cell {
if col > ColumnsLimit {
col = ColumnsLimit
} else {
if col < 1 {
col = 1
}
}
if row < 1 {
row = 1
} else {
if row > RowsLimit {
row = RowsLimit
}
}
return &Cell{
column: col,
row: row,
}
}
func newCellByID(cellID string) *Cell {
cleanCellID := strings.ToUpper(cellID)
re := regexp.MustCompile(`^([A-Z]+)([0-9]*)$`)
match := re.FindStringSubmatch(cleanCellID)
col := match[1]
row, err := strconv.ParseInt(fmt.Sprintf("0%s", match[2]), 10, 32)
if err != nil || row == 0 {
row = 1
}
return newCell(ColumnNameToIndex(col), int(row))
}
// String representation of Cell
func (c *Cell) String() string {
return fmt.Sprintf("%s%d", ColumnIndexToName(c.column), c.row)
}
// Clone creates a copy of Cell
func (c *Cell) Clone() *Cell {
return newCell(c.column, c.row)
}
// ColumnIndex returns column number
func (c *Cell) ColumnIndex() int {
return c.column
}
// RowIndex returns row number
func (c *Cell) RowIndex() int {
return c.row
}