pteridin / flexlsx

R Package to add flextables to Excel files
Other
13 stars 2 forks source link

Font color transparent is handled incorrect #4

Closed JanMarvin closed 1 year ago

JanMarvin commented 1 year ago

Another example from: https://www.ardata.fr/en/flextable-gallery/

The font of the table should be white, but is black. The cell color and the fmt_txt() color differ.

library(flextable)
library(flexlsx)
library(openxlsx2)

theme_design <- function(x) {
  x <- border_remove(x)
  std_border <- fp_border_default(width = 4, color = "white")
  x <- fontsize(x, size = 10, part = "all")
  x <- font(x, fontname = "Courier", part = "all")
  x <- align(x, align = "center", part = "all")
  x <- bold(x, bold = TRUE, part = "all")
  x <- bg(x, bg = "#475f77", part = "body")
  x <- bg(x, bg = "#eb5555", part = "header")
  x <- bg(x, bg = "#1bbbda", part = "footer")
  x <- color(x, color = "white", part = "all")
  x <- padding(x, padding = 6, part = "all")
  x <- border_outer(x, part="all", border = std_border )
  x <- border_inner_h(x, border = std_border, part="all")
  x <- border_inner_v(x, border = std_border, part="all")
  x <- set_table_properties(x, layout = "fixed")
  x
}

ft <- flextable(head(airquality)) %>%
  add_footer_lines(
    c("Daily air quality measurements in New York, May to September 1973.",
      "Hummm, non non rien.")) %>%
  autofit() %>%
  add_header_lines("New York Air Quality Measurements") %>%
  theme_design()
ft

wb <- openxlsx2::wb_workbook()$add_worksheet("mtcars", grid_lines = FALSE)

# add the flextable ft to the workbook, sheet "mtcars"
# offset the table to cell 'C2'
wb <- wb_add_flextable(wb, "mtcars", ft, dims = "C2")

wb$open()
pteridin commented 1 year ago

Ok. This is where I need someone with a little more expertise in the quirks of excel XML.

The fix itself should have been quite easy. Just coalesce the color if it is not set in the font itself, white color in the example. Nice.

But when I try to create something like this with the new code Excel complains of malformatted rows:

ft <- flextable::as_flextable(table(mtcars[,1:2]))
wb <- openxlsx2::wb_workbook()$add_worksheet("mtcars")
wb_add_flextable(wb, "mtcars", ft)$save(tmpfile)

I guess this has something to do with applying "standard values" for color - in this case "black" - that should not be set, because it is the standard color value in this Excel file? But that is just a wild guess.

JanMarvin commented 1 year ago

This is more or less a question for flextable. In openxml you have font colors in styles and rich text strings can contain style colors too. There is no such thing as a transparent font in openxml. You can apply font color auto, but not transparent (after all transparent would indicate that there is no visible content in the cell).

pteridin commented 1 year ago

Should be more or less fixed. Could become an issue again if color of the Excel file is not black as default, though maybe a non-problem. Will try that out later.