-
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
Issues with conditional formatting #664
Comments
Hi @Michiel91 , you have to assign the style to the workbook with a name. (That it works in the first place was a fluke). See my adjustment below, I had only libreoffice to test and here the gradient did not work as expected: library(openxlsx2)
## Create example dataset
data <- matrix(sample(c("A","B","C"), size = 20, replace = TRUE), 5, 4)
## create styles
# formatting_style1
xml_fill_code1 <- openxlsx2::read_xml(
glue::glue("
<gradientFill degree=\"45\">
<stop position=\"0\"><color rgb=\"440154\"/></stop>
<stop position=\"1\"><color rgb=\"31688E\"/></stop>
</gradientFill>
"),
pointer = FALSE)
formatting_style1 <- openxlsx2::create_dxfs_style(gradientFill = xml_fill_code1)
# formatting_style2
xml_fill_code2 <- openxlsx2::read_xml(
glue::glue("
<gradientFill degree=\"45\">
<stop position=\"0\"><color rgb=\"35B779\"/></stop>
<stop position=\"1\"><color rgb=\"FDE725\"/></stop>
</gradientFill>
"),
pointer = FALSE)
formatting_style2 <- openxlsx2::create_dxfs_style(gradientFill = xml_fill_code2)
## Create workbook
workbook <- openxlsx2::wb_workbook() %>%
openxlsx2::wb_add_worksheet("test") %>%
# add data
openxlsx2::wb_add_data(
x = data,
sheet = "test"
) %>%
# add conditional styles
wb_add_style(formatting_style1, "formatting_style1") %>%
wb_add_style(formatting_style2, "formatting_style2") %>%
# add conditional formatting
openxlsx2::wb_add_conditional_formatting(
sheet = "test",
dims = rowcol_to_dims(
seq_len(nrow(data)),
seq_along(data)
),
rule = '=="A"',
style = "formatting_style1"
) %>% openxlsx2::wb_add_conditional_formatting(
sheet = "test",
dims = rowcol_to_dims(
seq_len(nrow(data)),
seq_along(data)
),
rule = '=="B"',
style = "formatting_style2"
)
# ## save
# workbook %>%
# wb_save(
# path = "test.xlsx",
# overwrite = TRUE
# )
## Checks
workbook$worksheets[[1]]$conditionalFormatting
#> A1:T5
#> "<cfRule type=\"expression\" dxfId=\"0\" priority=\"2\"><formula>A1="A"</formula></cfRule>"
#> A1:T5
#> "<cfRule type=\"expression\" dxfId=\"1\" priority=\"1\"><formula>A1="B"</formula></cfRule>"
workbook$styles_mgr$dxf
#> typ id name
#> 1 dxf 0 formatting_style1
#> 2 dxf 1 formatting_style2
workbook$styles_mgr$styles$dxfs
#> [1] "<dxf><fill><gradientFill degree=\"45\"><stop position=\"0\"><color rgb=\"440154\"/></stop><stop position=\"1\"><color rgb=\"31688E\"/></stop></gradientFill></fill></dxf>"
#> [2] "<dxf><fill><gradientFill degree=\"45\"><stop position=\"0\"><color rgb=\"35B779\"/></stop><stop position=\"1\"><color rgb=\"FDE725\"/></stop></gradientFill></fill></dxf>" |
Hi @JanMarvin, thanks for the quick reply! Using the style_name together with swapping the order of wb_add_style and wb_add_conditional_formatting indeed solved the issue. I couldn't find this in the documentation or vignette, it appeared as if the style name was optional and the order of these two functions is arbitrary. Coming from openxlsx where a style is added automatically when using 'conditionalFormatting', do you perhaps see a possibility to embed wb_add_style into the wb_add_conditional_formatting function and have it run automatically in the background? |
Hm, it is straight up there on top, but still using openxlsx2/vignettes/conditional-formatting.Rmd Lines 23 to 27 in 8d6b3ed
Initially I had something like this in mind, where |
Thanks for improving the vignettes! I guess the styles_mgr instead of add_style caused me to miss it. I like your suggestion of wb_add_dxfs_style (and hope the word 'style' in there doesn't trigger the stylesObject PTSD 😜). I think having such a wrapper would be a great addition and reduces complexity. Cheers, |
#665 This should be sufficient. Needs an additional test, but should make it for 0.7.1 later next week. |
Awesome, thanks a lot for this nice addition! |
closed in 5e90271 🎉 |
Hi @JanMarvin, I might have been a bit too fast with replying everything works earlier today. Hopefully my thorough testing of openxlsx2 isn't giving you sleepless nights (or PTSD) yet... Just ran into what seems to be a downstream issue with conditional formatting, but I would like to double check with you before I open a new ticket. Supplying a custom dxfs style containing an XML gradient fill works perfectly when not specifying a "type" in wb_add_conditional_formatting. However when using it in combination with for example type = "containsText" it breaks and returns Error: xml import unsuccessfull (minor spelling mistake btw, - last l 😉). This happens for all types. Other custom dxfs styles do work with all types. Could it be that something in the type argument doesn't yet recognize the recently implemented XML code? |
Please ignore my comment above. It does work, but I had to do a bit of tweaking to get it working. I was using containsText in combination with a string containing special characters. These required an additional escape character in the rule value. Not sure why it does work with the regular conditional formatting without a type, but I guess it's a fluke just like the example from this morning. Cheers and thanks again for the implementation in #665 😉 |
Thanks for the debugging, it seems we just posted at the same time. I could also get it to work with simpler rules like "A" or "1" and then went on to see what was causing the other strings not to work. It turned out to be special characters also being wildcards in Excel. |
Typo is fixed in main, thanks. Could you give an example what "special characters also being wildcards in Excel." this means? Do we need to escape the rule? |
I might have a fix for the unescaped special characters #666 |
Hi @JanMarvin, I just tested your fix in #666 and this seems to work nicely for special characters like *, =, etc. Thanks again for the very fast implementation! |
Thanks, but openxlsx2/R/illegal-characters.R Lines 40 to 41 in 4680281
Conditional formatting is one of the things I have spent literally days on to get it working and documenting and still things like this pass by, because I don't use it at all and no one else has reported them. Therefore all the reports help. If your unsure, open a discussion, so that the issues part is not bloated that much and creating minimal reproduceable examples is really helpful, otherwise I have to... 😄 |
Hi @JanMarvin, Not sure what exactly changed downstream, but the formatting at least now works when I escape special characters myself upfront. I think an interaction between these special characters and == or & not being escaped might have been causing the issues? From my side all good to go as the issues I mentioned in this ticket are resolved. |
While testing several types of conditional formatting, I noticed that only one is applied to the actual workbook when more are set. Below is a detailed example showing what happens when two conditional formatting styles are applied:
The output Excel workbook shows that only one style works, while the other one is shown but not applied properly. The printed workbook properties hint at something going wrong with the dxfId's. Everything past the first style becomes NA (or all 0). Not sure if anything else is causing this bug, but this is what I could already find while debugging.
Thanks for looking into this! Let me know if you can use additional examples or info.
The text was updated successfully, but these errors were encountered: