diff --git a/.lintr b/.lintr index 7a58a3bee..470b37015 100644 --- a/.lintr +++ b/.lintr @@ -3,6 +3,7 @@ linters: linters_with_defaults( cyclocomp_linter = NULL, # commented_code_linter = NULL, # object_name_linter = NULL, # more camel case + object_length_linter = NULL, indentation_linter = NULL, # many quotes_linter = NULL ) diff --git a/DESCRIPTION b/DESCRIPTION index 0527704fc..6e90193cd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: openxlsx2 Title: Read, Write and Edit 'xlsx' Files -Version: 1.6 +Version: 1.6.0.9000 Language: en-US Authors@R: c( person("Jordan Mark", "Barbone", email = "jmbarbone@gmail.com", role = "aut", comment = c(ORCID = "0000-0001-9788-3628")), diff --git a/NAMESPACE b/NAMESPACE index 9158cec5c..7168191a2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -116,6 +116,7 @@ export(wb_protect_worksheet) export(wb_read) export(wb_remove_col_widths) export(wb_remove_comment) +export(wb_remove_conditional_formatting) export(wb_remove_creators) export(wb_remove_filter) export(wb_remove_named_region) diff --git a/NEWS.md b/NEWS.md index e6240b7d8..004a61358 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# openxlsx2 (development version) + +## New features + +* remove conditional formatting from worksheet + + +*************************************************************************** + # openxlsx2 1.6 ## New features diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 7b5b56528..8f952c61b 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -3707,8 +3707,8 @@ wb_add_form_control <- function( #' @param sheet A name or index of a worksheet #' @param dims A cell or cell range like "A1" or "A1:B2" #' @param rule The condition under which to apply the formatting. See **Examples**. -#' @param style A style to apply to those cells that satisfy the rule. -#' Default is `font_color = "FF9C0006"` and `bg_fill = "FFFFC7CE"` +#' @param style A name of a style to apply to those cells that satisfy the rule. See [wb_add_dxfs_style()] how to create one. +#' The default style has `font_color = "FF9C0006"` and `bg_fill = "FFFFC7CE"` #' @param type The type of conditional formatting rule to apply. One of `"expression"`, `"colorScale"` or others mentioned in **Details**. #' @param params A list of additional parameters passed. See **Details** for more. #' @param ... additional arguments @@ -3807,6 +3807,26 @@ wb_add_conditional_formatting <- function( ) } +#' @rdname wb_add_conditional_formatting +#' @param first remove the first conditional formatting +#' @param last remove the last conditional formatting +#' @export +wb_remove_conditional_formatting <- function( + wb, + sheet = current_sheet(), + dims = NULL, + first = FALSE, + last = FALSE +) { + assert_workbook(wb) + wb$clone()$remove_conditional_formatting( + sheet = sheet, + dims = dims, + first = first, + last = last + ) +} + #' Apply styling from a sheet to another within a workbook #' #' This function can be used to apply styling from a cell range, and apply it diff --git a/R/class-workbook.R b/R/class-workbook.R index 6b891e507..976cbaf2b 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -4744,8 +4744,6 @@ wbWorkbook <- R6::R6Class( ## conditional formatting ---- - # TODO remove_conditional_formatting? - #' @description Add conditional formatting #' @param rule rule #' @param style style @@ -5067,6 +5065,41 @@ wbWorkbook <- R6::R6Class( invisible(self) }, + #' @description Remove conditional formatting + #' @param sheet sheet + #' @param dims dims + #' @param first first + #' @param last last + #' @return The `wbWorkbook` object + remove_conditional_formatting = function( + sheet = current_sheet(), + dims = NULL, + first = FALSE, + last = FALSE + ) { + + sheet <- private$get_sheet_index(sheet) + + if (is.null(dims) && isFALSE(first) && isFALSE(last)) { + self$worksheets[[sheet]]$conditionalFormatting <- character() + } else { + + cf <- self$worksheets[[sheet]]$conditionalFormatting + + if (!is.null(dims)) { + if (any(sel <- names(cf) %in% dims)) { + self$worksheets[[sheet]]$conditionalFormatting <- cf[!sel] + } + } else if (first) { + self$worksheets[[sheet]]$conditionalFormatting <- cf[-1] + } else if (last) { + self$worksheets[[sheet]]$conditionalFormatting <- cf[-length(cf)] + } + } + + invisible(self) + }, + ## plots and images ---- #' @description diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 5142d2cc0..6a1b2fb95 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -198,6 +198,7 @@ worksheet names.} \item \href{#method-wbWorkbook-add_thread}{\code{wbWorkbook$add_thread()}} \item \href{#method-wbWorkbook-get_thread}{\code{wbWorkbook$get_thread()}} \item \href{#method-wbWorkbook-add_conditional_formatting}{\code{wbWorkbook$add_conditional_formatting()}} +\item \href{#method-wbWorkbook-remove_conditional_formatting}{\code{wbWorkbook$remove_conditional_formatting()}} \item \href{#method-wbWorkbook-add_image}{\code{wbWorkbook$add_image()}} \item \href{#method-wbWorkbook-add_plot}{\code{wbWorkbook$add_plot()}} \item \href{#method-wbWorkbook-add_drawing}{\code{wbWorkbook$add_drawing()}} @@ -1908,6 +1909,37 @@ The \code{wbWorkbook} object } } \if{html}{\out{