Advertisement
cydside

Column number to name as in excel

Jan 16th, 2024
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.57 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "errors"
  5.     "fmt"
  6. )
  7.  
  8. func main() {
  9.  
  10.     for cm := 1; cm < 100; cm++ {
  11.         cmName, _ := ColumnNumberToName(cm)
  12.         fmt.Println(cmName)
  13.     }
  14.  
  15. }
  16.  
  17. func ColumnNumberToName(num int) (string, error) {
  18.     if num < 1 {
  19.         return "", fmt.Errorf("incorrect column number %d", num)
  20.     }
  21.     if num > 16384 {
  22.         return "", errors.New("column number exceeds maximum limit")
  23.     }
  24.     var col string
  25.     for num > 0 {
  26.         col = string(rune((num-1)%26+65)) + col
  27.         num = (num - 1) / 26
  28.     }
  29.     return col, nil
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement