简体   繁体   English

如何根据 R 中的单元格字符格式化 xlsx 中的整行

[英]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.

相关问题 如何根据列是否全部为数字(但采用字符格式)将整列转换为数字? - How do I convert an entire column to numeric based on whether the column is all numbers (but in character format)? 在 R 中,如何根据特定的行/列条件有选择地将一个单元格“复制并粘贴”到另一个单元格中? - In R, how do I selectively 'copy and paste' a cell into another cell based on specific row/column criteria? 如何在 R 中格式化日期? - How do I format a date within R? 使用xlsx(R包)格式化具有相同单元格样式的整个数据框 - Format entire data frame with the same cell style using xlsx (R package) 如何使用xlsx(R包)设置特定单元格的值? - How do I set the value of a specific cell using xlsx (R package)? 如何在R中将字符字段转换为日期/时间格式? - How do I convert a character field to date/time format in R? 如何根据R Shiny DT中的模式突出显示一行中相邻单元格的串,每个单元格包含单个字符 - How to highlight bunch of adjacent cells in a row containing single character per cell based on a pattern in R Shiny DT 如何完全删除R中具有空单元格的行? - How do I completely delete a row with an empty cell in R? 基于另一行中的值合并 R 中的单元格,具有字符格式(非数字) - Merging cells in R based on a value in another row, with character format (not numeric) 如何根据 R 中的 url 拆分字符行 - How to split a character row, based on urls in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM