R - xlsx包 - 如何在不改变边框的情况下添加单元格颜色

标签 r excel xlsx

我正在尝试将带有布局的表格导出到 Excel。 我找到了很多有关 xlsx 包以及如何使用它的信息,但我的脚本仍然有问题。

我不知道如何在不修改之前添加的边框的情况下用颜色填充单元格。

作为示例,我创建了一个表 (Test.txt),我想为“Mass1”列的单元格着色,其值大于 30。

这是我编写的脚本:

library(xlsx)

Test<-read.table("Test.txt",sep="\t", dec=".", header = TRUE)

wb<-createWorkbook()
sheet <- createSheet(wb, "Sheet 1")

cs1 <- CellStyle(wb) + Alignment(horizontal="ALIGN_CENTER", vertical="VERTICAL_CENTER") + Border(color="black", position=c("TOP", "RIGHT" , "LEFT","BOTTOM"),pen=c("BORDER_MEDIUM","BORDER_MEDIUM","BORDER_MEDIUM","BORDER_MEDIUM"))
cs2 <- CellStyle(wb) + Border(color="black", position=c("LEFT","RIGHT","TOP", "BOTTOM"),pen=c("BORDER_THIN","BORDER_THIN","BORDER_THIN","BORDER_THIN"))

addDataFrame(Test, sheet, row.names = F, colnamesStyle=cs1, colStyle=list(`1`=cs2, `2`=cs2, `3`=cs2))

for(i in 1:nrow(Test) ){ 
  if(Test[i,2]>30){
    Row<-getRows(sheet, rowIndex=(i+1))
    Cell<-getCells(Row,colIndex = 2)
    cs3<- CellStyle(wb) + Fill(foregroundColor="lightblue", backgroundColor="lightblue", pattern="SOLID_FOREGROUND")    
    setCellStyle(Cell[[1]], cs3)
  }
}

saveWorkbook(wb, "Test.xlsx")

我的问题是单元格颜色正确,但底部边框消失了。我知道我可以在 cs3 样式中添加边框,但在我的真实脚本中,彩色单元格的底部边框并不总是相同(有些是细的,有些是中的)。

如何考虑先前创建的边框来添加填充颜色而不修改这些边框?我想 getCellStyle 函数可以提供帮助,但是当我应用此函数时,结果是“正式类 jobjref”,而不是像我的 cs3 样式那样的“8 的列表”...

最佳答案

我知道怎么做了!您需要使用 getBorderLeft 和其他三个函数获取单元格的边框样式,然后您可以在设置单元格样式时恢复边框。

这是一个示例,其中 test.xlsx 在位置 B2 处有一个带有一些边框、厚度为 1 的单元格,我们希望将其着色为黄色而不删除边框。

library(xlsx)

# load the workbook and get the sheet
wb <- loadWorkbook(file="test.xlsx")
SheetList <- getSheets(wb)
sheet <- SheetList[[1]]

# find the cell
row <- getRows(sheet, 2)
cells <- getCells(row, 2)
cell <- cells[[1]]

# get current borders
style <- getCellStyle(cell)
border <- c(style$getBorderLeft(), 
            style$getBorderTop(), 
            style$getBorderRight(),
            style$getBorderBottom()
           )

# construct new cell style; also works if there were no borders
new_style <- CellStyle(wb) + 
           Fill(backgroundColor = "yellow", 
                foregroundColor = "yellow",
                pattern         = "SOLID_FOREGROUND") +
           Border(color    = "black", 
                  position = c("LEFT", 
                               "TOP", 
                               "RIGHT", 
                               "BOTTOM"
                              )[border > 0])

# apply new cell style
setCellStyle(cell, new_style)
saveWorkbook(wb, "test2.xlsx")

边框厚度可以用类似的方式处理。提示:在 RStudio 中,在样式对象名称(本例中为 style)后面输入 $ 会显示所有可用方法的列表,这非常方便,因为太少了记录在 xlsx 包中。

关于R - xlsx包 - 如何在不改变边框的情况下添加单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41891373/

相关文章:

excel - Microsoft Excel 2010 复制/粘贴编辑想法和小细节帮助请求

linux - Perl 编写 excel 以在各种工作表中创建包含包含公式的单元格的图表

javascript - 在 javascript 中读取本地 xls/xlsx 文件

r - 如何从 R 中将 Excel 工作表范围导出到图片

excel - 宏仅在单步执行时有效

r - 将命名的 data.frames 列表写入 xlsx 文件

html - 使用R和plot.ly,如何将多个htmlwidgets保存到我的html中?

r - 如何使用 R 根据 child 的标签标记树状图中的每个节点

r - 使用 write.csv 时防止将行名称写入文件

r - 将函数列表应用于值向量