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

Add type blanks and not blanks to conditional formatting. fixes #179 #311

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
18 changes: 18 additions & 0 deletions R/WorkbookClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,24 @@ Workbook$methods(
values[1],
values[2]
)
} else if (type == "containsBlanks") {
cfRule <-
sprintf(
'<cfRule type="containsBlanks" dxfId="%s" priority="1">
<formula>LEN(TRIM(%s))=0</formula>
</cfRule>',
dxfId,
unlist(strsplit(sqref, split = ":"))[[1]]
)
} else if (type == "notContainsBlanks") {
cfRule <-
sprintf(
'<cfRule type="notContainsBlanks" dxfId="%s" priority="1">
<formula>LEN(TRIM(%s))&gt;0</formula>
</cfRule>',
dxfId,
unlist(strsplit(sqref, split = ":"))[[1]]
)
}

worksheets[[sheet]]$conditionalFormatting <<-
Expand Down
62 changes: 59 additions & 3 deletions R/conditional_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' @param rule The condition under which to apply the formatting. See examples.
#' @param style A style to apply to those cells that satisfy the rule. Default is createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
#' @param type Either 'expression', 'colourScale', 'databar', 'duplicates', 'beginsWith',
#' 'endsWith', 'topN', 'bottomN', 'contains' or 'notContains' (case insensitive).
#' 'endsWith', 'topN', 'bottomN', 'blanks', 'notBlanks', 'contains' or 'notContains' (case insensitive).
#' @param ... See below
#' @details See Examples.
#'
Expand Down Expand Up @@ -61,6 +61,18 @@
#' \item{style is a Style object. See [createStyle()]}
#' \item{rule is a numeric vector of length 2 specifying lower and upper bound (Inclusive)}
#' }
#' If type == "blanks"
#' \itemize{
#' \item{style is a Style object. See [createStyle()]}
#' \item{rule is ignored.}
#' }
#'
#' If type == "notBlanks"
#' \itemize{
#' \item{style is a Style object. See [createStyle()]}
#' \item{rule is ignored.}
#' }

#'
#' If type == "topN"
#' \itemize{
Expand Down Expand Up @@ -104,6 +116,8 @@
#' addWorksheet(wb, "between")
#' addWorksheet(wb, "topN")
#' addWorksheet(wb, "bottomN")
#' addWorksheet(wb, "containsBlanks")
#' addWorksheet(wb, "notContainsBlanks")
#' addWorksheet(wb, "logical operators")
#'
#' negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
Expand Down Expand Up @@ -238,6 +252,18 @@
#' # Highlight bottom 20 percentage in column y
#' conditionalFormatting(wb, "bottomN", cols = 2, rows = 2:11,
#' style = negStyle, type = "topN", rank = 20, percent = TRUE)
#'
#' ## cells containing blanks
#' sample_data <- sample(c("X", NA_character_), 10, replace = TRUE)
#' writeData(wb, "containsBlanks", sample_data)
#' conditionalFormatting(wb, "containsBlanks", cols = 1, rows = 1:10,
#' type = "blanks", style = negStyle)
#'
#' ## cells not containing blanks
#' sample_data <- sample(c("X", NA_character_), 10, replace = TRUE)
#' writeData(wb, "notContainsBlanks", sample_data)
#' conditionalFormatting(wb, "notContainsBlanks", cols = 1, rows = 1:10,
#' type = "notBlanks", style = posStyle)
#'
#' ## Logical Operators
#' # You can use Excels logical Operators
Expand Down Expand Up @@ -320,9 +346,13 @@ conditionalFormatting <-
type <- "topN"
} else if (type == "bottomn") {
type <- "bottomN"
} else if (type != "expression") {
} else if (type == "blanks") {
type <- "containsBlanks"
} else if (type == "notblanks") {
type <- "notContainsBlanks"
}else if (type != "expression") {
stop(
"Invalid type argument. Type must be one of 'expression', 'colourScale', 'databar', 'duplicates', 'beginsWith', 'endsWith', 'contains' or 'notContains'"
"Invalid type argument. Type must be one of 'expression', 'colourScale', 'databar', 'duplicates', 'beginsWith', 'endsWith', 'blanks', 'notBlanks', 'contains' or 'notContains'"
)
}

Expand Down Expand Up @@ -629,6 +659,32 @@ conditionalFormatting <-
invisible(dxfId <- wb$addDXFS(style))
values <- params
rule <- style
} else if (type == "containsBlanks") {
# rule is ignored

if (is.null(style)) {
style <-
createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
}

if (!"Style" %in% class(style)) {
stop("If type == 'blanks', style must be a Style object.")
}

invisible(dxfId <- wb$addDXFS(style))
} else if (type == "notContainsBlanks") {
# rule is ignored

if (is.null(style)) {
style <-
createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
}

if (!"Style" %in% class(style)) {
stop("If type == 'notBlanks', style must be a Style object.")
}

invisible(dxfId <- wb$addDXFS(style))
}


Expand Down
27 changes: 26 additions & 1 deletion man/conditionalFormatting.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test-conditionalFormatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ test_that("topN/bottomN conditions correspond to 'top10' type", {
expect_true(object = grepl(paste('type="top10"'), wb$worksheets[[1]]$conditionalFormatting[5]))
expect_true(object = grepl(paste('type="top10"'), wb$worksheets[[1]]$conditionalFormatting[6]))
})


context("Testing 'blanks' and 'notBlanks' conditions in conditionalFormatting")
BNB_test_data <- data.frame(col1 = sample(c("X", NA_character_), 10, replace = TRUE),
col2 = sample(c("Y", NA_character_), 10, replace = TRUE))

bg_blue <- createStyle(bgFill = "skyblue")
bg_red <- createStyle(bgFill = "red")

wb <- createWorkbook()
sht <- "Blanks_NonBlanks_TEST"
addWorksheet(wb, sht)
writeData(wb, sht, BNB_test_data)
conditionalFormatting(wb, sht, cols = 1, rows = 2:11, type = "blanks", style = bg_red)
conditionalFormatting(wb, sht, cols = 2, rows = 2:11, type = "notBlanks", style = bg_blue)

test_that("Number of conditionalFormatting rules added equal to 2", {
expect_equal(object = length(wb$worksheets[[1]]$conditionalFormatting), expected = 2)
})

test_that("type='blanks' calls type='containsBlanks'", {
expect_true(object = grepl(paste('containsBlanks'), wb$worksheets[[1]]$conditionalFormatting[1]))
})

test_that("type='notBlanks' calls type='notContainsBlanks'", {
expect_true(object = grepl(paste('notContainsBlanks'), wb$worksheets[[1]]$conditionalFormatting[2]))
})