Skip to content

Commit

Permalink
[covr] increase write coverate and fix with_filter not working with m…
Browse files Browse the repository at this point in the history
…ore than one sheet
  • Loading branch information
JanMarvin committed Jan 27, 2024
1 parent 0cf2d53 commit f794913
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
14 changes: 10 additions & 4 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ write_data_table <- function(
if (!data_table) {

## write autoFilter, can only have a single filter per worksheet
if (withFilter) {
if (withFilter) { # TODO: replace ref calculation with wb_dims()
coords <- data.frame("x" = c(startRow, startRow + nRow + colNames - 1L), "y" = c(startCol, startCol + nCol - 1L))
ref <- stri_join(get_cell_refs(coords), collapse = ":")

Expand All @@ -851,11 +851,17 @@ write_data_table <- function(

dn <- sprintf('<definedName name="_xlnm._FilterDatabase" localSheetId="%s" hidden="1">%s</definedName>', sheet - 1L, dfn)

if (length(wb$workbook$definedNames) > 0) {
ind <- grepl('name="_xlnm._FilterDatabase"', wb$workbook$definedNames)
if (length(ind) > 0) {
if (!is.null(wbdn <- wb$get_named_regions())) {

ind <- wbdn$name == "_xlnm._FilterDatabase" & wbdn$localSheetId == sheet - 1L
if (any(ind)) {
wb$workbook$definedNames[ind] <- dn
} else {
wb$workbook$definedNames <- c(
wb$workbook$definedNames, dn
)
}

} else {
wb$workbook$definedNames <- dn
}
Expand Down
49 changes: 49 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,52 @@ test_that("dimension limits work", {
)

})

test_that("numfmt option works", {

op <- options("openxlsx2.numFmt" = "€ #.0")
on.exit(options(op), add = TRUE)

wb <- wb_workbook()$add_worksheet()$add_data(x = 1:10)

exp <- "<numFmt numFmtId=\"165\" formatCode=\"€ #.0\"/>"
got <- wb$styles_mgr$styles$numFmts
expect_equal(exp, got)

})

test_that("comma option works", {

op <- options("openxlsx2.commaFormat" = "#.0")
on.exit(options(op), add = TRUE)

dat <- data.frame(x = 1:10 + rnorm(1:10))
class(dat$x) <- c("comma", class(dat$x))

wb <- wb_workbook()$add_worksheet()$add_data(x = dat)

exp <- "<numFmt numFmtId=\"165\" formatCode=\"#.0\"/>"
got <- wb$styles_mgr$styles$numFmts
expect_equal(exp, got)

})

test_that("filter works with wb_add_data()", {

wb <- wb_workbook()$
add_worksheet()$add_data(x = mtcars, with_filter = TRUE)$
add_worksheet()$add_data(x = mtcars, with_filter = TRUE)$
add_data(x = cars, with_filter = TRUE)

exp <- "<autoFilter ref=\"A1:K33\"/>"
got <- wb$worksheets[[1]]$autoFilter
expect_equal(exp, got)

exp <- c(
"<definedName name=\"_xlnm._FilterDatabase\" localSheetId=\"0\" hidden=\"1\">'Sheet 1'!$A$1:$K$33</definedName>",
"<definedName name=\"_xlnm._FilterDatabase\" localSheetId=\"1\" hidden=\"1\">'Sheet 2'!$A$1:$B$51</definedName>"
)
got <- wb$workbook$definedNames
expect_equal(exp, got)

})

0 comments on commit f794913

Please sign in to comment.