condformat2excelsheet: Writes the table to a worksheet of an existing Excel workbook

View source: R/render_xlsx.R

condformat2excelsheetR Documentation

Writes the table to a worksheet of an existing Excel workbook

Description

Requires the openxlsx package (install.packages("openxlsx")).

Usage

condformat2excelsheet(x, workbook, sheet_name)

Arguments

x

A condformat object, typically created with condformat()

workbook

An openxlsx Workbook object, as created with openxlsx::createWorkbook() or loaded with openxlsx::loadWorkbook()

sheet_name

The name of a worksheet already present in workbook (for instance added with openxlsx::addWorksheet()) where the table will be written

Details

Unlike condformat2excel(), this does not create the workbook or save it to disk: you pass in an openxlsx workbook (and an already-added worksheet) yourself, so you can add other sheets, or apply additional openxlsx formatting, before saving it with openxlsx::saveWorkbook().

This function applies its own styling (fill colour, bold, font colour) to every cell using stack = TRUE, so it merges with, rather than replaces, any formatting you already applied (e.g. a number format on a Date column, set either by you or automatically by openxlsx::writeData()).

openxlsx::addStyle() itself defaults to replacing, not merging, any style already present at a cell. So if you add your own openxlsx formatting after calling this function, remember to pass stack = TRUE to your own call too, or it will silently replace condformat's own styling instead of combining with it. See the example below.

See Also

condformat2excel(), which creates the workbook, writes a single sheet with this function, and saves it to disk in one call.

Examples

data(iris)
cf <- condformat(iris[1:5, ]) |>
  rule_fill_gradient(Sepal.Width)
## Not run: 
workbook <- openxlsx::createWorkbook(creator = "")
openxlsx::addWorksheet(workbook, sheetName = "iris")
condformat2excelsheet(cf, workbook, "iris")
# Combine condformat's own fill colour on Sepal.Width (column 2) with a
# percentage number format, using stack = TRUE so it doesn't replace
# condformat's own styling:
openxlsx::addStyle(
  workbook, "iris",
  style = openxlsx::createStyle(numFmt = "0%"),
  rows = 2:6, cols = 2, stack = TRUE
)
openxlsx::saveWorkbook(workbook, file = "iris.xlsx", overwrite = TRUE)

## End(Not run)

condformat documentation built on July 10, 2026, 1:07 a.m.