-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1.go
51 lines (42 loc) · 796 Bytes
/
a1.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
package main
import (
"strings"
)
func A1ToIndex(s string) int {
if s == "" {
panic("empty column name")
}
n := 0
s = strings.ToUpper(s)
for _, c := range s {
if c < 'A' || c > 'Z' {
panic("invalid column name:" + s)
}
n = n*26 + int(c-'A'+1)
}
return n - 1
}
func A1ToIndeces(s string) []int {
if s == "" {
return []int{}
}
ranges := strings.Split(s, ",")
columns := make([]int, 0)
for _, r := range ranges {
start, end, _ := strings.Cut(r, ":")
if end == "" {
columns = append(columns, A1ToIndex(start))
continue
}
startIndex := A1ToIndex(start)
endIndex := A1ToIndex(end)
step := 1
if startIndex > endIndex {
step = -1
}
for i := startIndex; i != endIndex+step; i += step {
columns = append(columns, i)
}
}
return columns
}