forked from all_user/stat_center
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
const TotalRows = 1048576
|
|
|
|
const MinColumns = 1
|
|
|
|
const MaxColumns = 16384
|
|
|
|
// ColumnNumberToName provides a function to convert the integer to Excel
|
|
// sheet column title.
|
|
//
|
|
// Example:
|
|
//
|
|
// excelize.ColumnNumberToName(37) // returns "AK", nil
|
|
func ColumnNumberToName(num int) (string, error) {
|
|
if num < MinColumns || num > MaxColumns {
|
|
return "", fmt.Errorf("the column number must be greater than or equal to %d and less than or equal to %d", MinColumns, MaxColumns)
|
|
}
|
|
var col string
|
|
for num > 0 {
|
|
col = string(rune((num-1)%26+65)) + col
|
|
num = (num - 1) / 26
|
|
}
|
|
return col, nil
|
|
}
|
|
|
|
// CoordinatesToCellName converts [X, Y] coordinates to alpha-numeric cell
|
|
// name or returns an error.
|
|
//
|
|
// Example:
|
|
//
|
|
// excelize.CoordinatesToCellName(1, 1) // returns "A1", nil
|
|
// excelize.CoordinatesToCellName(1, 1, true) // returns "$A$1", nil
|
|
func CoordinatesToCellName(col, row int, abs ...bool) (string, error) {
|
|
if col < 1 || row < 1 {
|
|
return "", errors.New("col row < 0")
|
|
}
|
|
if row > TotalRows {
|
|
return "", errors.New("row number exceeds maximum limit")
|
|
}
|
|
colName, err := ColumnNumberToName(col)
|
|
return colName + strconv.Itoa(row), err
|
|
}
|