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

Cloning styles across worksheets/workbooks #819

Open
JanMarvin opened this issue Oct 17, 2023 · 0 comments
Open

Cloning styles across worksheets/workbooks #819

JanMarvin opened this issue Oct 17, 2023 · 0 comments
Milestone

Comments

@JanMarvin
Copy link
Owner

I wanted to clone some styles across workbooks and came across two issues.

  • One was some stoi error (have seen this in a previous issue) if I was applying more styles than I had rows in my data. not sure why, was not able to reproduce this locally.
  • The style I wanted to clone was trickier to apply to a range of cells.
library(openxlsx2)

# some formatted input sheet
wb_style <- wb_workbook()$add_worksheet("tab")$
  add_fill(dims = "A1:A5", color = wb_color("yellow"))$
  add_fill(dims = "A6:A10", color = wb_color("orange"))

# the workbook where I want to apply the styling from the previous workbook
wb <- wb_workbook()$add_worksheet("dat")$add_data(x = head(mtcars, 10))

# clone the entire worksheet. maybe wb_clone_sheet_style() can use a workbook as well?
wb$clone_worksheet(old = "tab", new = "tab", from = wb_style)

# # to long styles caused "Error: stoi" issues on Windows
# styles <- wb$get_cell_style(sheet = "tab", dims = "A1:A11")
styles <- wb$get_cell_style(sheet = "tab", dims = "A1:A10")

# remove the sheet, I only need the style
wb$remove_worksheet("tab")

# # broken. the styles are all over the place
# wb$set_cell_style(sheet = "dat", dims = "A1:D10", style = styles)

# works, but looks a bit complex
wb$set_cell_style(
  sheet = "dat",
  dims = "A1:D10",
  style = unclass(t(matrix(styles, nrow = length(styles), ncol = 4)))
)

if (interactive()) wb$open()
@JanMarvin JanMarvin added this to the future milestone Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant