将索引转换为 A1 列表示法,反之亦然
Convert Index to Column A1 Notation and vice-versa
如何将索引(即 53)转换为列引用(即 GoLang 中的 BA)?下面的 table 显示了列和索引的双向预期输出。
即如果你输入 703
,你将得到 AAA
。如果你输入 YOU
,你将得到 17311
.
这是一个需要解决的有趣问题。该解决方案涉及2个功能:
indexToColumn(int) (string, error)
会将索引转换为 A1 表示法。例如703
至 AAA
columnToIndex(string) (int, error
) 会将 A1 Notation 转换为索引。例如BA
至 53
代码如下:
// indexToColumn takes in an index value & converts it to A1 Notation
// Index 1 is Column A
// E.g. 3 == C, 29 == AC, 731 == ABC
func indexToColumn(index int) (string, error) {
// Validate index size
maxIndex := 18278
if index > maxIndex {
return "", web.Errorf("index cannot be greater than %v (column ZZZ)", maxIndex)
}
// Get column from index
l := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if index > 26 {
letterA, _ := indexToColumn(int(math.Floor(float64(index-1)/26)))
letterB, _ := indexToColumn(index%26)
return letterA + letterB, nil
} else {
if index == 0 {
index = 26
}
return string(l[index-1]), nil
}
}
// columnToIndex takes in A1 Notation & converts it to an index value
// Column A is index 1
// E.g. C == 3, AC == 29, ABC == 731
func columnToIndex(column string) (int, error) {
// Calculate index from column string
var index int
var a uint8 = "A"[0]
var z uint8 = "Z"[0]
var alphabet = z - a + 1
i := 1
for n := len(column) - 1; n >= 0; n-- {
r := column[n]
if r < a || r > z {
return 0, web.Errorf("invalid character in column, expected A-Z but got [%c]", r)
}
runePos := int(r-a) + 1
index += runePos * int(math.Pow(float64(alphabet), float64(i-1)))
i++
}
// Return column index & success
return index, nil
}
如何将索引(即 53)转换为列引用(即 GoLang 中的 BA)?下面的 table 显示了列和索引的双向预期输出。
即如果你输入 703
,你将得到 AAA
。如果你输入 YOU
,你将得到 17311
.
这是一个需要解决的有趣问题。该解决方案涉及2个功能:
indexToColumn(int) (string, error)
会将索引转换为 A1 表示法。例如703
至 AAA
columnToIndex(string) (int, error
) 会将 A1 Notation 转换为索引。例如BA
至 53
代码如下:
// indexToColumn takes in an index value & converts it to A1 Notation
// Index 1 is Column A
// E.g. 3 == C, 29 == AC, 731 == ABC
func indexToColumn(index int) (string, error) {
// Validate index size
maxIndex := 18278
if index > maxIndex {
return "", web.Errorf("index cannot be greater than %v (column ZZZ)", maxIndex)
}
// Get column from index
l := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if index > 26 {
letterA, _ := indexToColumn(int(math.Floor(float64(index-1)/26)))
letterB, _ := indexToColumn(index%26)
return letterA + letterB, nil
} else {
if index == 0 {
index = 26
}
return string(l[index-1]), nil
}
}
// columnToIndex takes in A1 Notation & converts it to an index value
// Column A is index 1
// E.g. C == 3, AC == 29, ABC == 731
func columnToIndex(column string) (int, error) {
// Calculate index from column string
var index int
var a uint8 = "A"[0]
var z uint8 = "Z"[0]
var alphabet = z - a + 1
i := 1
for n := len(column) - 1; n >= 0; n-- {
r := column[n]
if r < a || r > z {
return 0, web.Errorf("invalid character in column, expected A-Z but got [%c]", r)
}
runePos := int(r-a) + 1
index += runePos * int(math.Pow(float64(alphabet), float64(i-1)))
i++
}
// Return column index & success
return index, nil
}