Learn R Programming

openxlsx (version 1.0.3)

conditionalFormat: Add conditional formatting to cells

Description

Add conditional formatting to cells

Usage

conditionalFormat(wb, sheet, cols, rows, rule, style = NULL)

Arguments

wb
A workbook object
sheet
A name or index of a worksheet
cols
Columns to apply conditional formattiong
rows
Rows to apply conditional formattiong
rule
The condition under which to apply the formatting. See details.
style
A Style to apply to those cells that satisify the rule. A Style object returned from createStyle()

Details

Conditional format will be applied to all cells within region specified by rows X cols. valid operators are "<", "<=", ">", ">=", "==", "!=". Default style given by: createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")

See Also

createStyle

Examples

Run this code
wb <- createWorkbook()

## Add a worksheet and data
addWorksheet(wb, "Worksheet Name")
writeData(wb, 1, matrix(runif(100), ncol = 10), xy = c(1,1), colNames=FALSE)
writeData(wb, 1, matrix(sample(LETTERS, 100, rep = TRUE), ncol = 10), xy = c(1, 11), colNames=FALSE)

## Create a style
conditionalStyle <- createStyle(bgFill = "#FFFF00")

## Conditionally format cells:Rows 10 to 20 columns A to J (1:10)
conditionalFormat(wb, 1, cols = 1:10, rows = 1:10, rule = "< 0.5", style = conditionalStyle)
conditionalFormat(wb, 1, cols = 1:10, rows = 11:20, rule = '== "A"')  # default style

## Save workbook
saveWorkbook(wb, "conditionalFormatExample.xlsx", overwrite = TRUE)

Run the code above in your browser using DataLab