Skip to content

Commit

Permalink
attempt to escape conditional formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jun 25, 2023
1 parent 933af56 commit 5177273
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

* Order of arguments in `wb_add_conditional_formatting()` changed, because previously overlooked `dims` argument was added. [642](https://github.com/JanMarvin/openxlsx2/pull/642)

* New argument `gradientFill` was added to `create_dxfs_style()`. [651](https://github.com/JanMarvin/openxlsx2/pull/6501
* New argument `gradientFill` was added to `create_dxfs_style()`. [651](https://github.com/JanMarvin/openxlsx2/pull/651)

* Special characters are now escaped in conditional formatting. Hence, previously manually escaped conditional formatting needs updates. [666](https://github.com/JanMarvin/openxlsx2/pull/666)


***************************************************************************
Expand Down
20 changes: 10 additions & 10 deletions R/conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ cf_create_contains_text <- function(dxfId, sqref, values) {
</cfRule>',
# cfRule
dxfId,
values,
replace_legal_chars(values),
# formula
values,
replace_legal_chars(values),
strsplit(sqref, split = ":")[[1]][1]
)

Expand All @@ -259,9 +259,9 @@ cf_create_not_contains_text <- function(dxfId, sqref, values) {
</cfRule>',
# cfRule
dxfId,
values,
replace_legal_chars(values),
# formula
values,
replace_legal_chars(values),
strsplit(sqref, split = ":")[[1]][1]
)

Expand All @@ -278,11 +278,11 @@ cf_begins_with <- function(dxfId, sqref, values) {
</cfRule>',
# cfRule
dxfId,
values,
replace_legal_chars(values),
# formula
strsplit(sqref, split = ":")[[1]][1],
values,
values
replace_legal_chars(values),
replace_legal_chars(values)
)

cf_rule
Expand All @@ -298,11 +298,11 @@ cf_ends_with <- function(dxfId, sqref, values) {
</cfRule>',
# cfRule
dxfId,
values,
replace_legal_chars(values),
# formula
strsplit(sqref, split = ":")[[1]][1],
values,
values
replace_legal_chars(values),
replace_legal_chars(values)
)

cf_rule
Expand Down
69 changes: 69 additions & 0 deletions tests/testthat/test-conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,72 @@ test_that("conditional formatting with gradientFill works", {
)

})

test_that("escaping conditional formatting works", {

wb <- wb_workbook()$
add_worksheet()$
add_data(x = "A & B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "containsText",
rule = "A & B"
)$
add_worksheet()$
add_data(x = "A == B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "containsText",
rule = "A == B"
)$
add_worksheet()$
add_data(x = "A <> B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "containsText",
rule = "A <> B"
)$
add_worksheet()$
add_data(x = "A != B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "notContainsText",
rule = "A <> B"
)

exp <- c(`A1:A10` = "<cfRule type=\"containsText\" dxfId=\"0\" priority=\"1\" operator=\"containsText\" text=\"A &amp; B\"><formula>NOT(ISERROR(SEARCH(\"A &amp; B\", A1)))</formula></cfRule>")
got <- wb$worksheets[[1]]$conditionalFormatting
expect_equal(exp, got)

exp <- c(`A1:A10` = "<cfRule type=\"containsText\" dxfId=\"1\" priority=\"1\" operator=\"containsText\" text=\"A == B\"><formula>NOT(ISERROR(SEARCH(\"A == B\", A1)))</formula></cfRule>")
got <- wb$worksheets[[2]]$conditionalFormatting
expect_equal(exp, got)

exp <- c(`A1:A10` = "<cfRule type=\"containsText\" dxfId=\"2\" priority=\"1\" operator=\"containsText\" text=\"A &lt;&gt; B\"><formula>NOT(ISERROR(SEARCH(\"A &lt;&gt; B\", A1)))</formula></cfRule>")
got <- wb$worksheets[[3]]$conditionalFormatting
expect_equal(exp, got)

## imports quietly
wb$
add_worksheet()$
add_data(x = "A <> B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "beginsWith",
rule = "A <"
)$
add_worksheet()$
add_data(x = "A <> B")$
add_conditional_formatting(
cols = 1,
rows = 1:10,
type = "endsWith",
rule = "> B"
)

})

0 comments on commit 5177273

Please sign in to comment.