[英]Font Coloring Using xlsx Package in R
I am writing an R function to save out pre-formatted data frames. 我正在编写一个R函数来保存预先格式化的数据帧。 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). 下面是一个裸版本,用于演示我的字体着色问题(只需将file_path变量更改为存在的位置)。
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)
I was able to get white text using the color index from the INDEXED_COLORS_ constant which is 9 for white. 我能够使用INDEXED_COLORS_常量的颜色索引获得白色文本,白色为9 。 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")
)
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.
也许包创建者可以发表评论。
The constants used by the xlsx package are define in these groups: xlsx包使用的常量在这些组中定义:
HALIGN_STYLES_
VALIGN_STYLES_
BORDER_STYLES_
FILL_STYLES_
CELL_STYLES_
INDEXED_COLORS_
So, just displaying them in the console for INDEX_COLORS_
you get 所以,只需在控制台中显示
INDEX_COLORS_
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.