From 9fb85d9921fd5876372900ccc0933851d6e262bf Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Mon, 23 Dec 2024 18:02:31 +0100 Subject: [PATCH] [write] allow shared formulas without A1 cell reference --- NEWS.md | 9 +++++++++ R/write.R | 2 +- tests/testthat/test-formulas.R | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7860cf42c..8807869e5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# openxlsx2 (development version) + +## Fixes + +* Formulas that do not have a `A1` cell reference to increase, are now shareable too. `wb_add_formula(x = "1", dims = "A1:A2", shared = TRUE)` + + +*************************************************************************** + # openxlsx2 1.12 ## New features diff --git a/R/write.R b/R/write.R index 489d5f20d..51bff5d9d 100644 --- a/R/write.R +++ b/R/write.R @@ -520,7 +520,7 @@ write_data2 <- function( cc$f_t <- "shared" cc[1, "f_ref"] <- dims cc[2:nrow(cc), "f"] <- "" - cc$f_si <- max(int_si) + 1L + cc$f_si <- max(int_si, -1L) + 1L } if (is.null(wb$worksheets[[sheetno]]$sheet_data$cc)) { diff --git a/tests/testthat/test-formulas.R b/tests/testthat/test-formulas.R index 4274d2d18..85c9cc424 100644 --- a/tests/testthat/test-formulas.R +++ b/tests/testthat/test-formulas.R @@ -187,6 +187,13 @@ test_that("writing shared formulas works", { got <- unique(cc$f_si) expect_equal(exp, got) + wb <- wb_workbook()$add_worksheet() + wb$add_formula(x = "1", dims = "A1:B1", shared = TRUE) + + exp <- "shared" + got <- unique(wb$worksheets[[1]]$sheet_data$cc$f_t) + expect_equal(exp, got) + }) test_that("increase formula dims if required", {