Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

addStyle

Add a style to a set of cells


Description

Function adds a style to a specified set of cells.

Usage

addStyle(wb, sheet, style, rows, cols, gridExpand = FALSE, stack = FALSE)

Arguments

wb

A Workbook object containing a worksheet.

sheet

A worksheet to apply the style to.

style

A style object returned from createStyle()

rows

Rows to apply style to.

cols

columns to apply style to.

gridExpand

If TRUE, style will be applied to all combinations of rows and cols.

stack

If TRUE the new style is merged with any existing cell styles. If FALSE, any existing style is replaced by the new style.

Author(s)

Alexander Walker

See Also

expand.grid

Examples

## See package vignette for more examples.

## Create a new workbook
wb <- createWorkbook("My name here")

## Add a worksheets
addWorksheet(wb, "Expenditure", gridLines = FALSE)

## write data to worksheet 1
writeData(wb, sheet = 1, USPersonalExpenditure, rowNames = TRUE)

## create and add a style to the column headers
headerStyle <- createStyle(
  fontSize = 14, fontColour = "#FFFFFF", halign = "center",
  fgFill = "#4F81BD", border = "TopBottom", borderColour = "#4F81BD"
)

addStyle(wb, sheet = 1, headerStyle, rows = 1, cols = 1:6, gridExpand = TRUE)

## style for body
bodyStyle <- createStyle(border = "TopBottom", borderColour = "#4F81BD")
addStyle(wb, sheet = 1, bodyStyle, rows = 2:6, cols = 1:6, gridExpand = TRUE)
setColWidths(wb, 1, cols = 1, widths = 21) ## set column width for row names column
## Not run: 
saveWorkbook(wb, "addStyleExample.xlsx", overwrite = TRUE)

## End(Not run)

openxlsx

Read, Write and Edit xlsx Files

v4.2.3
MIT + file LICENSE
Authors
Philipp Schauberger [aut, cre], Alexander Walker [aut], Luca Braglia [ctb], Joshua Sturm [ctb]
Initial release
2020-10-26

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.