-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
108 lines (97 loc) · 2.03 KB
/
utils.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
package excelKit
import (
"fmt"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"regexp"
"strconv"
)
// ConvertAxisToRowCol
/*
e.g.
("C2") =>
*/
func ConvertAxisToRowCol(axis string) (int, int, error) {
re := regexp.MustCompile(`^([A-Z]+)(\d+)$`)
if !re.MatchString(axis) {
return 0, 0, errorKit.New("axis(%s) is invalid", axis)
}
var tmp = re.FindAllStringSubmatch(axis, -1)
if len(tmp) != 1 {
return 0, 0, errorKit.New("axis(%s) is invalid", axis)
}
var s []string = tmp[0]
if len(s) != 3 {
return 0, 0, errorKit.New("axis(%s) is invalid", axis)
}
colStr := s[1]
rowStr := s[2]
fmt.Println(rowStr, colStr)
// TODO:
return 0, 0, nil
}
// ConvertRowColToAxis
/*
e.g.
(1, 2) => "C2", nil
*/
func ConvertRowColToAxis(row, col int) (string, error) {
str, err := ConvertColToString(col)
if err != nil {
return "", err
}
str1, err := ConvertRowToString(row)
if err != nil {
return "", err
}
return str + str1, nil
}
//func ConvertStringToRow(str string) (int, error) {
// row, err := strconv.Atoi(str)
// if err != nil {
// return 0, err
// }
// row--
// if row < 0 || row > MaxRow {
// return 0, errorKit.New("row(%d) is invalid", row)
// }
// return row, nil
//}
//
//func ConvertStringToCol(str string) (int, error) {
//
//}
func ConvertRowToString(row int) (string, error) {
if row < 0 || row > MaxRow {
return "", errorKit.New("row(%d) is invalid", row)
}
return strconv.Itoa(row + 1), nil
}
// ConvertColToString
/*
e.g.
(0) => "A", nil
(16368) => "XEO", nil
*/
func ConvertColToString(col int) (string, error) {
/* ts */
//convertToString(col: number): string {
// var rst = "";
//
// while (col >= 0) {
// rst = ToDsnString.fromCharCode(65 + col % 26) + rst;
// col = (col / 26 | 0) - 1;
//}
// return rst;
//}
if col < 0 || col > MaxCol {
return "", errorKit.New("col(%d) is invalid", col)
}
str := ""
for col >= 0 {
// 由于确定不存在中文字符的情况,此处使用 byte 而非 rune
var tmp = byte('A' + col%26)
str = string(tmp) + str
col = col/26 - 1
}
return str, nil
}