将索引转换为A1表示法,反之亦然。

huangapple go评论72阅读模式
英文:

Convert Index to Column A1 Notation and vice-versa

问题

你好!以下是你要翻译的内容:

如何将索引(例如53)转换为列引用(例如BA)在GoLang中?下表显示了列和索引的预期输出。

例如,如果你输入703,你将得到AAA。如果你输入YOU,你将得到17311

将索引转换为A1表示法,反之亦然。

英文:

How would you convert an index, i.e. 53, to a column reference, i.e. BA in GoLang? The table below shows a 2-way expected output for both column & index.

i.e. if you put in 703, you will get AAA. If you put in YOU, you will get 17311.

将索引转换为A1表示法,反之亦然。

答案1

得分: 3

这是一个有趣的问题。解决方案涉及两个函数:

indexToColumn(int) (string, error) 将索引转换为A1表示法。例如,将 703 转换为 AAA

columnToIndex(string) (int, error) 将A1表示法转换为索引。例如,将 BA 转换为 53

以下是代码:

// indexToColumn 将索引值转换为A1表示法
// 索引1对应列A
// 例如,3 对应 C,29 对应 AC,731 对应 ABC
func indexToColumn(index int) (string, error) {

	// 验证索引大小
	maxIndex := 18278
	if index > maxIndex {
		return "", web.Errorf("索引不能大于 %v (列 ZZZ)", maxIndex)
	}

	// 从索引获取列
	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 将A1表示法转换为索引值
// 列A对应索引1
// 例如,C 对应 3,AC 对应 29,ABC 对应 731
func columnToIndex(column string) (int, error) {

	// 从列字符串计算索引
	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("列中包含无效字符,期望 A-Z,但得到了 [%c]", r)
		}
		runePos := int(r-a) + 1
		index += runePos * int(math.Pow(float64(alphabet), float64(i-1)))
		i++
	}

	// 返回列索引和成功状态
	return index, nil

}

以上是代码的翻译结果。

英文:

This was an interesting problem to tackle. The solution involves 2 functions:

indexToColumn(int) (string, error) will convert an index to A1 Notation. e.g. 703 to AAA

columnToIndex(string) (int, error) will convert A1 Notation to an index. e.g. BA to 53

Here is the code:

// indexToColumn takes in an index value &amp; 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 &gt; maxIndex {
return &quot;&quot;, web.Errorf(&quot;index cannot be greater than %v (column ZZZ)&quot;, maxIndex)
}
// Get column from index
l := &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
if index &gt; 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 &amp; 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 = &quot;A&quot;[0]
var z uint8 = &quot;Z&quot;[0]
var alphabet = z - a + 1
i := 1
for n := len(column) - 1; n &gt;= 0; n-- {
r := column[n]
if r &lt; a || r &gt; z {
return 0, web.Errorf(&quot;invalid character in column, expected A-Z but got [%c]&quot;, r)
}
runePos := int(r-a) + 1
index += runePos * int(math.Pow(float64(alphabet), float64(i-1)))
i++
}
// Return column index &amp; success
return index, nil
}

huangapple
  • 本文由 发表于 2022年1月22日 03:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/70806630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定