Monday, September 28, 2020

Convert Excel column index between integer and A1 format

# Excel specifications and limits: https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3

.EXCLE_COLUMN_LIMIT = 16383L 


# Use static lookup for speed at the cost of space

.COLUMNS <- c(LETTERS,

              apply(gtools::permutations(26, 2, LETTERS, repeats.allowed = TRUE),

                    1,  paste, collapse=""),

              apply(gtools::permutations(26, 3, LETTERS, repeats.allowed = TRUE),

                    1,  paste, collapse=""))

.COLUMNS <- .COLUMNS[1:.EXCLE_COLUMN_LIMIT]


             

mapExcelColumnInt2Col <- function(aInt){

    return(COLUMNS[aInt])                 

}



mapExcelColumnCol2Int <- function(aCol){

    return(which(toupper(aCol) == .COLUMNS)[[1]])

}

# maybe add parameter validation