Home > Net >  addStyle function in openxlsx does not fill cells in excel spreadsheet with the right color
addStyle function in openxlsx does not fill cells in excel spreadsheet with the right color

Time:03-25

Consider the following code block from the r package openxlsx.

I'm trying to fill certain cells in the excel spreadsheet with a particular color.

This is the code I used in order to do this.

library(openxlsx)

# Create a new workbook
wb <- createWorkbook("My name here")

# Add a worksheets
addWorksheet(wb, "Expenditure", gridLines = FALSE)
addWorksheet(wb, "Income", gridLines = FALSE)

# write data to worksheet 1
writeData(wb, sheet = 1, USPersonalExpenditure, rowNames = TRUE)

# Create style object in order to fill certain cells in the excel spreadsheet.
Styling_object <- createStyle(fontColour = "red", bgFill = "yellow")

# Add style to cell in row 2 column 1.
addStyle(wb, sheet = 1, style = Styling_object, rows = 2, cols = 3)

# Add style to cell in row 4 column 3.
addStyle(wb, sheet = 1, style = Styling_object, rows = 4, cols = 3)

# save the workbook
saveWorkbook(wb, "testing_add_style_1.xlsx", overwrite  = TRUE)

What I don't understand is why the cells that I specified to be filled up in the excel sheet is filled up with the wrong color. I specified the bgfill argument in the createStyle to be "yellow" which is the fill color I want for the specified cells, but for some reason the cell is filled with a black color in the resulting excel spreadsheet.

This is what I get in my excel spreadsheet.

enter image description here

While it should look something like this:

enter image description here

CodePudding user response:

Use fgFill instead of bgFill. bgFill is for conditional formatting.

Styling_object <- createStyle(fontColour = "red", fgFill = "yellow")

enter image description here

  • Related