Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to escape conditional formatting #666

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)

})