Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[formula] Restore extending single cell dims for wb_add_formula(). fixes #1130 #1131

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: openxlsx2
Title: Read, Write and Edit 'xlsx' Files
Version: 1.9
Version: 1.9.0.9000
Language: en-US
Authors@R: c(
person("Jordan Mark", "Barbone", email = "jmbarbone@gmail.com", role = "aut", comment = c(ORCID = "0000-0001-9788-3628")),
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# openxlsx2 (development version)

## Fixes

* The integration of the shared formula feature in the previous release broke the silent extension of dims, if a single cell `dims` was provided for an `x` that was larger than a single cell in `wb_add_formula()`. [1131](https://github.com/JanMarvin/openxlsx2/pull/1131)


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

# openxlsx2 1.9

## New features
Expand Down
7 changes: 6 additions & 1 deletion R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,12 @@ do_write_formula <- function(
if (array || enforce) {
dfx <- data.frame("X" = x, stringsAsFactors = FALSE)
} else {
dfx <- dims_to_dataframe(dims)
# if dims a single cell and x > dfx, increase dfx
if (!grepl(":", dims) && (NROW(x) > 1 || NCOL(x) > 1)) {
dfx <- dims_to_dataframe(wb_dims(x = x, from_dims = dims))
} else {
dfx <- dims_to_dataframe(dims)
}
dfx[] <- x
}

Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-formulas.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,22 @@ test_that("writing shared formulas works", {
expect_equal(exp, got)

})

test_that("increase formula dims if required", {

fml <- c("SUM(A2:B2)", "SUM(A3:B3)")

# This only handles single cells, if C2 is passed and length(x) > 1
wb <- wb_workbook()$
add_worksheet()$
add_data(x = matrix(1:4, 2, 2))

wb1 <- wb_add_formula(wb, dims = "C2", x = fml)
wb2 <- wb_add_formula(wb, dims = "C2:D2", x = fml)

expect_equal(
wb1$worksheets[[1]]$sheet_data$cc,
wb2$worksheets[[1]]$sheet_data$cc
)

})
Loading