golang /js index 转换excel字母表头

Golang

 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6     var Letters = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
 7     row := 675
 8     result := Letters[row%26]
 9     row = row / 26
10     for row > 0 {
11         row = row - 1
12         result = Letters[row%26] + result
13         row = row / 26
14     }
15     fmt.Println("result==", result)
16 }

JS:

 1 function getPostition(index) {
 2     let result = String.fromCharCode(65 + parseInt(index % 26))
 3     let value = index / 26
 4     while (value >= 1) {
 5         value = value - 1
 6         result = String.fromCharCode(65 + parseInt(value % 26)) + result
 7         value = parseInt(value / 26)
 8     }
 9     return result
10 }

 

posted @ 2019-12-20 09:32  少年工藤  阅读(477)  评论(0编辑  收藏  举报