Skip to content

Commit

Permalink
add wb_remove_conditional_formatting(). part of #1010 (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed May 6, 2024
1 parent 70058cf commit dba5b7d
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 7 deletions.
1 change: 1 addition & 0 deletions .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# openxlsx2 (development version)

## New features

* remove conditional formatting from worksheet


***************************************************************************

# openxlsx2 1.6

## New features
Expand Down
24 changes: 22 additions & 2 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -4744,8 +4744,6 @@ wbWorkbook <- R6::R6Class(

## conditional formatting ----

# TODO remove_conditional_formatting?

#' @description Add conditional formatting
#' @param rule rule
#' @param style style
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions man/wbWorkbook.Rd

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

17 changes: 15 additions & 2 deletions man/wb_add_conditional_formatting.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -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` = "<cfRule type=\"expression\" dxfId=\"3\" priority=\"1\"><formula>A1&lt;2</formula></cfRule>")
got <- wb$worksheets[[2]]$conditionalFormatting
expect_equal(exp, got)

exp <- c(`A1:A3` = "<cfRule type=\"expression\" dxfId=\"5\" priority=\"1\"><formula>A1&lt;2</formula></cfRule>")
got <- wb$worksheets[[3]]$conditionalFormatting
expect_equal(exp, got)

exp <- c(`A1:A4` = "<cfRule type=\"expression\" dxfId=\"6\" priority=\"2\"><formula>A1&gt;2</formula></cfRule>")
got <- wb$worksheets[[4]]$conditionalFormatting
expect_equal(exp, got)
})

0 comments on commit dba5b7d

Please sign in to comment.