Welcome to the styling manual for openxlsx2
. In this
manual you will learn how to use openxlsx2
to style your
worksheets. data from xlsx-files to R as well as how to export data from
R to xlsx, and how to import and modify these openxml workbooks in
R.
Below we show you two ways how to create styled tables with
openxlsx2
one using the high level functions to style
worksheet areas and one using the bare metal approach of creating the
identical table. We show both ways to create styles in
openxlsx2
to show how you could build on our functions or
create your very own functions.
The example below, with increased column width.
# add some dummy data
set.seed(123)
<- matrix(rnorm(28 * 28, mean = 44444, sd = 555), ncol = 28)
mat colnames(mat) <- make.names(seq_len(ncol(mat)))
<- wb_colour(theme = "1")
border_col <- "thin"
border_sty
# prepare workbook with data and formated first row
<- wb_workbook() %>%
wb wb_add_worksheet("test") %>%
wb_add_data("test", mat) %>%
wb_add_border("test", "A1:AB1",
top_color = border_col, top_border = border_sty,
bottom_color = border_col, bottom_border = border_sty,
left_color = border_col, left_border = border_sty,
right_color = border_col, right_border = border_sty,
inner_hcolor = border_col, inner_hgrid = border_sty
%>%
) wb_add_fill("test", "A1:AB1", color = wb_colour(hex = "FF334E6F")) %>%
wb_add_font("test", "A1:AB1", name = "Arial", bold = "1", color = wb_colour(hex = "FFFFFFFF"), size = "20") %>%
wb_add_cell_style("test", "A1:AB1", horizontal = "center", textRotation = "45")
# create various number formats
<- c(
x 0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
37, 38, 39, 40, 45, 46, 47, 48, 49
)
# apply the styles
for (i in seq_along(x)) {
<- sprintf("%s2:%s29", int2col(i), int2col(i))
cell <- wb_add_numfmt(wb, "test", cell, x[i])
wb
}
# wb$open()
# create workbook
<- wb_workbook()
wb <- wb_add_worksheet(wb, "test")
wb
# add some dummy data to the worksheet
set.seed(123)
<- matrix(rnorm(28 * 28, mean = 44444, sd = 555), ncol = 28)
mat colnames(mat) <- make.names(seq_len(ncol(mat)))
$add_data("test", mat, colNames = TRUE)
wb
# create a border style and assign it to the workbook
<- c(rgb = "FF000000")
black <- create_border(
new_border bottom = "thin", bottom_color = black,
top = "thin", top_color = black,
left = "thin", left_color = black,
right = "thin", right_color = black
)$styles_mgr$add(new_border, "new_border")
wb
# create a fill style and assign it to the workbook
<- create_fill(patternType = "solid", fgColor = wb_colour(hex = "FF334E6F"))
new_fill $styles_mgr$add(new_fill, "new_fill")
wb
# create a font style and assign it to the workbook
<- create_font(sz = "20", name = "Arial", b = "1", color = wb_colour(hex = "FFFFFFFF"))
new_font $styles_mgr$add(new_font, "new_font")
wb
# create a new cell style, that uses the fill, the font and the border style
<- create_cell_style(
new_cellxfs numFmtId = "0",
horizontal = "center",
textRotation = "45",
fillId = wb$styles_mgr$get_fill_id("new_fill"),
fontId = wb$styles_mgr$get_font_id("new_font"),
borderId = wb$styles_mgr$get_border_id("new_border")
)# assign this style to the workbook
$styles_mgr$add(new_cellxfs, "new_styles")
wb
# assign the new cell style to the header row of our data set
<- sprintf("A1:%s1", int2col(nrow(mat)))
cell wb_set_cell_style(wb, "test", cell, wb$styles_mgr$get_xf_id("new_styles"))
## style the cells with some builtin format codes (no new numFmt entry is needed)
# add builtin style ids
<- c(
x "0", "1", "2", "3", "4", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "37", "38", "39", "40", "45", "46",
"47", "48", "49"
)
# create styles
<- create_cell_style(numFmtId = x, horizontal = "center")
new_cellxfs
# assign the styles to the workbook
for (i in seq_along(x)) {
$styles_mgr$add(new_cellxfs[i], paste0("new_style", i))
wb
}
# new styles are 1:28
<- wb$styles_mgr$get_xf()
new_styles for (i in as.integer(new_styles$id[new_styles$name %in% paste0("new_style", seq_along(x))])) {
<- sprintf("%s2:%s29", int2col(i), int2col(i))
cell wb_set_cell_style(wb, "test", cell, i)
}
# assign a custom tabColor
$worksheets[[1]]$sheetPr <- xml_node_create(
wb"sheetPr",
xml_children = xml_node_create(
"tabColor",
xml_attributes = wb_colour(hex = "FF00FF00")
)
)
# # look at the beauty you've created
# wb_open(wb)
Per default openxlsx2
will pick up number formats for
selected R classes.
## Create Workbook object and add worksheets
<- wb_workbook()
wb $add_worksheet("S1")
wb$add_worksheet("S2")
wb
<- data.frame(
df "Date" = Sys.Date() - 0:19,
"T" = TRUE, "F" = FALSE,
"Time" = Sys.time() - 0:19 * 60 * 60,
"Cash" = paste("$", 1:20), "Cash2" = 31:50,
"hLink" = "https://CRAN.R-project.org/",
"Percentage" = seq(0, 1, length.out = 20),
"TinyNumbers" = runif(20) / 1E9, stringsAsFactors = FALSE
)
## openxlsx will apply default Excel styling for these classes
class(df$Cash) <- c(class(df$Cash), "currency")
class(df$Cash2) <- c(class(df$Cash2), "accounting")
class(df$hLink) <- "hyperlink"
class(df$Percentage) <- c(class(df$Percentage), "percentage")
class(df$TinyNumbers) <- c(class(df$TinyNumbers), "scientific")
$add_data("S1", x = df, startRow = 4, rowNames = FALSE)
wb$add_data_table("S2", x = df, startRow = 4, rowNames = FALSE) wb
In addition, you can set the style to be picked up using
openxlsx2
options.
<- wb_workbook()
wb <- wb_add_worksheet(wb, "test")
wb
options("openxlsx2.dateFormat" = "yyyy")
options("openxlsx2.datetimeFormat" = "yyyy-mm-dd")
options("openxlsx2.numFmt" = "€ #.0")
<- data.frame(
df "Date" = Sys.Date() - 0:19,
"T" = TRUE, "F" = FALSE,
"Time" = Sys.time() - 0:19 * 60 * 60,
"Cash" = paste("$", 1:20), "Cash2" = 31:50,
"hLink" = "https://CRAN.R-project.org/",
"Percentage" = seq(0, 1, length.out = 20),
"TinyNumbers" = runif(20) / 1E9, stringsAsFactors = FALSE,
"numeric" = 1
)
## openxlsx will apply default Excel styling for these classes
class(df$Cash) <- c(class(df$Cash), "currency")
class(df$Cash2) <- c(class(df$Cash2), "accounting")
class(df$hLink) <- "hyperlink"
class(df$Percentage) <- c(class(df$Percentage), "percentage")
class(df$TinyNumbers) <- c(class(df$TinyNumbers), "scientific")
$add_data("test", df) wb
<- "mtcars"
sheet <- wb_workbook()
wb <- wb_add_worksheet(wb, sheet)
wb $add_data(sheet, mtcars, rowNames = TRUE)
wb
<- 1:12
cols <- wb_set_col_widths(wb, sheet, cols = cols, widths = "auto")
wb
wb#> A Workbook object.
#>
#> Worksheets:
#> Sheets: mtcars
#> Write order: 1
<- wb_workbook()
wb # full inner grid
$add_worksheet("S1", gridLines = FALSE)$add_data(x = mtcars)
wb$add_border(
wbdims = "A2:K33",
inner_hgrid = "thin", inner_hcolor = c(rgb = "FF808080"),
inner_vgrid = "thin", inner_vcolor = c(rgb = "FF808080")
)# only horizontal grid
$add_worksheet("S2", gridLines = FALSE)$add_data(x = mtcars)
wb$add_border(dims = "A2:K33", inner_hgrid = "thin", inner_hcolor = c(rgb = "FF808080"))
wb# only vertical grid
$add_worksheet("S3", gridLines = FALSE)$add_data(x = mtcars)
wb$add_border(dims = "A2:K33", inner_vgrid = "thin", inner_vcolor = c(rgb = "FF808080"))
wb# no inner grid
$add_worksheet("S4", gridLines = FALSE)$add_data(x = mtcars)
wb$add_border("S4", dims = "A2:K33") wb
Below we show you two ways how to create styled tables with
openxlsx2
one using the high level functions to style
worksheet areas and one using the bare metal approach of creating the
identical table.
# add some dummy data to the worksheet
<- matrix(1:4, ncol = 2, nrow = 2)
mat colnames(mat) <- make.names(seq_len(ncol(mat)))
<- wb_workbook() %>%
wb wb_add_worksheet("test") %>%
wb_add_data(x = mat, colNames = TRUE, startCol = 2, startRow = 2) %>%
# center first row
wb_add_cell_style(dims = "B2:C2", horizontal = "center") %>%
# add border for first row
wb_add_border(
dims = "B2:C2",
bottom_color = c(theme = "1"), bottom_border = "thin",
top_color = c(theme = "1"), top_border = "double",
left_border = "", right_border = ""
%>%
) # add border for last row
wb_add_border(
dims = "B4:C4",
bottom_color = c(theme = "1"), bottom_border = "double",
top_border = "", left_border = "", right_border = ""
)
# add some dummy data to the worksheet
<- matrix(1:4, ncol = 2, nrow = 2)
mat colnames(mat) <- make.names(seq_len(ncol(mat)))
<- wb_workbook()
wb <- wb_add_worksheet(wb, "test")
wb
$add_data("test", mat, colNames = TRUE, startCol = 2, startRow = 2)
wb
# create a border style and assign it to the workbook
<- c(rgb = "FF000000")
black <- create_border(
top_border top = "double", top_color = black,
bottom = "thin", bottom_color = black
)
<- create_border(bottom = "double", bottom_color = black)
bottom_border
$styles_mgr$add(top_border, "top_border")
wb$styles_mgr$add(bottom_border, "bottom_border")
wb
# create a new cell style, that uses the fill, the font and the border style
<- create_cell_style(
top_cellxfs numFmtId = "0",
horizontal = "center",
borderId = wb$styles_mgr$get_border_id("top_border")
)<- create_cell_style(
bottom_cellxfs numFmtId = "0",
borderId = wb$styles_mgr$get_border_id("bottom_border")
)
# assign this style to the workbook
$styles_mgr$add(top_cellxfs, "top_styles")
wb$styles_mgr$add(bottom_cellxfs, "bottom_styles")
wb
# assign the new cell style to the header row of our data set
<- "B2:C2"
cell wb_set_cell_style(wb, "test", cell, wb$styles_mgr$get_xf_id("top_styles"))
<- "B4:C4"
cell wb_set_cell_style(wb, "test", cell, wb$styles_mgr$get_xf_id("bottom_styles"))
The loop below will apply the tint attribute to the fill color
Tint variations of the theme colors.
<- wb_workbook() %>% wb_add_worksheet("S1")
wb
<- seq(-0.9, 0.9, by = 0.1)
tints
for (i in 0:9) {
for (tnt in tints) {
<- paste0(int2col(i + 1), which(tints %in% tnt))
col
if (tnt == 0) {
<- wb %>% wb_add_fill(dims = col, color = wb_colour(theme = as.character(i)))
wb else {
} <- wb %>% wb_add_fill(dims = col, color = wb_colour(theme = as.character(i), tint = tnt))
wb
}
} }
It is possible to copy the styles of several cells at once. In the
following example, the styles of some cells from a formatted workbook
are applied to a previously empty cell range. Be careful though,
wb_get_cell_style()
returns only some styles, so you have
to make sure that the copy-from and copy-to dimensions match in a
meaningful way.
<- wb_load(system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")) %>%
wb wb_set_cell_style(1, "A30:G35", wb_get_cell_style(., 1, "A10:G15"))
# wb_open(wb)