Skip to content

Commit

Permalink
[tables] escape specials in total row formula (#1181)
Browse files Browse the repository at this point in the history
* [tables] escape special characters in total row function

* [misc] update NEWS again
  • Loading branch information
JanMarvin authored Nov 13, 2024
1 parent e9227c9 commit c3e4b6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Fixes

* Previously rows that trigger scientific notation (e.g. `1e+05`) would cause issues, when matched against a non scientific version. [1170](https://github.com/JanMarvin/openxlsx2/pull/1170)
* When using `wb_add_data_table(..., total_row = TRUE)` the last row of the returned data tabled was replaced with the total row. This caused loss of data. [1179](https://github.com/JanMarvin/openxlsx2/issues/1179)
* When using `wb_add_data_table(..., total_row = TRUE)` the last row of the data table in the workbook was mistakenly overwritten with the total row formula, which should have been placed below the last row of the table. This caused loss of data. [1179](https://github.com/JanMarvin/openxlsx2/issues/1179)


***************************************************************************
Expand Down
10 changes: 8 additions & 2 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -982,19 +982,25 @@ clone_shared_strings <- function(wb_old, old, wb_new, new) {

}

# In table names special characters must have a leading ' in front
# @params str a variable name
escape_specials <- function(str) {
gsub("([^a-zA-Z0-9\\s])", "'\\1", str, perl = TRUE)
}

known_subtotal_funs <- function(x, total, table, row_names = FALSE) {

# unfortunately x has no row names at this point
ncol_x <- ncol(x) + row_names
nms_x <- names(x)
nms_x <- escape_specials(names(x))
if (row_names) nms_x <- c("_rowNames_", nms_x)

fml <- vector("character", ncol_x)
atr <- vector("character", ncol_x)
lbl <- rep(NA_character_, ncol_x)

if (isTRUE(total) || all(as.character(total) == "109") || all(total == "sum")) {
fml <- paste0("SUBTOTAL(109,", table, "[", names(x), "])")
fml <- paste0("SUBTOTAL(109,", table, "[", nms_x, "])")
atr <- rep("sum", ncol_x)
} else {

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,23 @@ test_that("writing total row works", {

})

test_that("escaping special characters works", {
df <- data.frame(
foo = rep("#Ref!", 5),
`1#1` = 1:5,
`@Two` = 1:5,
`A1-A3` = 1:5,
check.names = FALSE
)

wb <- wb_workbook()$add_worksheet()$add_data_table(x = df, total_row = TRUE)

exp <- c("SUBTOTAL(109,Table1[foo])", "SUBTOTAL(109,Table1[1'#1])",
"SUBTOTAL(109,Table1['@Two])", "SUBTOTAL(109,Table1[A1'-A3])")
got <- unname(unlist(wb_to_df(wb, dims = "A7:D7", col_names = FALSE, show_formula = TRUE)))
expect_equal(exp, got)
})

test_that("writing vectors direction with dims works", {

# write vectors column or rowwise
Expand Down

0 comments on commit c3e4b6b

Please sign in to comment.