
Add conditional formatting to cells in a worksheet
Source:R/class-workbook-wrappers.R
wb_add_conditional_formatting.RdAdd conditional formatting to cells.
You can find more details in vignette("conditional-formatting").
Usage
wb_add_conditional_formatting(
wb,
sheet = current_sheet(),
dims = NULL,
rule = NULL,
style = NULL,
type = c("expression", "colorScale", "dataBar", "iconSet", "duplicatedValues",
"uniqueValues", "containsErrors", "notContainsErrors", "containsBlanks",
"notContainsBlanks", "containsText", "notContainsText", "beginsWith", "endsWith",
"between", "topN", "bottomN"),
params = list(showValue = TRUE, gradient = TRUE, border = TRUE, percent = FALSE, rank =
5L),
...
)
wb_remove_conditional_formatting(
wb,
sheet = current_sheet(),
dims = NULL,
first = FALSE,
last = FALSE
)Arguments
- wb
A Workbook object
- sheet
A name or index of a worksheet
- dims
A cell or cell range like "A1" or "A1:B2"
- rule
The condition under which to apply the formatting. See Examples.
- style
A name of a style to apply to those cells that satisfy the rule. See
wb_add_dxfs_style()how to create one. The default style hasfont_color = "FF9C0006"andbg_fill = "FFFFC7CE"- type
The type of conditional formatting rule to apply. One of
"expression","colorScale"or others mentioned in Details.- params
A list of additional parameters passed. See Details for more.
- ...
additional arguments
- first
remove the first conditional formatting
- last
remove the last conditional formatting
Details
openxml uses the alpha channel first then RGB, whereas the usual default is RGBA.
Conditional formatting type accept different parameters. Unless noted,
unlisted parameters are ignored.
If an expression is pointing to a cell "A1=1", this cell reference is fluid and not fixed like "$A$1=1". It will behave similar to a formula, when dims is spanning multiple columns or rows (A1, A2, A3 ... in vertical direction, A1, B1, C1 ... in horizontal direction). If dims is a non consecutive range ("A1:B2,D1:F2"), the expression is applied to each range. For the second dims range it will be evaluated again as "A1=1".
expression[style]
AStyleobject[rule]
An Excel expression (as a character). Valid operators are:<,<=,>,>=,==,!=colorScale[style]
Acharactervector of valid colors with length2or3[rule]NULLor acharactervector of valid colors of equal length tostylesdataBar[style]
Acharactervector of valid colors with length2or3[rule]
Anumericvector specifying the range of the databar colors. Must be equal length tostyle[params$showValue]
IfFALSEthe cell value is hidden. DefaultTRUE[params$gradient]
IfFALSEcolor gradient is removed. DefaultTRUE[params$border]
IfFALSEthe border around the database is hidden. DefaultTRUEduplicatedValues/uniqueValues/containsErrors[style]
AStyleobjectcontains[style]
AStyleobject[rule]
The text to look for within cellsbetween[style]
AStyleobject.[rule]
Anumericvector of length2specifying lower and upper bound (Inclusive)topN[style]
AStyleobject[params$rank]
Anumericvector of length1indicating number of highest values. Default5L[params$percent]IfTRUE, uses percentagebottomN[style]
AStyleobject[params$rank]
Anumericvector of length1indicating number of lowest values. Default5L[params$percent]
IfTRUE, uses percentageiconSet[params$showValue]
IfFALSE, the cell value is hidden. DefaultTRUE[params$reverse]
IfTRUE, the order is reversed. DefaultFALSE[params$percent]
IfTRUE, uses percentage[params$iconSet]
Uses one of the implemented icon sets. Values must match the length of the icons in the set 3Arrows, 3ArrowsGray, 3Flags, 3Signs, 3Stars, 3Symbols, 3Symbols2, 3TrafficLights1, 3TrafficLights2, 3Triangles, 4Arrows, 4ArrowsGray, 4Rating, 4RedToBlack, 4TrafficLights, 5Arrows, 5ArrowsGray, 5Boxes, 5Quarters, 5Rating. The default is 3TrafficLights1.
See also
Other worksheet content functions:
col_widths-wb,
filter-wb,
grouping-wb,
named_region-wb,
row_heights-wb,
wb_add_data(),
wb_add_data_table(),
wb_add_formula(),
wb_add_hyperlink(),
wb_add_pivot_table(),
wb_add_slicer(),
wb_add_thread(),
wb_freeze_pane(),
wb_merge_cells()
Examples
wb <- wb_workbook()
wb$add_worksheet("a")
wb$add_data(x = 1:4, col_names = FALSE)
wb$add_conditional_formatting(dims = wb_dims(cols = "A", rows = 1:4), rule = ">2")