diff --git a/R/conditional_formatting.R b/R/conditional_formatting.R index 9b0fbae54..3b1daaa85 100644 --- a/R/conditional_formatting.R +++ b/R/conditional_formatting.R @@ -240,9 +240,9 @@ cf_create_contains_text <- function(dxfId, sqref, values) { ', # cfRule dxfId, - values, + replace_legal_chars(values), # formula - values, + replace_legal_chars(values), strsplit(sqref, split = ":")[[1]][1] ) @@ -259,9 +259,9 @@ cf_create_not_contains_text <- function(dxfId, sqref, values) { ', # cfRule dxfId, - values, + replace_legal_chars(values), # formula - values, + replace_legal_chars(values), strsplit(sqref, split = ":")[[1]][1] ) @@ -278,11 +278,11 @@ cf_begins_with <- function(dxfId, sqref, values) { ', # 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 @@ -298,11 +298,11 @@ cf_ends_with <- function(dxfId, sqref, values) { ', # 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 diff --git a/tests/testthat/test-conditional_formatting.R b/tests/testthat/test-conditional_formatting.R index 0667a69fd..2d29974bd 100644 --- a/tests/testthat/test-conditional_formatting.R +++ b/tests/testthat/test-conditional_formatting.R @@ -702,7 +702,7 @@ test_that("containsBlanks works", { exp <- c( "LEN(TRIM(A1:A4))=0", - "LEN(TRIM(B1:B4))>0" + "LEN(TRIM(B1:B4))>0" ) got <- as.character(wb$worksheets[[1]]$conditionalFormatting) expect_equal(exp, got) @@ -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` = "NOT(ISERROR(SEARCH(\"A & B\", A1)))") + got <- wb$worksheets[[1]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c(`A1:A10` = "NOT(ISERROR(SEARCH(\"A == B\", A1)))") + got <- wb$worksheets[[2]]$conditionalFormatting + expect_equal(exp, got) + + exp <- c(`A1:A10` = "NOT(ISERROR(SEARCH(\"A <> B\", A1)))") + 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" + ) + +})