I am trying to export a table in Excel with a layout. I found a lot of information about the xlsx package and how to use it, but there is still something wrong with my script.
I do not know how to fill a cell with color without changing previously added borders.
As an example, I created a table (Test.txt), and I want to color the cells of the column "Mass1" with a value greater than 30.
Here is the script I wrote:
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")
My problem is that the cells are correctly colored, but the bottom border disappears. I know that I can add borders to my cs3 style, but in my real script, the lower borders of my color cells are not always the same (some of them are Thin and others are Medium).
, ? , getCellStyle , , " jobjref", " 8", cs3...