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 875f5d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
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
71 changes: 70 additions & 1 deletion tests/testthat/test-conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ test_that("containsBlanks works", {

exp <- c(
"<cfRule type=\"containsBlanks\" dxfId=\"0\" priority=\"2\"><formula>LEN(TRIM(A1:A4))=0</formula></cfRule>",
"<cfRule type=\"notContainsBlanks\" dxfId=\"1\" priority=\"1\"><formula>LEN(TRIM(B1:B4))>0</formula></cfRule>"
"<cfRule type=\"notContainsBlanks\" dxfId=\"1\" priority=\"1\"><formula>LEN(TRIM(B1:B4))&gt;0</formula></cfRule>"
)
got <- as.character(wb$worksheets[[1]]$conditionalFormatting)
expect_equal(exp, got)
Expand Down 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 875f5d3

Please sign in to comment.