| condformat2excelsheet | R Documentation |
Requires the openxlsx package (install.packages("openxlsx")).
condformat2excelsheet(x, workbook, sheet_name)
x |
A condformat object, typically created with |
workbook |
An |
sheet_name |
The name of a worksheet already present in |
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.
condformat2excel(), which creates the workbook, writes a single
sheet with this function, and saves it to disk in one call.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.