Merge pull request #114 from lichaofei/master

change the TitleToNumber function
This commit is contained in:
Ri Xu 2017-09-06 12:17:58 +08:00 committed by GitHub
commit cf1077dc9f
1 changed files with 8 additions and 3 deletions

11
lib.go
View File

@ -70,16 +70,21 @@ func ToAlphaString(value int) string {
} }
// TitleToNumber provides function to convert Excel sheet column title to int // TitleToNumber provides function to convert Excel sheet column title to int
// (this function doesn't do value check currently). For example convert AK to // (this function doesn't do value check currently). For example convert AK
// column title 36: // and ak to column title 36:
// //
// excelize.TitleToNumber("AK") // excelize.TitleToNumber("AK")
// excelize.TitleToNumber("ak")
// //
func TitleToNumber(s string) int { func TitleToNumber(s string) int {
weight := 0.0 weight := 0.0
sum := 0 sum := 0
for i := len(s) - 1; i >= 0; i-- { for i := len(s) - 1; i >= 0; i-- {
sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight)) ch := int(s[i])
if int(s[i]) >= int('a') && int(s[i]) <= int('z') {
ch = int(s[i]) - 32
}
sum = sum + (ch-int('A')+1)*int(math.Pow(26, weight))
weight++ weight++
} }
return sum - 1 return sum - 1