Skip to content

Commit

Permalink
[misc] return a named list in dims_to_rowcol()
Browse files Browse the repository at this point in the history
And use this in code. To be a bit more explicit of what is going on.
  • Loading branch information
JanMarvin committed Dec 30, 2024
1 parent e31e721 commit c430507
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions R/class-comment.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ do_write_comment <- function(

if (!is.null(dims)) {
ref <- dims
col <- col2int(dims_to_rowcol(dims)[[1]])
row <- as.integer(dims_to_rowcol(dims)[[2]])
col <- col2int(dims_to_rowcol(dims)[["col"]])
row <- as.integer(dims_to_rowcol(dims)[["row"]])
} else {
if (!is.numeric(col)) {
col <- col2int(col)
Expand Down
38 changes: 19 additions & 19 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1766,8 +1766,8 @@ wbWorkbook <- R6::R6Class(
ddims <- dims_to_rowcol(dims, as_integer = TRUE)

dims <- rowcol_to_dims(
row = c(ddims[[2]], ddims[[2]] + 12L),
col = c(ddims[[1]], ddims[[1]] + 1L)
row = c(ddims[["row"]], ddims[["row"]] + 12L),
col = c(ddims[["col"]], ddims[["col"]] + 1L)
)
}

Expand Down Expand Up @@ -2117,8 +2117,8 @@ wbWorkbook <- R6::R6Class(
ddims <- dims_to_rowcol(dims, as_integer = TRUE)

dims <- rowcol_to_dims(
row = c(ddims[[2]], ddims[[2]] + 12L),
col = c(ddims[[1]], ddims[[1]] + 1L)
row = c(ddims[["row"]], ddims[["row"]] + 12L),
col = c(ddims[["col"]], ddims[["col"]] + 1L)
)
}

Expand Down Expand Up @@ -3810,8 +3810,8 @@ wbWorkbook <- R6::R6Class(
}

rowcol <- dims_to_rowcol(ref)
ref_rows <- as.integer(rowcol[[2]])
ref <- rowcol_to_dims(c(ref_rows, max(ref_rows) + 1L), rowcol[[1]])
ref_rows <- as.integer(rowcol[["row"]])
ref <- rowcol_to_dims(c(ref_rows, max(ref_rows) + 1L), rowcol[["col"]])
}


Expand Down Expand Up @@ -5336,8 +5336,8 @@ wbWorkbook <- R6::R6Class(

ddims <- dims_to_rowcol(dims)

rows <- ddims[[2]]
cols <- ddims[[1]]
rows <- ddims[["row"]]
cols <- ddims[["col"]]

sheet <- private$get_sheet_index(sheet)
self$worksheets[[sheet]]$unmerge_cells(
Expand Down Expand Up @@ -5776,8 +5776,8 @@ wbWorkbook <- R6::R6Class(
}

ddims <- dims_to_rowcol(dims, as_integer = TRUE)
rows <- ddims[[2]]
cols <- ddims[[1]]
rows <- ddims[["row"]]
cols <- ddims[["col"]]

if (!is.null(style)) assert_class(style, "character")
assert_class(type, "character")
Expand All @@ -5793,8 +5793,8 @@ wbWorkbook <- R6::R6Class(
rows <- as.integer(rows)
} else if (!is.null(dims)) {
rowcol <- dims_to_rowcol(dims, as_integer = TRUE)
rows <- rowcol[[2]]
cols <- rowcol[[1]]
rows <- rowcol[["row"]]
cols <- rowcol[["col"]]
}

## check valid rule
Expand Down Expand Up @@ -6657,18 +6657,18 @@ wbWorkbook <- R6::R6Class(

if (!is.null(dims)) {
xy <- dims_to_rowcol(dims)
left <- col2int(xy[[1]][1]) - 1L
top <- as.integer(xy[[2]][1]) - 1L
left <- col2int(xy[["col"]][1]) - 1L
top <- as.integer(xy[["row"]][1]) - 1L

# for A1:B2
if (length(xy[[1]]) > 1) {
right <- max(col2int(xy[[1]]))
right <- max(col2int(xy[["col"]]))
} else {
right <- left + 1L
}

if (length(xy[[2]]) > 1) {
bottom <- max(as.integer(xy[[2]]))
bottom <- max(as.integer(xy[["row"]]))
} else {
bottom <- top + 1L
}
Expand Down Expand Up @@ -7937,8 +7937,8 @@ wbWorkbook <- R6::R6Class(
}

rowcols <- dims_to_rowcol(dims, as_integer = TRUE)
rows <- rowcols[[2]]
cols <- rowcols[[1]]
rows <- rowcols[["row"]]
cols <- rowcols[["col"]]

startCol <- min(cols)
endCol <- max(cols)
Expand Down Expand Up @@ -8931,7 +8931,7 @@ wbWorkbook <- R6::R6Class(

if (!is.null(rows)) {
if (is.character(rows)) # row2int
rows <- as.integer(dims_to_rowcol(rows)[[2]])
rows <- as.integer(dims_to_rowcol(rows)[["row"]])

dims <- wb_dims(rows, "A")
cells <- unname(unlist(dims_to_dataframe(dims, fill = TRUE)))
Expand Down
4 changes: 2 additions & 2 deletions R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ wbWorksheet <- R6::R6Class(

ddims <- dims_to_rowcol(dims)

rows <- ddims[[2]]
cols <- ddims[[1]]
rows <- ddims[["row"]]
cols <- ddims[["col"]]

rows <- range(as.integer(rows))
cols <- range(col2int(cols))
Expand Down
4 changes: 2 additions & 2 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ fits_in_dims <- function(x, dims, startCol, startRow) {
if (fits) {

# why oh why wasn't dims_to_rowcol()/rowcol_to_dims() created as a matching pair
dims <- rowcol_to_dims(row = dims[[2]], col = dims[[1]])
dims <- rowcol_to_dims(row = dims[["row"]], col = dims[["col"]])

} else {

Expand All @@ -1288,7 +1288,7 @@ fits_in_dims <- function(x, dims, startCol, startRow) {
}

rc <- dims_to_rowcol(dims)
if (max(as.integer(rc[[2]])) > 1048576 || max(col2int(rc[[1]])) > 16384)
if (max(as.integer(rc[["row"]])) > 1048576 || max(col2int(rc[["col"]])) > 16384)
stop("Dimensions exceed worksheet")

dims
Expand Down
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ random_string <- function(n = 1, length = 16, pattern = "[A-Za-z0-9]", keep_seed
#' @param single argument indicating if [rowcol_to_dims()] returns a single cell dimension
#' @returns
#' * A `dims` string for `_to_dim` i.e "A1:A1"
#' * A list of rows and columns for `to_rowcol`
#' * A named list of rows and columns for `to_rowcol`
#' @examples
#' dims_to_rowcol("A1:J10")
#' wb_dims(1:10, 1:10)
Expand Down Expand Up @@ -223,7 +223,7 @@ dims_to_rowcol <- function(x, as_integer = FALSE) {
rows_out <- unique(c(rows_out, rows))
}

list(cols_out, rows_out)
setNames(list(cols_out, rows_out), c("col", "row"))
}

#' @rdname dims_helper
Expand Down Expand Up @@ -754,8 +754,8 @@ wb_dims <- function(..., select = NULL) {
}
# transform to
from_row_and_col <- dims_to_rowcol(args$from_dims, as_integer = TRUE)
fcol <- from_row_and_col[[1]]
frow <- from_row_and_col[[2]]
fcol <- from_row_and_col[["col"]]
frow <- from_row_and_col[["row"]]
} else {
fcol <- args$from_col %||% 1L
frow <- args$from_row %||% 1L
Expand Down
8 changes: 4 additions & 4 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ write_data2 <- function(
rows_attr <- vector("list", nrow(rtyp))

# create <rows ...>
want_rows <- as.integer(dims_to_rowcol(dims)[[2]])
want_rows <- as.integer(dims_to_rowcol(dims)[["row"]])
rows_attr <- empty_row_attr(n = length(want_rows))
# number of rows might differ
if (enforce) rows_attr <- empty_row_attr(n = nrow(rtyp))
Expand Down Expand Up @@ -873,8 +873,8 @@ write_data_table <- function(
if (!is.null(dims)) {
dims <- dims_to_rowcol(dims, as_integer = TRUE)
# if dims = "K1,A1" startCol = "A" and startRow = "1" are selected
startCol <- min(dims[[1]])
startRow <- min(dims[[2]])
startCol <- min(dims[["col"]])
startRow <- min(dims[["row"]])
}

# avoid stoi error with NULL
Expand Down Expand Up @@ -1342,7 +1342,7 @@ do_write_formula <- function(
# transpose match write_data_table
if (array || enforce) {
rc <- dims_to_rowcol(dims)
if (length(rc[[1]]) > length(rc[[2]])) {
if (length(rc[["col"]]) > length(rc[["row"]])) {
dfx <- transpose_df(dfx)
}
}
Expand Down
2 changes: 1 addition & 1 deletion man/dims_helper.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ test_that("rbindlist", {

test_that("dims to col & row and back", {

exp <- list(c("A", "B"), c("1", "2"))
exp <- list(col = c("A", "B"), row = c("1", "2"))
got <- dims_to_rowcol("A1:B2")
expect_equal(exp, got)

exp <- list(1:2, 1:2)
exp <- list(col = 1:2, row = 1:2)
got <- dims_to_rowcol("A1:B2", as_integer = TRUE)
expect_equal(exp, got)

exp <- list(1:2, c(1L))
exp <- list(col = 1:2, row = c(1L))
got <- dims_to_rowcol("A:B", as_integer = TRUE)
expect_equal(exp, got)

exp <- list("A", c("1"))
exp <- list(col = "A", row = c("1"))
got <- dims_to_rowcol("A:A", as_integer = FALSE)
expect_equal(exp, got)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-wb_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test_that("dims_to_dataframe", {
got <- dims_to_dataframe("A1;A2,C1;C2", fill = TRUE, empty_rm = TRUE)
expect_equal(exp, got)

exp <- list(c("A", "B"), "1")
exp <- list(col = c("A", "B"), row = "1")
got <- dims_to_rowcol("A1;B1")
expect_equal(exp, got)

Expand Down

0 comments on commit c430507

Please sign in to comment.