[英]How do I format an entire row within xlsx based on a cell character in R
I am using R to edit an xlsx worksheet.我正在使用 R 编辑 xlsx 工作表。 I would like to format the worksheet with colored rows based on character values in a specific column, then save the workbook in xlsx form.
我想根据特定列中的字符值使用彩色行格式化工作表,然后以 xlsx 形式保存工作簿。 I successfully loaded the workbook using xlsx in R. I can run through the characters in the workbook and change that specific cell background color based on a condition, but am having trouble changing the color of that entire row.
我在 R 中使用 xlsx 成功加载了工作簿。我可以浏览工作簿中的字符并根据条件更改特定的单元格背景颜色,但无法更改整行的颜色。
My question is, how can I make the entire row a solid color, rather than just the cell?我的问题是,如何使整行成为纯色,而不仅仅是单元格? So far, I have followed the instructions and code located here:
到目前为止,我已按照此处的说明和代码进行操作:
Color cells with specific character values in r to export to xlsx r 中具有特定字符值的颜色单元格以导出到 xlsx
What would you have to add to the code in the above link to make the entire row the same color as the specific cell that was targeted?您必须向上述链接中的代码添加什么才能使整行与目标特定单元格的颜色相同?
greenStyle <- createStyle(fontColour = "#000000", bgFill = "green")
yellowStyle <- createStyle(fontColour = "#000000", bgFill = "yellow")
conditionalFormatting(wb, "entire report", cols=1:12, rows=1:2000, rule="Finished", style = greenStyle, type = "contains")
conditionalFormatting(wb, "entire report", cols=1:12, rows=1:2000, rule="In Process", style = yellowStyle, type = "contains")
saveWorkbook(wb, file, overwrite=TRUE)
This question is from some time ago, I was looking to do this, then found the answer and wanted to share (I am the author of the post referred in the question btw :) )这个问题来自前一段时间,我想这样做,然后找到了答案并想分享(我是问题 btw 中提到的帖子的作者 :) )
So, in order to color an entire row, I made a reproducible example:所以,为了给整行着色,我做了一个可重复的例子:
Reproducible example:可重现的例子:
dfX <- data.frame('a' = c(1:4),
'b' = c(1:2,2:1),
'c' = LETTERS[1:4],
'e' = LETTERS[1:2][2:1],
'f' = c('Finished', 'In Process', 'In Process', 'In Process'))
library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Sheet", gridLines = TRUE)
writeData(wb, "Sheet", dfX)
greenRows = data.frame(which(dfX == "Finished", arr.ind=TRUE))
yellowRows = data.frame(which(dfX == "In Process", arr.ind=TRUE))
## Here I create data frames where it states which rows and columns
## have 'Finished' and which have 'In Process'. From here I want to keep only the
## rows from these data frames.
# Create a heading style
Heading <- createStyle(textDecoration = "bold", border = "Bottom")
# Row styles
greenStyle <- createStyle(fontColour = "#000000", fgFill = "green")
yellowStyle <- createStyle(fontColour = "#000000", fgFill = "yellow")
Important Note: I use "fgFill"
instead of "bgFill"
because in order to do this, we will use addStyle
(and not conditionalFormatting
), and in the documentation, it states that bgFill
is only for conditionalFormatting
重要说明:我使用
"fgFill"
而不是"bgFill"
因为为了做到这一点,我们将使用addStyle
(而不是conditionalFormatting
),并且在文档中,它指出bgFill
仅用于conditionalFormatting
# Apply header style:
addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = 1, style = Heading)
# Apply greenStyle:
addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = greenRows[,1]+1,
style = greenStyle, gridExpand = TRUE)
# Apply yellowStyle:
addStyle(wb, "Sheet", cols = 1:ncol(dfX), rows = yellowRows[,1]+1,
style = yellowStyle, gridExpand = TRUE)
saveWorkbook(wb, file, overwrite=TRUE)
Note that in "rows = "
I input greenRows[,1]+1
, which means only the first column of the greenRows data.frame, plus 1 (the first row would be the header, so skip this one)请注意,在
"rows = "
我输入greenRows[,1]+1
,这意味着只有 greenRows data.frame 的第一列加 1(第一行将是标题,所以跳过这一行)
Also note that in the last line, in the file
part you should specify the directory where to save the file with the .xlsx
termination, such as:另请注意,在最后一行的
file
部分中,您应该指定保存文件的目录,以.xlsx
结尾,例如:
saveWorkbook(wb, file = "C:/Documents/newfile.xlsx", overwrite=TRUE)
This post , though not the same question, helped me. 这篇文章虽然不是同一个问题,但对我有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.