From 51772739b27f559c13d3cca5dcfac78e3f87a6ad Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Mon, 26 Jun 2023 00:20:02 +0200 Subject: [PATCH] attempt to escape conditional formatting --- NEWS.md | 4 +- R/conditional_formatting.R | 20 +++--- tests/testthat/test-conditional_formatting.R | 69 ++++++++++++++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/NEWS.md b/NEWS.md index b1b1b5303..379a6c682 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) *************************************************************************** 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..7f98d1001 100644 --- a/tests/testthat/test-conditional_formatting.R +++ b/tests/testthat/test-conditional_formatting.R @@ -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" + ) + +})