使用包 openxlsx 将多个 styles() 应用于同一行
Multiple styles() applied to the same row with package openxlsx
我最近开始使用 R
软件包 openxlsx
,因为它根本不需要 rJava
软件包或 Java。我正在尝试将日期写入列,但行格式并未应用于我的 xlsx
文件中的最终日期列。
示例数据:
tmp <- structure(list(someNumbers = c(8L, 3L, 4L, 4L, 1L, 4L, 2L, 2L,
7L, 2L), someLetters = structure(c(9L, 4L, 1L, 2L, 7L, 3L, 4L,
6L, 8L, 5L), .Label = c("b", "d", "e", "h", "l", "n", "o", "t",
"u"), class = "factor"), moreNumbers = c(25L, 25L, 36L, 38L,
39L, 32L, 31L, 40L, 28L, 32L), moreStuff = c(0.37, 1.39, -2.27,
0.65, -0.71, -1.67, 0.71, 1.32, 2.14, 1.13), words = structure(c(5L,
3L, 7L, 1L, 2L, 6L, 4L, 10L, 9L, 8L), .Label = c("And another, but
maybe I'll make this one a little bit longer.",
"camera", "cat", "Ceramic penil holder made by yours truly.",
"dog", "headset", "Here's a list of words, almost like a
description.",
"Last", "Number 9. Number 9. Number 9.", "This phrase is not six
words long."
), class = "factor"), dates = structure(c(2000, 3217.55555555556,
4435.11111111111, 5652.66666666667, 6870.22222222222,
8087.77777777778,
9305.33333333333, 10522.8888888889, 11740.4444444444, 12958), class =
"Date")), .Names = c("someNumbers",
"someLetters", "moreNumbers", "moreStuff", "words", "dates"),
row.names = c(NA,
-10L), class = "data.frame")
和代码:
#Setting date options
options("openxlsx.dateFormat" = "yyyy-mm-dd")
#Create the xlsx workbook and insert a worksheet
testing <- createWorkbook()
addWorksheet(testing, "openxlsxTesting")
#Set column widths
setColWidths(testing, "openxlsxTesting", cols = 1:ncol(tmp), widths =
"auto")
#Create a header style
headerStyle <- createStyle(fontColour = "white", fgFill =
"blue", halign = "left", valign = "center", textDecoration =
"bold")
#Create styles to shade alternate rows
lightStyle <- createStyle(fontColour = "black", fgFill = "#E7E0C5", halign =
"right", valign = "center")
darkStyle <- createStyle(fontColour = "black", fgFill = "#E1D8B7", halign =
"right", valign = "center")
#A vector of the xlsx rows
colorRows <- 2:(nrow(tmp)+1)
#Apply the header style
addStyle(testing, "openxlsxTesting", headerStyle, rows = 1, cols =
1:ncol(tmp), gridExpand = TRUE)
#Apply the alternate shading styles
addStyle(testing, "openxlsxTesting", lightStyle, rows =
colorRows[which(colorRows %% 2 == 0)], cols = 1:ncol(tmp), gridExpand = TRUE)
addStyle(testing, "openxlsxTesting", darkStyle, rows =
colorRows[which(colorRows %% 2 == 1)], cols = 1:ncol(tmp), gridExpand = TRUE)
#Write data to the workbook
writeData(testing, "openxlsxTesting", tmp, startRow = 1, startCol = 1)
#Save workbook to getwd()
saveWorkbook(testing, "openXlsxTesting.xlsx", TRUE)
我最终得到的是下面的图像,它几乎 格式正确,但日期列缺少阴影颜色。
我错过了什么吗?我已阅读文档,但无法找到将格式应用于日期字段的方法。我试过专门格式化日期列,然后在应用日期格式后 运行 addStyle()
,但仍然不行。
为什么 addStyle()
函数没有将 lightStyle
或 darkStyle
应用到日期列?
如有任何帮助,我们将不胜感激。
样式正在被日期列样式覆盖。在添加任何样式之前调用 writeData() 并在调用 addStyle() 时设置 stack = TRUE 将解决此问题。我将在 github 上更改代码
https://github.com/awalker89/openxlsx 以防止样式被覆盖。
我最近开始使用 R
软件包 openxlsx
,因为它根本不需要 rJava
软件包或 Java。我正在尝试将日期写入列,但行格式并未应用于我的 xlsx
文件中的最终日期列。
示例数据:
tmp <- structure(list(someNumbers = c(8L, 3L, 4L, 4L, 1L, 4L, 2L, 2L,
7L, 2L), someLetters = structure(c(9L, 4L, 1L, 2L, 7L, 3L, 4L,
6L, 8L, 5L), .Label = c("b", "d", "e", "h", "l", "n", "o", "t",
"u"), class = "factor"), moreNumbers = c(25L, 25L, 36L, 38L,
39L, 32L, 31L, 40L, 28L, 32L), moreStuff = c(0.37, 1.39, -2.27,
0.65, -0.71, -1.67, 0.71, 1.32, 2.14, 1.13), words = structure(c(5L,
3L, 7L, 1L, 2L, 6L, 4L, 10L, 9L, 8L), .Label = c("And another, but
maybe I'll make this one a little bit longer.",
"camera", "cat", "Ceramic penil holder made by yours truly.",
"dog", "headset", "Here's a list of words, almost like a
description.",
"Last", "Number 9. Number 9. Number 9.", "This phrase is not six
words long."
), class = "factor"), dates = structure(c(2000, 3217.55555555556,
4435.11111111111, 5652.66666666667, 6870.22222222222,
8087.77777777778,
9305.33333333333, 10522.8888888889, 11740.4444444444, 12958), class =
"Date")), .Names = c("someNumbers",
"someLetters", "moreNumbers", "moreStuff", "words", "dates"),
row.names = c(NA,
-10L), class = "data.frame")
和代码:
#Setting date options
options("openxlsx.dateFormat" = "yyyy-mm-dd")
#Create the xlsx workbook and insert a worksheet
testing <- createWorkbook()
addWorksheet(testing, "openxlsxTesting")
#Set column widths
setColWidths(testing, "openxlsxTesting", cols = 1:ncol(tmp), widths =
"auto")
#Create a header style
headerStyle <- createStyle(fontColour = "white", fgFill =
"blue", halign = "left", valign = "center", textDecoration =
"bold")
#Create styles to shade alternate rows
lightStyle <- createStyle(fontColour = "black", fgFill = "#E7E0C5", halign =
"right", valign = "center")
darkStyle <- createStyle(fontColour = "black", fgFill = "#E1D8B7", halign =
"right", valign = "center")
#A vector of the xlsx rows
colorRows <- 2:(nrow(tmp)+1)
#Apply the header style
addStyle(testing, "openxlsxTesting", headerStyle, rows = 1, cols =
1:ncol(tmp), gridExpand = TRUE)
#Apply the alternate shading styles
addStyle(testing, "openxlsxTesting", lightStyle, rows =
colorRows[which(colorRows %% 2 == 0)], cols = 1:ncol(tmp), gridExpand = TRUE)
addStyle(testing, "openxlsxTesting", darkStyle, rows =
colorRows[which(colorRows %% 2 == 1)], cols = 1:ncol(tmp), gridExpand = TRUE)
#Write data to the workbook
writeData(testing, "openxlsxTesting", tmp, startRow = 1, startCol = 1)
#Save workbook to getwd()
saveWorkbook(testing, "openXlsxTesting.xlsx", TRUE)
我最终得到的是下面的图像,它几乎 格式正确,但日期列缺少阴影颜色。
我错过了什么吗?我已阅读文档,但无法找到将格式应用于日期字段的方法。我试过专门格式化日期列,然后在应用日期格式后 运行 addStyle()
,但仍然不行。
为什么 addStyle()
函数没有将 lightStyle
或 darkStyle
应用到日期列?
如有任何帮助,我们将不胜感激。
样式正在被日期列样式覆盖。在添加任何样式之前调用 writeData() 并在调用 addStyle() 时设置 stack = TRUE 将解决此问题。我将在 github 上更改代码 https://github.com/awalker89/openxlsx 以防止样式被覆盖。