Skip to content

Commit 34a22d4

Browse files
authored
Merge branch 'main' into colour_color
2 parents fc4c335 + 3e9a0d5 commit 34a22d4

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
@@ -2618,7 +2618,7 @@ wb_remove_comment <- function(
26182618
#' `[params$gradient]`\cr If `FALSE` colour gradient is removed. Default `TRUE`\cr\cr
26192619
#' `[params$border]`\cr If `FALSE` the border around the database is hidden. Default `TRUE`
26202620
#' }
2621-
#' \item{duplicated}{
2621+
#' \item{duplicatedValues/uniqueValues/containsErrors}{
26222622
#' `[style]`\cr A `Style` object
26232623
#' }
26242624
#' \item{contains}{
@@ -2639,6 +2639,15 @@ wb_remove_comment <- function(
26392639
#' `[params$rank]`\cr A `numeric` vector of length `1` indicating number of lowest values. Default `5L`\cr\cr
26402640
#' `[params$percent]`\cr If `TRUE` uses percentage
26412641
#' }
2642+
#' \item{iconSet}{
2643+
#' `[params$showValue]`\cr If `FALSE` the cell value is hidden. Default `TRUE`\cr\cr
2644+
#' `[params$reverse]`\cr If `TRUE` the order is reversed. Default `FALSE`\cr\cr
2645+
#' `[params$percent]`\cr If `TRUE` uses percentage\cr\cr
2646+
#' `[params$iconSet]`\cr Uses one of the implemented icon sets. Values must match the length of the icons
2647+
#' in the set 3Arrows, 3ArrowsGray, 3Flags, 3Signs, 3Symbols, 3Symbols2, 3TrafficLights1, 3TrafficLights2,
2648+
#' 4Arrows, 4ArrowsGray, 4Rating, 4RedToBlack, 4TrafficLights, 5Arrows, 5ArrowsGray, 5Quarters, 5Rating. The
2649+
#' default is 3TrafficLights1.
2650+
#' }
26422651
#' }
26432652
#'
26442653
#' @examples
@@ -2654,8 +2663,13 @@ wb_add_conditional_formatting <- function(
26542663
rows,
26552664
rule = NULL,
26562665
style = NULL,
2657-
type = c("expression", "colorScale", "dataBar", "duplicatedValues",
2658-
"containsText", "notContainsText", "beginsWith", "endsWith",
2666+
type = c("expression", "colorScale",
2667+
"dataBar", "iconSet",
2668+
"duplicatedValues", "uniqueValues",
2669+
"containsErrors", "notContainsErrors",
2670+
"containsBlanks", "notContainsBlanks",
2671+
"containsText", "notContainsText",
2672+
"beginsWith", "endsWith",
26592673
"between", "topN", "bottomN"),
26602674
params = list(
26612675
showValue = TRUE,

R/class-workbook.R

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,8 +3220,13 @@ wbWorkbook <- R6::R6Class(
32203220
rule = NULL,
32213221
style = NULL,
32223222
# TODO add vector of possible values
3223-
type = c("expression", "colorScale", "dataBar", "duplicatedValues",
3224-
"containsText", "notContainsText", "beginsWith", "endsWith",
3223+
type = c("expression", "colorScale",
3224+
"dataBar", "iconSet",
3225+
"duplicatedValues", "uniqueValues",
3226+
"containsErrors", "notContainsErrors",
3227+
"containsBlanks", "notContainsBlanks",
3228+
"containsText", "notContainsText",
3229+
"beginsWith", "endsWith",
32253230
"between", "topN", "bottomN"),
32263231
params = list(
32273232
showValue = TRUE,
@@ -3251,7 +3256,9 @@ wbWorkbook <- R6::R6Class(
32513256
params <- validate_cf_params(params)
32523257
values <- NULL
32533258

3254-
sel <- c("expression", "duplicatedValues", "containsText", "notContainsText", "beginsWith", "endsWith", "between", "topN", "bottomN")
3259+
sel <- c("expression", "duplicatedValues", "containsText", "notContainsText", "beginsWith",
3260+
"endsWith", "between", "topN", "bottomN", "uniqueValues", "iconSet",
3261+
"containsErrors", "notContainsErrors", "containsBlanks", "notContainsBlanks")
32553262
if (is.null(style) && type %in% sel) {
32563263
smp <- random_string()
32573264
style <- create_dxfs_style(font_color = wb_colour(hex = "FF9C0006"), bgFill = wb_colour(hex = "FFFFC7CE"))
@@ -3347,6 +3354,12 @@ wbWorkbook <- R6::R6Class(
33473354
rule <- style
33483355
},
33493356

3357+
iconSet = {
3358+
# - rule is the iconSet values
3359+
msg <- "When type == 'iconSet', "
3360+
values <- rule
3361+
},
3362+
33503363
duplicatedValues = {
33513364
# type == "duplicatedValues"
33523365
# - style is a Style object
@@ -3355,6 +3368,46 @@ wbWorkbook <- R6::R6Class(
33553368
rule <- style
33563369
},
33573370

3371+
uniqueValues = {
3372+
# type == "uniqueValues"
3373+
# - style is a Style object
3374+
# - rule is ignored
3375+
3376+
rule <- style
3377+
},
3378+
3379+
containsBlanks = {
3380+
# - style is Style object
3381+
# - rule is cell to check for errors
3382+
msg <- "When type == 'containsBlanks', "
3383+
3384+
rule <- style
3385+
},
3386+
3387+
notContainsBlanks = {
3388+
# - style is Style object
3389+
# - rule is cell to check for errors
3390+
msg <- "When type == 'notContainsBlanks', "
3391+
3392+
rule <- style
3393+
},
3394+
3395+
containsErrors = {
3396+
# - style is Style object
3397+
# - rule is cell to check for errors
3398+
msg <- "When type == 'containsErrors', "
3399+
3400+
rule <- style
3401+
},
3402+
3403+
notContainsErrors = {
3404+
# - style is Style object
3405+
# - rule is cell to check for errors
3406+
msg <- "When type == 'notContainsErrors', "
3407+
3408+
rule <- style
3409+
},
3410+
33583411
containsText = {
33593412
# - style is Style object
33603413
# - rule is text to look for
@@ -6357,6 +6410,24 @@ wbWorkbook <- R6::R6Class(
63576410
## bottomN ----
63586411
bottomN = cf_bottom_n(dxfId, values),
63596412

6413+
## uniqueValues ---
6414+
uniqueValues = cf_unique_values(dxfId),
6415+
6416+
## iconSet ----
6417+
iconSet = cf_icon_set(values, params),
6418+
6419+
## containsErrors ----
6420+
containsErrors = cf_iserror(dxfId, sqref),
6421+
6422+
## notContainsErrors ----
6423+
notContainsErrors = cf_isnoerror(dxfId, sqref),
6424+
6425+
## containsBlanks ----
6426+
containsBlanks = cf_isblank(dxfId, sqref),
6427+
6428+
## notContainsBlanks ----
6429+
notContainsBlanks = cf_isnoblank(dxfId, sqref),
6430+
63606431
# do we have a match.arg() anywhere or will it just be showned in this switch()?
63616432
stop("type `", type, "` is not a valid formatting rule")
63626433
)

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)