diff --git a/NAMESPACE b/NAMESPACE index 961522ded..daaf73eec 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -122,6 +122,7 @@ export(wb_remove_creators) export(wb_remove_filter) export(wb_remove_named_region) export(wb_remove_row_heights) +export(wb_remove_slicer) export(wb_remove_tables) export(wb_remove_worksheet) export(wb_save) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index e62f28d37..777e7f2d9 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -508,6 +508,8 @@ wb_add_pivot_table <- function( #' * level: the granularity of the slicer (for timeline 0 = year, 1 = quarter, 2 = month) #' * show_caption: logical if caption should be shown or not #' +#' Removing slicers works on the spreadsheet level. Therefore all slicers are removed from a worksheet. +#' #' @param wb A Workbook object containing a worksheet. #' @param x A `data.frame` that inherits the [`wb_data`][wb_data()] class. #' @param sheet A worksheet @@ -597,6 +599,18 @@ wb_add_slicer <- function( } +#' @rdname wb_add_slicer +#' @export +wb_remove_slicer <- function( + wb, + sheet = current_sheet() +) { + assert_workbook(wb) + wb$clone()$remove_slicer( + sheet = sheet + ) +} + #' @rdname wb_add_slicer #' @export wb_add_timeline <- function( diff --git a/R/class-workbook.R b/R/class-workbook.R index 8dba29ae0..da809d029 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -1960,6 +1960,48 @@ wbWorkbook <- R6::R6Class( invisible(self) }, + #' @description add pivot table + #' @return The `wbWorkbook` object + remove_slicer = function(sheet = current_sheet()) { + sheet <- private$get_sheet_index(sheet) + + # get indices + slicer_id <- self$worksheets[[sheet]]$relships$slicer + + # skip if nothing to do + if (identical(slicer_id, integer())) return(invisible(self)) + + cache_names <- unname(sapply(xml_attr(self$slicers[slicer_id], "slicers", "slicer"), "[", "cache")) + slicer_names <- unname(sapply(xml_attr(self$slicerCaches, "slicerCacheDefinition"), "[", "name")) + slicer_cache_id <- which(cache_names %in% slicer_names) + + # strings to grep + slicer_xml <- sprintf("slicers/slicer%s.xml", slicer_id) + caches_xml <- sprintf("slicerCaches/slicerCache%s.xml", slicer_cache_id) + + # empty slicer + self$slicers[slicer_id] <- "" + # empty slicerCache + self$slicerCaches[slicer_cache_id] <- "" + + # remove slicer cache relship + self$worksheets[[sheet]]$relships$slicer <- integer() + # remove worksheet relationship + self$worksheets_rels[[sheet]] <- self$worksheets_rels[[sheet]][!grepl(slicer_xml, self$worksheets_rels[[sheet]])] + # remove "x14:slicerList" + is_ext_x14 <- grepl("xmlns:x14", self$worksheets[[sheet]]$extLst) + extLst <- xml_rm_child(self$worksheets[[sheet]]$extLst[is_ext_x14], xml_child = "x14:slicerList") + self$worksheets[[sheet]]$extLst[is_ext_x14] <- extLst + + # clear workbook.xml.rels + self$workbook.xml.rels <- self$workbook.xml.rels[!grepl(paste0(caches_xml, collapse = "|"), self$workbook.xml.rels)] + + # clear Content_Types + self$Content_Types <- self$Content_Types[!grepl(paste0(c(slicer_xml, caches_xml), collapse = "|"), self$Content_Types)] + + invisible(self) + }, + #' @description add pivot table #' @param x a wb_data object #' @param dims the worksheet cell where the pivot table is placed @@ -2662,15 +2704,16 @@ wbWorkbook <- R6::R6Class( slicersDir <- dir_create(tmpDir, "xl", "slicers") slicerCachesDir <- dir_create(tmpDir, "xl", "slicerCaches") - slicer <- self$slicers[self$slicers != ""] - for (i in seq_along(slicer)) { + slicer_id <- which(self$slicers != "") + for (i in slicer_id) { write_file( - body = slicer[i], + body = self$slicers[i], fl = file.path(slicersDir, sprintf("slicer%s.xml", i)) ) } - for (i in seq_along(self$slicerCaches)) { + caches_id <- which(self$slicerCaches != "") + for (i in caches_id) { write_file( body = self$slicerCaches[[i]], fl = file.path(slicerCachesDir, sprintf("slicerCache%s.xml", i)) diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 1797ced2d..27e5e1eba 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -164,6 +164,7 @@ worksheet names.} \item \href{#method-wbWorkbook-add_data_table}{\code{wbWorkbook$add_data_table()}} \item \href{#method-wbWorkbook-add_pivot_table}{\code{wbWorkbook$add_pivot_table()}} \item \href{#method-wbWorkbook-add_slicer}{\code{wbWorkbook$add_slicer()}} +\item \href{#method-wbWorkbook-remove_slicer}{\code{wbWorkbook$remove_slicer()}} \item \href{#method-wbWorkbook-add_timeline}{\code{wbWorkbook$add_timeline()}} \item \href{#method-wbWorkbook-add_formula}{\code{wbWorkbook$add_formula()}} \item \href{#method-wbWorkbook-add_style}{\code{wbWorkbook$add_style()}} @@ -748,6 +749,26 @@ The \code{wbWorkbook} object } } \if{html}{\out{