Skip to content

Commit

Permalink
[wb_dims] without x and non sequential rows, incorrect results were…
Browse files Browse the repository at this point in the history
… returned. fixes #1093
  • Loading branch information
JanMarvin committed Jul 30, 2024
1 parent d058b8a commit 70e78e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Experimental support for reading shared formulas. If `show_formula` is used with `wb_to_df()`, we try to show the value that is shown in spreadsheet software. [1091](https://github.com/JanMarvin/openxlsx2/pull/1091)

## Fixes

* Previously, if only `cols` and `rows` were passed to `wb_dims()` incorrect results were returned. This has been fixed. [1094](https://github.com/JanMarvin/openxlsx2/pull/1094)


***************************************************************************

Expand Down
7 changes: 6 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ wb_dims <- function(..., select = NULL) {
if (all(diff(rows_arg) == 1L))
frow <- frow + min(rows_arg) - 1L
else
frow <- vapply(rows_arg, function(x) frow + min(x) - 1L, NA_real_)
frow <- vapply(rows_arg, function(x) frow + min(x) - 1L, NA_real_)
}
}

Expand All @@ -934,6 +934,11 @@ wb_dims <- function(..., select = NULL) {
ncol_to_span <- 1L
}

if (is.null(args$x) && !all(diff(rows_arg) == 1L)) {
diff <- min(frow) - min(rows_arg)
frow <- diff + rows_arg
}

if (all(diff(frow) == 1))
row_span <- frow + seq_len(nrow_to_span) - 1L
else
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,34 @@ test_that("wb_dims() handles `from_dims`", {
expect_error(wb_dims(from_dims = "65"))
})

test_that("wb_dims() corner cases work", {

exp <- "B2:B2,B30:B30"
got <- wb_dims(rows = c(2, 30), cols = 2) # expect B2,B30
expect_equal(exp, got)

exp <- "B1:B1,B30:B30"
got <- wb_dims(rows = c(1, 30), cols = 2) # expect B1,B30
expect_equal(exp, got)

exp <- "B2:B2,D2:D2,B30:B30,D30:D30"
got <- wb_dims(rows = c(2, 30), cols = c(2, 4)) # expect B2,D2,B30,D30
expect_equal(exp, got)

exp <- "B1:B1,D1:D1,B30:B30,D30:D30"
got <- wb_dims(rows = c(1, 30), cols = c(2, 4)) # expect B1,D1,B30,D30
expect_equal(exp, got)

exp <- "B2:B2,D2:D2,B1:B1,D1:D1"
got <- wb_dims(rows = c(2, 1), cols = c(2, 4)) # expect B2,D2,B1,D1
expect_equal(exp, got)

exp <- "B5:B5,D5:D5,B1:B1,D1:D1"
got <- wb_dims(rows = c(5, 1), cols = c(2, 4)) # expect B5,D5,B1,D1
expect_equal(exp, got)

})

test_that("create_char_dataframe", {

exp <- data.frame(x1 = rep("", 5), z1 = rep("", 5), stringsAsFactors = FALSE)
Expand Down

0 comments on commit 70e78e7

Please sign in to comment.