Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active November 13, 2023 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-dray/3910663e0c7cbf499d65ee3a73efcb88 to your computer and use it in GitHub Desktop.
Save matt-dray/3910663e0c7cbf499d65ee3a73efcb88 to your computer and use it in GitHub Desktop.
A test of how automatic font colour is handled in {openxlsx} (it isn't) and {openxlsx2} (it is)
library(openxlsx)
font_name <- "Comic Sans MS"
sheet_name <- "font_test"
font_null <- createStyle(fontName = font_name)
font_black <- createStyle(fontName = font_name, fontColour = "black")
font_red <- createStyle(fontName = font_name, fontColour = "red")
row_seq <- 1:nrow(beaver1)
wb_openxlsx <- createWorkbook()
addWorksheet(wb_openxlsx, sheet_name)
writeDataTable(wb_openxlsx, 1, beaver1)
addStyle(wb_openxlsx, 1, font_null, cols = 2, rows = row_seq)
addStyle(wb_openxlsx, 1, font_black, cols = 3, rows = row_seq)
addStyle(wb_openxlsx, 1, font_red, cols = 4, rows = row_seq)
openXL(wb_openxlsx)
library(openxlsx2)
font_name <- "Comic Sans MS"
sheet_name <- "font_test"
wb_openxlsx2 <- wb_workbook() |>
wb_add_worksheet(sheet_name) |>
wb_add_data_table(sheet_name, beaver1) |>
wb_add_font(
sheet = sheet_name,
dims = paste0("A1:A", nrow(beaver1)),
name = font_name,
color = wb_color(auto = "1")
)
wb_open(wb_openxlsx2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment