Skip to content

Commit

Permalink
to_r1c1_col() and to_a1_col() functions are added
Browse files Browse the repository at this point in the history
  • Loading branch information
seokhoonj committed Dec 9, 2024
1 parent e60f9a6 commit 5a271bc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export(sum_by_colnames)
export(sum_by_dimnames)
export(sum_by_rownames)
export(timeit)
export(to_a1_col)
export(to_r1c1_col)
export(traverse)
export(trim_ws)
export(type)
Expand Down
65 changes: 65 additions & 0 deletions R/excel.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
#' To a R1C1 format column
#'
#' Convert a cell or column string from A1 to R1C1 format.
#'
#' @param x A string specifying a cell or a column
#' @return A numeric
#'
#' @examples
#' # convert a cell or column string from A1 to R1C1 format
#' \donttest{
#' to_r1c1_col("C33")
#' to_r1c1_col("ABC")
#' to_r1c1_col("ABC123")}
#'
#' @export
to_r1c1_col <- function(x) {
tbl <- seq_along(LETTERS)
names(tbl) <- LETTERS
spl <- unlist(strsplit(get_pattern("[A-Z]+", x), split = "",
perl = TRUE))
num <- tbl[spl]
dig <- rev(seq_along(num) - 1)
return(sum(num * 26^dig))
}

#' To a A1 format column
#'
#' Convert a cell or column numeric from R1C1 to A1 format.
#'
#' @param x A string specifying a cell or a column
#' @return A string
#'
#' @examples
#' # convert a cell or column numeric from R1C1 to A1 format.
#' \donttest{
#' to_a1_col(1)
#' to_a1_col(3)
#' to_a1_col(26)}
#'
#' @export
to_a1_col <- function(x) {
tbl <- seq_along(LETTERS)
names(tbl) <- LETTERS
if (x <= 26) {
return(paste0(names(tbl[x]), collapse = ""))
}
quo_vec <- vector(mode = "integer")
rem_vec <- vector(mode = "integer")
i <- 1
while (x > 26) {
quo_vec[i] <- quo <- x%/%26
rem_vec[i] <- rem <- x%%26
x <- quo
i <- i + 1L
}
quo <- quo_vec[length(quo_vec)]
z <- c(quo, rev(rem_vec))
for (i in rev(2:length(z))) {
if (z[i] == 0) {
z[i] <- 26
z[i - 1] <- z[i - 1] - 1L
}
}
return(paste0(names(tbl[z]), collapse = ""))
}

write_data <- function(wb, sheet, data, rc = c(1L, 1L), rowNames = TRUE,
fontName = "Comic Sans MS", borderColour = "#000000",
Expand Down
25 changes: 25 additions & 0 deletions man/to_a1_col.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/to_r1c1_col.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a271bc

Please sign in to comment.