-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
139 lines (128 loc) · 2.8 KB
/
lib.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
package xlsx
import (
"math"
"regexp"
"strconv"
"strings"
"sync"
"unicode/utf8"
)
var (
colNames = make(map[byte]int)
once sync.Once
)
const (
cols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
colsLen = 26
)
func init() {
once.Do(func() {
for i := 0; i < colsLen; i++ {
colNames[cols[i]] = i
}
})
}
// ColumnNumber convert the column name to column number
//
// Example:
//
// xlsx.ColumnNumber("AY") // returns 51
func ColumnNumber(columnName string) (columnNumber int) {
columnName = strings.ToUpper(columnName)
if ok, _ := regexp.MatchString(`^[A-Z]{1,10}$`, columnName); !ok {
return 0
}
digits := 0
for i := len(columnName) - 1; i >= 0; i-- {
c := columnName[i]
columnNumber += (colNames[c] + 1) * int(math.Pow(colsLen, float64(digits)))
digits++
}
return
}
// ColumnName convert the column number to column name
//
// Example:
//
// xlsx.ColumnName(51) // returns "AY"
func ColumnName(columnNumber int) (columnName string) {
if columnNumber < 1 {
return ""
}
for {
columnName = string([]byte{cols[0] + byte((columnNumber-1)%colsLen)}) + columnName
columnNumber = (columnNumber - 1) / colsLen
//columnNumber--
if columnNumber <= 0 {
break
}
}
return
}
// CellNameToCoordinates convert cell name to [col, row] coordinates
//
// Example:
//
// xlsx.CellNameToCoordinates("A1") // returns 1, 1
// xlsx.CellNameToCoordinates("B5") // returns 2, 5
func CellNameToCoordinates(cellName string) (col int, row int) {
cellName = strings.ToUpper(cellName)
re := regexp.MustCompile(`^([A-Z]+)([1-9][0-9]*)$`)
mcs := re.FindStringSubmatch(cellName)
if len(mcs) < 3 {
return 0, 0
}
col = ColumnNumber(mcs[1])
row, _ = strconv.Atoi(mcs[2])
return
}
// CoordinatesToCellName convert [col, row] coordinates to cell name
//
// Example:
//
// xlsx.CoordinatesToCellName(1, 1) // returns "A1"
func CoordinatesToCellName(col, row int) string {
if col < 1 || row < 1 {
return ""
}
return ColumnName(col) + strconv.Itoa(row)
}
// filter and make sure sheet name valid
func trimSheetName(name string) string {
const nameMaxLen = 31
if strings.ContainsAny(name, `:\/?*[]`) || utf8.RuneCountInString(name) > nameMaxLen {
r := make([]rune, 0, nameMaxLen)
for _, v := range name {
switch v {
case 42, 47, 58, 63, 91, 92, 93: // replace :\/?*[]
continue
default:
r = append(r, v)
}
if len(r) == nameMaxLen {
break
}
}
name = string(r)
}
return name
}
// ColumnRange parse column range
//
// Example:
//
// xlsx.ColumnRange("A:Z") // returns 1, 26
// xlsx.ColumnRange("Z:A") // returns 1, 26
func ColumnRange(columnRange string) (min, max int) {
re := regexp.MustCompile(`^([A-Z]+):([A-Z]+)$`)
mcs := re.FindStringSubmatch(columnRange)
if len(mcs) < 3 {
return 0, 0
}
min = ColumnNumber(mcs[1])
max = ColumnNumber(mcs[2])
if max < min {
min, max = max, min
}
return
}