Skip to content

Commit 3e9a0d5

Browse files
authored
New cf (#493)
* New conditional formatting * uniqueValues * iconSet * containsErrors * notContainsErrors * fix typo in vignette * cleanup * containsBlanks/notContainsBlanks * fix typo * add tests * typo found with codecov * New conditional formatting * uniqueValues * iconSet * containsErrors * notContainsErrors * fix typo in vignette * cleanup * containsBlanks/notContainsBlanks * fix typo * add tests * typo found with codecov * cleanups
1 parent 8e3b6d3 commit 3e9a0d5

File tree

8 files changed

+342
-13
lines changed

8 files changed

+342
-13
lines changed

R/class-workbook-utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ wb_check_overwrite_tables <- function(
125125

126126

127127
validate_cf_params <- function(params) {
128-
bad <- names(params) %out% c("showValue", "gradient", "border", "percent", "rank")
128+
bad <- names(params) %out% c("border", "gradient", "iconSet", "percent", "rank", "reverse", "showValue")
129129
if (any(bad)) {
130130
stop("Invalid parameters: ", toString(names(params)[bad]))
131131
}

R/class-workbook-wrappers.R

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ wb_remove_comment <- function(
25952595
#' `[params$gradient]`\cr If `FALSE` colour gradient is removed. Default `TRUE`\cr\cr
25962596
#' `[params$border]`\cr If `FALSE` the border around the database is hidden. Default `TRUE`
25972597
#' }
2598-
#' \item{duplicated}{
2598+
#' \item{duplicatedValues/uniqueValues/containsErrors}{
25992599
#' `[style]`\cr A `Style` object
26002600
#' }
26012601
#' \item{contains}{
@@ -2616,6 +2616,15 @@ wb_remove_comment <- function(
26162616
#' `[params$rank]`\cr A `numeric` vector of length `1` indicating number of lowest values. Default `5L`\cr\cr
26172617
#' `[params$percent]`\cr If `TRUE` uses percentage
26182618
#' }
2619+
#' \item{iconSet}{
2620+
#' `[params$showValue]`\cr If `FALSE` the cell value is hidden. Default `TRUE`\cr\cr
2621+
#' `[params$reverse]`\cr If `TRUE` the order is reversed. Default `FALSE`\cr\cr
2622+
#' `[params$percent]`\cr If `TRUE` uses percentage\cr\cr
2623+
#' `[params$iconSet]`\cr Uses one of the implemented icon sets. Values must match the length of the icons
2624+
#' in the set 3Arrows, 3ArrowsGray, 3Flags, 3Signs, 3Symbols, 3Symbols2, 3TrafficLights1, 3TrafficLights2,
2625+
#' 4Arrows, 4ArrowsGray, 4Rating, 4RedToBlack, 4TrafficLights, 5Arrows, 5ArrowsGray, 5Quarters, 5Rating. The
2626+
#' default is 3TrafficLights1.
2627+
#' }
26192628
#' }
26202629
#'
26212630
#' @examples
@@ -2631,8 +2640,13 @@ wb_add_conditional_formatting <- function(
26312640
rows,
26322641
rule = NULL,
26332642
style = NULL,
2634-
type = c("expression", "colorScale", "dataBar", "duplicatedValues",
2635-
"containsText", "notContainsText", "beginsWith", "endsWith",
2643+
type = c("expression", "colorScale",
2644+
"dataBar", "iconSet",
2645+
"duplicatedValues", "uniqueValues",
2646+
"containsErrors", "notContainsErrors",
2647+
"containsBlanks", "notContainsBlanks",
2648+
"containsText", "notContainsText",
2649+
"beginsWith", "endsWith",
26362650
"between", "topN", "bottomN"),
26372651
params = list(
26382652
showValue = TRUE,

R/class-workbook.R

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,8 +3212,13 @@ wbWorkbook <- R6::R6Class(
32123212
rule = NULL,
32133213
style = NULL,
32143214
# TODO add vector of possible values
3215-
type = c("expression", "colorScale", "dataBar", "duplicatedValues",
3216-
"containsText", "notContainsText", "beginsWith", "endsWith",
3215+
type = c("expression", "colorScale",
3216+
"dataBar", "iconSet",
3217+
"duplicatedValues", "uniqueValues",
3218+
"containsErrors", "notContainsErrors",
3219+
"containsBlanks", "notContainsBlanks",
3220+
"containsText", "notContainsText",
3221+
"beginsWith", "endsWith",
32173222
"between", "topN", "bottomN"),
32183223
params = list(
32193224
showValue = TRUE,
@@ -3243,7 +3248,9 @@ wbWorkbook <- R6::R6Class(
32433248
params <- validate_cf_params(params)
32443249
values <- NULL
32453250

3246-
sel <- c("expression", "duplicatedValues", "containsText", "notContainsText", "beginsWith", "endsWith", "between", "topN", "bottomN")
3251+
sel <- c("expression", "duplicatedValues", "containsText", "notContainsText", "beginsWith",
3252+
"endsWith", "between", "topN", "bottomN", "uniqueValues", "iconSet",
3253+
"containsErrors", "notContainsErrors", "containsBlanks", "notContainsBlanks")
32473254
if (is.null(style) && type %in% sel) {
32483255
smp <- random_string()
32493256
style <- create_dxfs_style(font_color = wb_colour(hex = "FF9C0006"), bgFill = wb_colour(hex = "FFFFC7CE"))
@@ -3339,6 +3346,12 @@ wbWorkbook <- R6::R6Class(
33393346
rule <- style
33403347
},
33413348

3349+
iconSet = {
3350+
# - rule is the iconSet values
3351+
msg <- "When type == 'iconSet', "
3352+
values <- rule
3353+
},
3354+
33423355
duplicatedValues = {
33433356
# type == "duplicatedValues"
33443357
# - style is a Style object
@@ -3347,6 +3360,46 @@ wbWorkbook <- R6::R6Class(
33473360
rule <- style
33483361
},
33493362

3363+
uniqueValues = {
3364+
# type == "uniqueValues"
3365+
# - style is a Style object
3366+
# - rule is ignored
3367+
3368+
rule <- style
3369+
},
3370+
3371+
containsBlanks = {
3372+
# - style is Style object
3373+
# - rule is cell to check for errors
3374+
msg <- "When type == 'containsBlanks', "
3375+
3376+
rule <- style
3377+
},
3378+
3379+
notContainsBlanks = {
3380+
# - style is Style object
3381+
# - rule is cell to check for errors
3382+
msg <- "When type == 'notContainsBlanks', "
3383+
3384+
rule <- style
3385+
},
3386+
3387+
containsErrors = {
3388+
# - style is Style object
3389+
# - rule is cell to check for errors
3390+
msg <- "When type == 'containsErrors', "
3391+
3392+
rule <- style
3393+
},
3394+
3395+
notContainsErrors = {
3396+
# - style is Style object
3397+
# - rule is cell to check for errors
3398+
msg <- "When type == 'notContainsErrors', "
3399+
3400+
rule <- style
3401+
},
3402+
33503403
containsText = {
33513404
# - style is Style object
33523405
# - rule is text to look for
@@ -6337,6 +6390,24 @@ wbWorkbook <- R6::R6Class(
63376390
## bottomN ----
63386391
bottomN = cf_bottom_n(dxfId, values),
63396392

6393+
## uniqueValues ---
6394+
uniqueValues = cf_unique_values(dxfId),
6395+
6396+
## iconSet ----
6397+
iconSet = cf_icon_set(values, params),
6398+
6399+
## containsErrors ----
6400+
containsErrors = cf_iserror(dxfId, sqref),
6401+
6402+
## notContainsErrors ----
6403+
notContainsErrors = cf_isnoerror(dxfId, sqref),
6404+
6405+
## containsBlanks ----
6406+
containsBlanks = cf_isblank(dxfId, sqref),
6407+
6408+
## notContainsBlanks ----
6409+
notContainsBlanks = cf_isnoblank(dxfId, sqref),
6410+
63406411
# do we have a match.arg() anywhere or will it just be showned in this switch()?
63416412
stop("type `", type, "` is not a valid formatting rule")
63426413
)

R/conditional_formatting.R

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,148 @@ cf_bottom_n <- function(dxfId, values) {
356356

357357
cf_rule
358358
}
359+
360+
#' @rdname cf_rules
361+
#' @keywords internal
362+
#' @noRd
363+
cf_icon_set <- function(
364+
values,
365+
params
366+
) {
367+
368+
type <- ifelse(params$percent, "percent", "num")
369+
priority <- "1"
370+
showValue <- NULL
371+
reverse <- NULL
372+
iconSet <- NULL
373+
374+
if (!is.null(params$iconSet))
375+
iconSet <- params$iconSet
376+
377+
# only if non default
378+
if (!is.null(params$showValue))
379+
if (!params$showValue) showValue <- "0"
380+
381+
if (!is.null(params$reverse))
382+
if (params$reverse) reverse <- "1"
383+
384+
# create cfRule with iconset and cfvo
385+
386+
cf_rule <- xml_node_create(
387+
"cfRule",
388+
xml_attributes = c(
389+
type = "iconSet",
390+
priority = priority
391+
)
392+
)
393+
394+
iconset <- xml_node_create(
395+
"iconSet",
396+
xml_attributes = c(
397+
iconSet = iconSet,
398+
showValue = showValue,
399+
reverse = reverse
400+
)
401+
)
402+
403+
for (i in seq_along(values)) {
404+
iconset <- xml_add_child(
405+
iconset,
406+
xml_child = c(
407+
xml_node_create(
408+
"cfvo",
409+
xml_attributes = c(
410+
type = type,
411+
val = values[i]
412+
)
413+
)
414+
)
415+
)
416+
}
417+
418+
# return
419+
xml_add_child(
420+
cf_rule,
421+
xml_child = iconset
422+
)
423+
}
424+
425+
#' @rdname cf_rules
426+
#' @keywords internal
427+
#' @noRd
428+
cf_unique_values <- function(dxfId) {
429+
cf_rule <- sprintf(
430+
'<cfRule type="uniqueValues" dxfId="%s" priority="1"/>',
431+
dxfId
432+
)
433+
434+
cf_rule
435+
}
436+
437+
#' @rdname cf_rules
438+
#' @keywords internal
439+
#' @noRd
440+
cf_iserror <- function(dxfId, sqref) {
441+
cf_rule <- sprintf(
442+
'<cfRule type="containsErrors" dxfId="%s" priority="1">
443+
<formula>ISERROR(%s)</formula>
444+
</cfRule>',
445+
# cfRule
446+
dxfId,
447+
# formula
448+
sqref
449+
)
450+
451+
cf_rule
452+
}
453+
454+
#' @rdname cf_rules
455+
#' @keywords internal
456+
#' @noRd
457+
cf_isnoerror <- function(dxfId, sqref) {
458+
cf_rule <- sprintf(
459+
'<cfRule type="notContainsErrors" dxfId="%s" priority="1">
460+
<formula>NOT(ISERROR(%s))</formula>
461+
</cfRule>',
462+
# cfRule
463+
dxfId,
464+
# formula
465+
sqref
466+
)
467+
468+
cf_rule
469+
}
470+
471+
#' @rdname cf_rules
472+
#' @keywords internal
473+
#' @noRd
474+
cf_isblank <- function(dxfId, sqref) {
475+
cf_rule <- sprintf(
476+
'<cfRule type="containsBlanks" dxfId="%s" priority="1">
477+
<formula>LEN(TRIM(%s))=0</formula>
478+
</cfRule>',
479+
# cfRule
480+
dxfId,
481+
# formula
482+
sqref
483+
)
484+
485+
cf_rule
486+
}
487+
488+
#' @rdname cf_rules
489+
#' @keywords internal
490+
#' @noRd
491+
cf_isnoblank <- function(dxfId, sqref) {
492+
cf_rule <- sprintf(
493+
'<cfRule type="notContainsBlanks" dxfId="%s" priority="1">
494+
<formula>LEN(TRIM(%s))>0</formula>
495+
</cfRule>',
496+
# cfRule
497+
dxfId,
498+
# formula
499+
sqref
500+
)
501+
502+
cf_rule
503+
}

man/wbWorkbook.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wb_add_conditional_formatting.Rd

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)