1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/*
coords calculates coordinates for spreadsheets.
*/
package coords
import (
"errors"
"fmt"
)
const (
maxCols = 16384
maxAlphabets = (maxCols / 26) - 1
)
// ColAlpha returns an alpha representation of a column index.
// index is an integer - ColAlpha(0) returns "A", etc.
func ColIndexToAlpha(index int) (string, error) {
max := len(Colstream) - 1
if index <= max {
return Colstream[index], nil
} else {
msg := fmt.Sprintf("cannot have more than %d columns", max)
return "", errors.New(msg)
}
}
var Colstream = cols(maxAlphabets)
// ColLettersToIndex converts an alpha column
// reference to a zero-based numeric column identifier.
func ColAlphaToIndex(letters string) (int, error) {
max := len(Colstream) - 1
for i, v := range Colstream {
if i > max {
msg := fmt.Sprintf("Cannot exceed maximum of %d", max)
return 0, errors.New(msg)
}
if v == letters {
return i, nil
}
}
return 0, errors.New("Cannot find requested string.")
}
//alphabet generates all the letters of the alphabet.
func alphabet() []string {
letters := make([]string, 26)
for idx := range letters {
letters[idx] = string('A' + byte(idx))
}
return letters
}
//cols generates the alpha column component of Excel cell references
//Adds n alphabets to the first (A..Z) alphabet.
func cols(n int) []string {
out := alphabet()
alen := len(out)
tmp := make([]string, alen)
copy(tmp, out)
for cycle := 0; cycle < n; cycle++ {
for y := 0; y < alen; y++ {
out = append(out, out[(cycle+2)-2]+tmp[y])
}
}
return out
}
|