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

[wb_add_data] unify hyperlink and numeric styling #365

Merged
merged 18 commits into from
Oct 13, 2022

Conversation

JanMarvin
Copy link
Owner

@JanMarvin JanMarvin commented Oct 8, 2022

Second attempt to clean up style application. This unifies our attempts to apply styles when writing data. If we write to a workbook we can

  • apply our styles applyCellStyles = TRUE on top of what other style we have found (e.g. if a cell uses a font, border or fill: we only add our number format to the existing style).
  • use the original style or no style at all applyCellStyles = FALSE
  • use only our styles and remove the previous style removeCellStyle = TRUE
library(openxlsx2)

wb <- wb_workbook()$
  add_worksheet()$
  add_fill(dims = "B2:G8", color = wb_colour("yellow"))$
  add_data(dims = "C3", x = Sys.Date())$
  add_data(dims = "E5", x = Sys.Date(), removeCellStyle = TRUE)$
  add_data(dims = "A1", x = Sys.Date())

wb$open()
  • new tests
  • test with third party workbooks

allow dims to be "A1:A2;C1:C2"
should be a C style index starting at 0
If FALSE no cellStyle is applied and the original cell style remains (if any was found).
This was linked to issues Oct 8, 2022
@JanMarvin JanMarvin added enhancement 😀 New feature or request styles 🎨 labels Oct 8, 2022
@JanMarvin JanMarvin changed the title Add data and add style [wb_add_data] unify hyperlink and numeric styling Oct 8, 2022
@JanMarvin
Copy link
Owner Author

the style creation is a bunch of duplicated code, but I've been to long at the PC already

@JanMarvin JanMarvin added the help wanted 🙏 Extra attention is needed label Oct 11, 2022
@JanMarvin
Copy link
Owner Author

This could need some testing with styled files and newly created files

@JanMarvin
Copy link
Owner Author

Looks like it is working. The code below adds an ugly table into an existing worksheet. It goes back and forth to add background colors, number formats, borders, font and cell styles.

wb <- wb_load(file = system.file("extdata", "loadExample.xlsx", package = "openxlsx2"))$
# add title
  add_data("testing", dims = "C10", x = "MTCARS Title")$
  add_font(dims = "C10", bold = "1", size = "16")$
  # add data
  add_data(x = head(mtcars), startCol = 3, startRow = 11, removeCellStyle = TRUE)$
  add_fill(dims = "C11:M11", color = wb_colour("turquoise"))$
  add_font(dims = "C11:M11", color = wb_colour("white"))$
  add_border(dims = "C17:M17", 
             bottom_color = wb_colour(hex = "FF000000"),
             bottom_border = "thin",
             left_border = "",
             right_border = "",
             top_border = "")$
  add_numfmt(dims = "J12:M17", numfmt = "0.%")$
  add_fill(dims = "C12:M17", color = wb_colour("white"))$
  add_border(dims = "C12:M17", 
             bottom_color = wb_colour(hex = "FF000000"),
             bottom_border = "thin",
             left_border = "thin",
             right_border = "thin",
             top_border = "",
             inner_vgrid = "thin")$
  add_border(dims = "C11:M11", 
             bottom_color = wb_colour(hex = "FF000000"),
             bottom_border = "thick",
             left_border = "thick",
             right_border = "thick",
             top_border = "thick",
             inner_vgrid = "")$
  add_fill(dims = "C12:M17", color = wb_colour("lightgray"), every_nth_row = 2)
  
wb$open()

Screenshot 2022-10-11 at 23 42 21

@JanMarvin JanMarvin merged commit 1e43bad into main Oct 13, 2022
@JanMarvin JanMarvin deleted the add_data_and_add_style branch October 13, 2022 07:08
This was referenced Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 😀 New feature or request help wanted 🙏 Extra attention is needed styles 🎨
Projects
None yet
Development

Successfully merging this pull request may close these issues.

merge cell styles in add_data only one hyperlink is colored
1 participant