-
Notifications
You must be signed in to change notification settings - Fork 11
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
[clone_worksheet] copy worksheet across workbooks #622
Conversation
I think this could be very useful! Whenever you come back to this, let me know, (and if I can help) Maybe a syntax like this could be cool:? wb1 <- wb_load("file1.xlsx")
wb2 <- wb_load("file2.xlsx")
# assuming wb2 has "Sheet 1"
wb1$add_worksheet(sheet = "Sheet 1", from_wb = wb2) |
It's a long standing feature request, but unfortunately also quite a nuisance and nothing I really require. (I once handcrafted a solution for myself, but obviously that's nothing to show to the user.) |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Guess you could have a look at what works and what doesn't @olivroy . I might have fixed a few bugs in the original Unless there are major malfunctions, we might have something working, at least for data and maybe native charts. Obviously there are still lots of things to check and we might have to add a still experimental note to the man page. |
That is fantastic, I will try it out. |
0457553
to
5a721ba
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
* clone_worksheet_to()
…mething else would replace all "chart" entries in the chart.xml file
library(openxlsx2)
# we will clone this styled chart into another workbook
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
wb_in <- wb_load(fl)
# create a second workbook
wb <- wb_workbook()$
add_worksheet("NOT_SUM")$
add_data(x = head(iris))$
add_fill(dims = "A1:B2", color = wb_color("yellow"))$
add_border(dims = "B2:C3")
# clone styles and shared strings
wb$clone_worksheet(old = "SUM", new = "SUM", from = wb_in)
wb$clone_worksheet(old = "SUM", new = "SUM_clone")
wb$clone_worksheet(old = "SUM", new = "SUM2", from = wb_in)
wb_in <- wb_workbook()$add_worksheet("tab")$add_data_table(x = mtcars)
# clone table
wb$clone_worksheet(old = "tab", new = "tab", from = wb_in)
# clone it twice
wb$clone_worksheet(old = "tab", new = "tab", from = wb_in)
#> Warning: Attempted to add a worksheet that is invalid or already exists.
#> Fixing: a sheet with name "tab" already exists. Creating a unique sheetname"
library(mschart)
## Add mschart to worksheet (adds data and chart)
scatter <- ms_scatterchart(data = iris, x = "Sepal.Length", y = "Sepal.Width", group = "Species")
scatter <- chart_settings(scatter, scatterstyle = "marker")
wb_ms <- wb_workbook() %>%
wb_add_worksheet("chart") %>%
wb_add_mschart(dims = "F4:L20", graph = scatter)
wb$clone_worksheet(old = "chart", new = "chart_1", from = wb_ms)
wb$clone_worksheet(old = "chart", new = "chart_2", from = wb_ms)
img <- system.file("extdata", "einstein.jpg", package = "openxlsx2")
wb_img <- wb_workbook()$
add_worksheet()$
add_image("Sheet 1", dims = "C5", file = img, width = 6, height = 5)$
add_worksheet()$
add_image(dims = "B2", file = img)$
add_worksheet()$
add_image(dims = "G3", file = img, width = 15, height = 12, units = "cm")
wb$clone_worksheet(old = "Sheet 1", new = "img", from = wb_img)
wb_ex <- wb_load(system.file("extdata", "loadExample.xlsx", package = "openxlsx"))
# this one is still broken, not sure why. maybe the hyperlink reference?
wb$clone_worksheet(old = "testing", new = "test", from = wb_ex)
# hack to silnce this. need to get dxfsId from conditional formatting
wb$styles_mgr$styles$dxfs <- wb_ex$styles_mgr$styles$dxfs
if (interactive()) wb$open() |
* improve cloning images * improve cloning numfmts * document that this is experimental * trying to wrap my head around dxf style selection
It's as good as it gets for the first take. There are many things not yet working, but they are either nothing I required atm (like conditional formatting), tricky to implement (pivot tables and slicers) or a little special (is anyone actually using custom table styles). Fixing these can always happen later, once someone stumbles over these. |
Wow! Thanks for the great work. I think these caveats are fine for now. I will try it when I have the chance! |
Early attempt at #380
There are a couple of things to do and this is just a first step
copy elements
wb$worksheet[[n]]
wb$workbook
,wb$workbook.xml.rels
,wb$worksheet_rels
update copy elements
wb$worksheet_rels
(most likely everything will require a new file name)Hopefully we can build on
wb_clone_worksheet()
, but even then there is still a lot to do.a first draft of what could be possible