Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow custom table styles #594

Merged
merged 8 commits into from
Apr 27, 2023
Merged

allow custom table styles #594

merged 8 commits into from
Apr 27, 2023

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented Apr 26, 2023

For custom tablestyles (idea from CHOP-CGTInformatics/REDCapTidieR#148):

  • create a custom style in Excel
  • export the styled workbook
  • import the workbook with openxlsx2
  • copy wb$styles_mgr$styles$dxfs and wb$styles_mgr$styles$tableStyles such as below. We might create some better logic, but it is more or less a niche feature
  • Should be safe if no other dxf nodes or tableStyles node exist in wb$styles_mgr$styles
library(openxlsx2)
wb <- wb_workbook() %>%
  wb_add_worksheet()

wb$styles_mgr$styles$dxfs <- c(
  "<dxf><fill><patternFill><bgColor theme=\"7\" tint=\"0.79998168889431442\"/></patternFill></fill></dxf>", 
  "<dxf><fill><patternFill><bgColor theme=\"5\" tint=\"0.79998168889431442\"/></patternFill></fill></dxf>", 
  "<dxf><border><left style=\"slantDashDot\"><color auto=\"1\"/></left><right style=\"slantDashDot\"><color auto=\"1\"/></right><top style=\"slantDashDot\"><color auto=\"1\"/></top><bottom style=\"slantDashDot\"><color auto=\"1\"/></bottom><vertical style=\"slantDashDot\"><color auto=\"1\"/></vertical><horizontal style=\"slantDashDot\"><color auto=\"1\"/></horizontal></border></dxf>", 
  "<dxf><fill><patternFill><bgColor rgb=\"FFC00000\"/></patternFill></fill></dxf>"
)
wb$styles_mgr$dxf <- data.frame(
  typ = "dxf",
  id = seq_along(wb$styles_mgr$styles$dxfs) - 1L,
  name = wb$styles_mgr$styles$dxfs
)

wb$styles_mgr$styles$tableStyles <- "<tableStyles count=\"1\" defaultTableStyle=\"TableStyleMedium2\" defaultPivotStyle=\"PivotStyleLight16\"><tableStyle name=\"RedTableStyle\" pivot=\"0\" count=\"4\" xr9:uid=\"{91A57EDA-14C5-4643-B7E3-C78161B6BBA4}\"><tableStyleElement type=\"wholeTable\" dxfId=\"2\"/><tableStyleElement type=\"headerRow\" dxfId=\"3\"/><tableStyleElement type=\"firstRowStripe\" dxfId=\"0\"/><tableStyleElement type=\"secondColumnStripe\" dxfId=\"1\"/></tableStyle></tableStyles>"


wb %>% 
  wb_add_data_table(x = mtcars, tableStyle = "RedTableStyle") %>% 
  wb_open()

[Edit] A little less hackish solution is below:

library(openxlsx2)
wb <- wb_workbook() %>%
  wb_add_worksheet()

tabCol1 <- create_dxfs_style(bgFill = wb_color(theme = 7))
tabCol2 <- create_dxfs_style(bgFill = wb_color(theme = 5))
tabBrd1 <- create_dxfs_style(border = TRUE)
tabCol3 <- create_dxfs_style(bgFill = wb_color(hex = "FFC00000"), font_color = wb_color("white"))

wb$add_style(tabCol1)
wb$add_style(tabCol2)
wb$add_style(tabBrd1)
wb$add_style(tabCol3)


wb$styles_mgr$styles$tableStyles <-
  sprintf(
    "<tableStyles count=\"1\" defaultTableStyle=\"TableStyleMedium2\" defaultPivotStyle=\"PivotStyleLight16\">
     <tableStyle name=\"RedTableStyle\" pivot=\"0\" count=\"%s\" xr9:uid=\"{91A57EDA-14C5-4643-B7E3-C78161B6BBA4}\">
       <tableStyleElement type=\"wholeTable\" dxfId=\"%s\"/>
       <tableStyleElement type=\"headerRow\" dxfId=\"%s\"/>
       <tableStyleElement type=\"firstRowStripe\" dxfId=\"%s\"/>
       <tableStyleElement type=\"secondColumnStripe\" dxfId=\"%s\"/>
     </tableStyle>
  </tableStyles>",
    length(c(tabCol1, tabCol2, tabCol3, tabBrd1)),
    wb$styles_mgr$get_dxf_id("tabBrd1"),
    wb$styles_mgr$get_dxf_id("tabCol3"),
    wb$styles_mgr$get_dxf_id("tabCol1"),
    wb$styles_mgr$get_dxf_id("tabCol2")
  )


wb %>% 
  wb_add_data_table(x = mtcars, tableStyle = "RedTableStyle") %>% 
  wb_open()

@JanMarvin JanMarvin merged commit 3368e26 into main Apr 27, 2023
@JanMarvin JanMarvin deleted the custom_tab_styles branch April 27, 2023 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant