Skip to content

Commit

Permalink
[slicer] allow setting hide items with no data (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin authored Oct 29, 2024
1 parent 8e89506 commit 1a7e421
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# openxlsx2 (development version)

## New features

* Add `hide_no_data_items` option in `wb_add_slicer()`. [1169](https://github.com/JanMarvin/openxlsx2/pull/1169)

## 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)
Expand Down
1 change: 1 addition & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ wb_add_pivot_table <- function(
#' `c(agegp = 'x > "25-34"')` for the `esoph` dataset.
#' * locked_position
#' * start_item
#' * hide_no_data_items
#'
#' Possible `params` arguments for timelines are listed below.
#' * beg_date/end_date: dates when the timeline should begin or end
Expand Down
18 changes: 15 additions & 3 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1758,8 +1758,8 @@ wbWorkbook <- R6::R6Class(
} else {
arguments <- c(
"caption", "choose", "column_count", "cross_filter", "edit_as",
"level", "locked_position", "row_height", "show_caption",
"show_missing", "sort_order", "start_item", "style"
"hide_no_data_items", "level", "locked_position", "row_height",
"show_caption", "show_missing", "sort_order", "start_item", "style"
)
params <- standardize_case_names(params, arguments = arguments, return = TRUE)
}
Expand Down Expand Up @@ -1819,6 +1819,16 @@ wbWorkbook <- R6::R6Class(
xml_children = get_items(x, which(names(x) == slicer), NULL, slicer = TRUE, choose = choo, has_default = TRUE)
)


hide_items_with_no_data <- ""
if (isTRUE(params$hide_no_data_items)) {
hide_items_with_no_data <- '<extLst>
<x:ext xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" uri="{470722E0-AACD-4C17-9CDC-17EF765DBC7E}">
<x15:slicerCacheHideItemsWithNoData/>
</x:ext>
</extLst>'
}

slicer_cache <- read_xml(sprintf(
'<slicerCacheDefinition xmlns="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x xr10" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:xr10="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10" name="Slicer_%s" xr10:uid="{72B411E0-23B7-7444-B533-EAC1856BE56A}" sourceName="%s">
<pivotTables>
Expand All @@ -1827,12 +1837,14 @@ wbWorkbook <- R6::R6Class(
<data>
%s
</data>
%s
</slicerCacheDefinition>',
uni_name,
slicer,
sheet,
pivot_table,
tab_xml
tab_xml,
hide_items_with_no_data
), pointer = FALSE)

# we need the slicer cache
Expand Down
1 change: 1 addition & 0 deletions man/wb_add_slicer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,32 @@ test_that("removing timelines works", {

})

test_that("slicer extension 'hide_no_data_items' works", {

dat <- data.frame(
var = c("x", "y", "y", "z", "z", "x"),
speed = c(4, 4, 7, 7, 8, 9),
dis = c(2, 10, 4, 22, 16, 10),
slicervar1 = c("option1", "option1", "option1", "option2", "option2", "option2"),
slicervar2 = c("choice1", "choice1", "choice2", "choice3", "choice3", "choice1")
)

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

df <- wb_data(wb)

wb$
add_pivot_table(df, sheet = "pivot", dims = "A3", slicer = c("slicervar1", "slicervar2"), rows = c("var"), data = "speed", params = list(name = "pivot_1"))$
add_slicer(x = df, sheet = "pivot", dims = "D3:E9", slicer = "slicervar1", pivot_table = "pivot_1", param = list(hide_no_data_items = TRUE))$
add_slicer(x = df, sheet = "pivot", dims = "F3:G9", slicer = "slicervar2", pivot_table = "pivot_1")

expect_true(grepl("x15:slicerCacheHideItemsWithNoData", wb$slicerCaches[[1]]))
expect_true(!grepl("x15:slicerCacheHideItemsWithNoData", wb$slicerCaches[[2]]))

})

test_that("writing na.strings = NULL works", {

# write na.strings = na_strings()
Expand Down

0 comments on commit 1a7e421

Please sign in to comment.