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{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-wbWorkbook-remove_conditional_formatting}{}}} +\subsection{Method \code{remove_conditional_formatting()}}{ +Remove conditional formatting +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{wbWorkbook$remove_conditional_formatting( + sheet = current_sheet(), + dims = NULL, + first = FALSE, + last = FALSE +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{sheet}}{sheet} + +\item{\code{dims}}{dims} + +\item{\code{first}}{first} + +\item{\code{last}}{last} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +The \code{wbWorkbook} object +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-wbWorkbook-add_image}{}}} \subsection{Method \code{add_image()}}{ diff --git a/man/wb_add_conditional_formatting.Rd b/man/wb_add_conditional_formatting.Rd index 2a4773bba..1fcce9bbd 100644 --- a/man/wb_add_conditional_formatting.Rd +++ b/man/wb_add_conditional_formatting.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/class-workbook-wrappers.R \name{wb_add_conditional_formatting} \alias{wb_add_conditional_formatting} +\alias{wb_remove_conditional_formatting} \title{Add conditional formatting to cells in a worksheet} \usage{ wb_add_conditional_formatting( @@ -18,6 +19,14 @@ wb_add_conditional_formatting( 5L), ... ) + +wb_remove_conditional_formatting( + wb, + sheet = current_sheet(), + dims = NULL, + first = FALSE, + last = FALSE +) } \arguments{ \item{wb}{A Workbook object} @@ -28,14 +37,18 @@ wb_add_conditional_formatting( \item{rule}{The condition under which to apply the formatting. See \strong{Examples}.} -\item{style}{A style to apply to those cells that satisfy the rule. -Default is \code{font_color = "FF9C0006"} and \code{bg_fill = "FFFFC7CE"}} +\item{style}{A name of a style to apply to those cells that satisfy the rule. See \code{\link[=wb_add_dxfs_style]{wb_add_dxfs_style()}} how to create one. +The default style has \code{font_color = "FF9C0006"} and \code{bg_fill = "FFFFC7CE"}} \item{type}{The type of conditional formatting rule to apply. One of \code{"expression"}, \code{"colorScale"} or others mentioned in \strong{Details}.} \item{params}{A list of additional parameters passed. See \strong{Details} for more.} \item{...}{additional arguments} + +\item{first}{remove the first conditional formatting} + +\item{last}{remove the last conditional formatting} } \description{ Add conditional formatting to cells. diff --git a/tests/testthat/test-class-workbook-wrappers.R b/tests/testthat/test-class-workbook-wrappers.R index c1a6a1298..c57571f6f 100644 --- a/tests/testthat/test-class-workbook-wrappers.R +++ b/tests/testthat/test-class-workbook-wrappers.R @@ -542,6 +542,16 @@ test_that("wb_add_conditional_formatting() is a wrapper", { ) }) +# wb_remove_conditional_formatting() ----------------------------------------- + +test_that("wb_remove_conditional_formatting() is a wrapper", { + wb <- wb_workbook()$add_worksheet() + expect_wrapper( + "remove_conditional_formatting", + wb = wb + ) +}) + # wb_set_sheet_names() ---------------------------------------------------- test_that("wb_set_sheet_names() is a wrapper", { diff --git a/tests/testthat/test-conditional_formatting.R b/tests/testthat/test-conditional_formatting.R index 7c0497b3c..927c6f486 100644 --- a/tests/testthat/test-conditional_formatting.R +++ b/tests/testthat/test-conditional_formatting.R @@ -868,3 +868,46 @@ test_that("escaping conditional formatting works", { ) }) + +test_that("remove conditional formatting works", { + wb <- wb_workbook()$ + add_worksheet()$ + add_data(x = 1:4)$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:4), rule = ">2")$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:3), rule = "<2")$ + add_worksheet()$ + add_data(x = 1:4)$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:4), rule = ">2")$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:3), rule = "<2")$ + add_worksheet()$ + add_data(x = 1:4)$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:4), rule = ">2")$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:3), rule = "<2")$ + add_worksheet()$ + add_data(x = 1:4)$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:4), rule = ">2")$ + add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:3), rule = "<2") + + wb$remove_conditional_formatting(sheet = 1) + wb$remove_conditional_formatting(sheet = 1, first = TRUE) + wb$remove_conditional_formatting(sheet = 1, last = TRUE) + wb$remove_conditional_formatting(sheet = 2, dims = wb_dims(cols = "A", rows = 1:4)) + wb$remove_conditional_formatting(sheet = 3, first = TRUE) + wb$remove_conditional_formatting(sheet = 4, last = TRUE) + + exp <- character() + got <- wb$worksheets[[1]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c(`A1:A3` = "A1<2") + got <- wb$worksheets[[2]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c(`A1:A3` = "A1<2") + got <- wb$worksheets[[3]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c(`A1:A4` = "A1>2") + got <- wb$worksheets[[4]]$conditionalFormatting + expect_equal(exp, got) +})