awalker89 / openxlsx

R package for .xlsx file reading and writing.
Other
364 stars 79 forks source link

bgFill with AddStyle always returning black cells #541

Closed LafontRapnouilTristan closed 2 years ago

LafontRapnouilTristan commented 2 years ago

Hi, first, thanks for this amazing package and work!!

I encounter some issues to color specific cell position within .xlsx file with addstyle while it work whit conditional formatting.

Expected Behavior

My cells should be colored according to specified bgFill style. I have tried many times, and cell positioning is good but bgFill is always black. Whatever "Style" I supply to the function.

Actual Behavior

It works perfectly fine with conditional formatting but with addStyle it returns a black colored cell while the used style is the same. As I want to color wide range of specific cell coordinates, I'd prefer to use addstyle rather than a workaround with conditional formatting.

Steps to Reproduce the Problem

(please attach an example xlsx file if possible)

  1. Create style the_style <- openxlsx::createStyle(bgFill = "#E3E338") # a yellowish bgFill

    1. Build a dummy .xlsx workbook dumdum <- openxlsx::buildWorkbook(data.frame(x = 1, y = 1:10))
  2. Applying style via addStyleto the first column, fifth row

    openxlsx::addStyle(dumdum ,
                   sheet = 1,
                   cols = 1,
                   rows = 5,
                   style = the_style )
  3. Applying style via conditionalFormattingto the first column, fourth row

    openxlsx::conditionalFormatting(dumdum ,
                                sheet = 1,
                                cols = 1,
                                rows = 4,
                                style = csctab,
                                rule = "1",
                                type = "contains")
  1. Save the xlsx

openxlsx::saveWorkbook(dumdum , "../../wow.xlsx", overwrite = T)

  1. The returned xlsx: image

sessionInfo()

R version 4.1.1 (2021-08-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale: [1] LC_COLLATE=English_Europe.1252 [2] LC_CTYPE=English_Europe.1252
[3] LC_MONETARY=English_Europe.1252 [4] LC_NUMERIC=C
[5] LC_TIME=English_Europe.1252

attached base packages: [1] stats graphics grDevices utils datasets methods
[7] base

other attached packages: [1] magrittr_2.0.2 EcophyCofog_0.0.0.9000

loaded via a namespace (and not attached): [1] zip_2.2.0 Rcpp_1.0.8.2 pillar_1.7.0
[4] compiler_4.1.1 cellranger_1.1.0 tools_4.1.1
[7] lifecycle_1.0.1 tibble_3.1.6 gtable_0.3.0
[10] pkgconfig_2.0.3 rlang_1.0.2 openxlsx_4.2.5
[13] DBI_1.1.2 cli_3.2.0 rstudioapi_0.13
[16] dplyr_1.0.8 stringr_1.4.0 generics_0.1.2
[19] vctrs_0.3.8 hms_1.1.1 combinat_0.0-8
[22] grid_4.1.1 tidyselect_1.1.2 data.table_1.14.2 [25] glue_1.6.2 R6_2.5.1 fansi_1.0.2
[28] readxl_1.3.1 farver_2.1.0 ggplot2_3.3.5
[31] purrr_0.3.4 readr_2.1.2 tzdb_0.2.0
[34] scales_1.2.0 ellipsis_0.3.2 rlist_0.4.6.2
[37] assertthat_0.2.1 colorspace_2.0-3 utf8_1.2.2
[40] stringi_1.7.6 munsell_0.5.0 crayon_1.5.1

LafontRapnouilTristan commented 2 years ago

wow.xlsx

LafontRapnouilTristan commented 2 years ago

ERRATUM: Following further investigation I just found a Stackoverflow question dealing with this issue. It is about using Fgfill instead of bgFill when using addStyle and not ConditionalFormatting. Not intuitive but it worked!!