-
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
Feature Request: Clear Content (delete data but keep formatting) #562
Comments
Hi @DavZim , please have a look at |
Very close, but I want to clean only a given range. |
Got it, I can see if the existing function can be extended to include |
One use case that was reliable in producing bugs was to have one whole column with a given format (red background) with some values in rows 1-10. Note that my solution on top fails for this use case (last part regarding used sizes). |
Hi again, I was traveling earlier and and my patience with github on the smartphone was limited. I am glad you find I have opened a pull request in #563. Maybe you can have a look if this solves your issue? I don't really get your latest reply. Could you craft a reproducible example or mail me one? |
Maybe you mean something like this: # create example data
wb <- wb_workbook() %>%
wb_add_worksheet() %>%
wb_add_fill(dims = "D1:D11", color = wb_colour("red")) %>%
wb_add_data(x = matrix(1:100, 10, 10))
# apply style 1 as column color (this is only the visual)
wb$worksheets[[1]]$cols_attr <- "<col min=\"4\" max=\"4\" width=\"8.83203125\" style=\"1\"/>"
# remove the red numbers
wb$clean_sheet(dims = "D2:D10000", styles = FALSE)
## you see this (original dimensions A1:J11)
wb_to_df(wb)
## you expect this? (dimensions A1:J100000)
wb$add_data(dims = "A12", x = matrix(NA, nrow = 1e5-12, ncol = 10), na.strings = NULL, colNames = FALSE)
wb_to_df(wb) In this case I would rather not change the current behavior. We had to jump through some hoops to deal with the openxml way of having uninitialized cells. If you need them and are not sure if they are available, you will have to create them yourself. |
If I understand you correctly, this should now be possible. I have recreated your example below: library(openxlsx2)
# create example data
wb <- wb_workbook() %>%
wb_add_worksheet("ex1") %>%
wb_add_data(x = c(rep(NA, 3), 1:10), na.strings = NULL) %>%
wb_add_fill(dims = "A7:A13", color = wb_colour("red"))
# apply style from A7 as column color
# (this is a visual overlay for uninintialized cells)
style <- wb %>% wb_get_cell_style(dims = "A7")
wb$worksheets[[1]]$cols_attr <-
sprintf("<col min=\"1\" max=\"1\" width=\"8.83203125\" style=\"%s\"/>", style)
# clone, clean and update sheet
wb$clone_worksheet(new = "target")$
clean_sheet(dims = "A7:A100000", styles = FALSE)$
add_data(dims = "A7", x = letters[1:3])
# wb$open() One issue remains, if you write more rows than were previously initialized in the worksheet, you have to manually set a style for these cells: # clone, clean and update sheet
wb$clone_worksheet(new = "target2")$
clean_sheet(dims = "A7:A100000", styles = FALSE)$
add_data(dims = "A7", x = letters)
# we write a larger vector and have to set styles accordingly
dims_to_style <- sprintf("A7:A%s", 6 + nrow(as.data.frame(letters)))
wb$set_cell_style(dims = dims_to_style, style = style)
# wb$open() |
That looks promising. I'll try the solution but it might take some time. Thank you for the quick responses! Highly appreciated! |
We have all the time in the world ;) I've merged the PR. A new CRAN release should follow next weekend. |
Just to give you some quick feedback. That works as expected! |
First, I love how this fixes many of the issues of openxlsx that I faced... Your work is highly appreciated.
It would be great to have a function that allows me to clear the contents, ie delete the data but keep the formatting.
At the moment I use this wrapper. But maybe there is a better way...
The text was updated successfully, but these errors were encountered: