ebyerly
ebyerly

Reputation: 672

Font Coloring Using xlsx Package in R

I am writing an R function to save out pre-formatted data frames. Part of the format template calls for changing the column heading font color. The cell styling returns the correct alignment and bolding, but the text is still black.

The below is a bare version to demonstrate my font coloring problem (just change the file_path variable to a location that exists).

library(xlsx)
file_path <- "C:/Users/.../Desktop/tst.xlsx"
wb <- createWorkbook()

headerStyle <- CellStyle(wb,
                         font = Font(wb, isBold=TRUE, color = "#ffffff"),
                         fill = Fill(foregroundColor = "#2db6e8",
                                     pattern = "SOLID_FOREGROUND"),
                         alignment = Alignment(wrapText = TRUE,
                                               horizontal = "ALIGN_CENTER",
                                               vertical = "VERTICAL_CENTER")
)

x <- mtcars
sheet <- createSheet(wb, "test")

cellBlock <- CellBlock(sheet,
                       startRow = 1,
                       startCol = 1,
                       noRows = nrow(x) + 1,
                       noColumns = ncol(x) + 1,
                       create = TRUE)

CB.setRowData(cellBlock = cellBlock,
              x = colnames(x),
              rowIndex = 1,
              colOffset = 1,
              rowStyle = headerStyle +
                Border(pen = "BORDER_MEDIUM", color = "black",
                       position = "BOTTOM"))

saveWorkbook(wb, file_path)

Upvotes: 3

Views: 4337

Answers (3)

jcarlos
jcarlos

Reputation: 435

The constants used by the xlsx package are define in these groups:

HALIGN_STYLES_

VALIGN_STYLES_

BORDER_STYLES_

FILL_STYLES_

CELL_STYLES_

INDEXED_COLORS_

So, just displaying them in the console for INDEX_COLORS_ you get

  BLACK                 WHITE                   RED          BRIGHT_GREEN                  BLUE                YELLOW 
                    8                     9                    10                    11                    12                    13 
                 PINK             TURQUOISE              DARK_RED                 GREEN             DARK_BLUE           DARK_YELLOW 
                   14                    15                    16                    17                    18                    19 
               VIOLET                  TEAL       GREY_25_PERCENT       GREY_50_PERCENT       CORNFLOWER_BLUE                MAROON 
                   20                    21                    22                    23                    24                    25 
        LEMON_CHIFFON                ORCHID                 CORAL            ROYAL_BLUE LIGHT_CORNFLOWER_BLUE              SKY_BLUE 
                   26                    28                    29                    30                    31                    40 
      LIGHT_TURQUOISE           LIGHT_GREEN          LIGHT_YELLOW             PALE_BLUE                  ROSE              LAVENDER 
                   41                    42                    43                    44                    45                    46 
                  TAN            LIGHT_BLUE                  AQUA                  LIME                  GOLD          LIGHT_ORANGE 
                   47                    48                    49                    50                    51                    52 
               ORANGE             BLUE_GREY       GREY_40_PERCENT             DARK_TEAL             SEA_GREEN            DARK_GREEN 
                   53                    54                    55                    56                    57                    58 
          OLIVE_GREEN                 BROWN                  PLUM                INDIGO       GREY_80_PERCENT             AUTOMATIC 
                   59                    60                    61                    62                    63                    64 

You can use numeric or alias style:

  cs2 <- CellStyle(wb) +
    Font(
      wb,
      heightInPoints = 12,
      isBold = F,
      isItalic = F,
      name = "Arial",
      color="ORANGE"
    )

 cs2 <- CellStyle(wb) +
    Font(
      wb,
      heightInPoints = 12,
      isBold = F,
      isItalic = F,
      name = "Arial",
      color=59
    )

Upvotes: 1

JonH
JonH

Reputation: 46

I was able to get white text using the color index from the INDEXED_COLORS_ constant which is 9 for white. For your example code it would read:

headerStyle <- CellStyle(wb,
          font = Font(wb, isBold=TRUE, color = "9"),
          fill = Fill(foregroundColor = "#2db6e8",
          pattern = "SOLID_FOREGROUND"),
          alignment = Alignment(wrapText = TRUE,
                            horizontal = "ALIGN_CENTER",
                            vertical = "VERTICAL_CENTER")
)

Upvotes: 3

Minnow
Minnow

Reputation: 1811

It seems to relate uniquely to white text. Try using different colors:

headerStyle <- CellStyle(wb,
              font = Font(wb, isBold=TRUE, color = "grey"),
              fill = Fill(foregroundColor = "#2db6e8",
              pattern = "SOLID_FOREGROUND"),
              alignment = Alignment(wrapText = TRUE,
                                horizontal = "ALIGN_CENTER",
                                vertical = "VERTICAL_CENTER")

)

It works with orange, grey, blue, but not white. This may be an effort to prevent text from being invisible if the background were the default white, but I can't say for certain. Perhaps the package creator can comment.

Upvotes: 1

Related Questions