【问题标题】:R - xlsx package - how to add cell color without changing borderR - xlsx 包 - 如何在不改变边框的情况下添加单元格颜色
【发布时间】:2017-01-27 10:06:51
【问题描述】:

我正在尝试使用布局将表格导出到 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 列表”...

【问题讨论】:

    标签: r excel xlsx


    【解决方案1】:

    我知道怎么做!您需要使用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 包中记录的内容很少。

    【讨论】:

      【解决方案2】:

      对你来说可能为时已晚,但我认为你应该更换:

      cs3<- CellStyle(wb) + Fill(foregroundColor="lightblue", backgroundColor="lightblue", pattern="SOLID_FOREGROUND") 
      

      cs3<- cs2 + Fill(foregroundColor="lightblue", backgroundColor="lightblue", pattern="SOLID_FOREGROUND") 
      

      问候

      【讨论】:

        猜你喜欢
        • 2014-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        相关资源
        最近更新 更多