2015-08-24 18 views
5

UPDATE: RISOLTO - il codice è stato aggiornato per formattare mia cella singola bluR pacchetto XLSX: formattazione della cella singola

Sono nuovo di utilizzare XLSX e sto cercando di fare il mio tutta la seconda fila blu (compreso la prima colonna) nel mio foglio di lavoro di output. Ma ho problemi a fare riferimento a quella cella per renderla blu.

Aggiornamento 3: Sotto il commento "#reia per rendere la cella A2 blu", posso (a partire dal terzo aggiornamento) fare riferimento alla cella; tuttavia, posso fare riferimento solo creando una nuova cella, che quindi crea un problema con i dati che ho già inserito. Se creo prima la nuova cella, viene semplicemente sovrascritta quando aggiungo i dati dalla cornice dati. C'è un modo per fare riferimento a una cella quando è già stata creata?

mio codice è qui sotto:

library(xlsx) 
# create a new workbook for outputs 
wb<-createWorkbook(type="xlsx") 

# Define some cell styles 
TITLE_STYLE <- CellStyle(wb)+ Font(wb, heightInPoints=10, 
            isBold=TRUE, name="Arial") + Alignment(horizontal="ALIGN_CENTER") 

# Styles for the data table row/column names 
TABLE_ROWNAMES_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, name="Arial") 
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, isBold=TRUE, color ="9", name="Arial") + Fill(foregroundColor="#0069AA") + 
    Alignment(wrapText=TRUE, horizontal="ALIGN_CENTER") 
TABLE_STYLE <- CellStyle(wb) + Font(wb, heightInPoints=10, name="Arial") 


# Create a new sheet in the workbook 
#++++++++++++++++++++++++++++++++++++ 
sheet <- createSheet(wb, sheetName = "US State Facts") 

#++++++++++++++++++++++++ 
# Helper function to add titles 
#++++++++++++++++++++++++ 
# - sheet : sheet object to contain the title 
# - rowIndex : numeric value indicating the row to 
#contain the title 
# - title : the text to use as title 
# - titleStyle : style object to use for title 
xlsx.addTitle<-function(sheet, rowIndex, title, titleStyle){ 
    rows <-createRow(sheet,rowIndex=rowIndex) 
    sheetTitle <-createCell(rows, colIndex=1) 
    setCellValue(sheetTitle[[1,1]], title) 
    setCellStyle(sheetTitle[[1,1]], titleStyle) 
} 

# Add title 
xlsx.addTitle(sheet, rowIndex=1, title="US State Facts", 
       titleStyle = TITLE_STYLE) 


#Add a table into a worksheet 

cell.format <- rep(list(TABLE_STYLE), (dim(state.x77)[2])) # style for remaining columns 
names(cell.format) <- seq(1, dim(state.x77)[2], by = 1) # assign names to list elements 
addDataFrame(state.x77, sheet, startRow=2, startColumn=1, 
      colStyle = cell.format, 
      colnamesStyle = TABLE_COLNAMES_STYLE, 
      rownamesStyle = TABLE_ROWNAMES_STYLE 
) 

# Change column width to auto 
autoSizeColumn(sheet, colIndex=c(1:ncol(state.x77))) 

#try to make cell A2 blue (as it's not included in the col name style) 
#rows <- createRow(sheet,rowIndex=2) #update 3 
#extracell <- createCell(rows, colIndex=1) #update 3 
#setCellStyle(extracell[[1,1]], TABLE_COLNAMES_STYLE) #update 3 

#######SOLUTION####### 
rows <- getRows(sheet) 
cells <- getCells(rows) 
setCellStyle(cells[[2]], TABLE_COLNAMES_STYLE) 

#merge header 
addMergedRegion(sheet, 1, 1, 1, 9) 


# Save the workbook to a file... 
saveWorkbook(wb, "h:/r-xlsx-report-example.xlsx") 

UPDATE 2: Ho ottenuto tutto il problema facendo solo i rownames una nuova variabile, e poi non la visualizzazione dei rownames. Ma se qualcuno potesse ancora spiegare come fare riferimento a una singola cella, sarebbe fantastico!

+1

Forse puoi chiarire qual è il problema concreto. C'è un errore quando chiami il codice? Dato che hai menzionato qualcosa sulla prima colonna, gli indici nel POI sono basati su 0. Potrebbe essere questo il problema? – bdecaf

+1

Non importa sugli indici. Il pacchetto si prende cura di esso. ma 'cell.A2 <- createCell (rowIndex = 1, colIndex = 1)' fornisce un errore sulla mia macchina. – bdecaf

+0

Questo crea un errore: – Kath05

risposta

2

È possibile fare riferimento a una singola cella come questo

rows <- getRows(sheet) 
cells <- getCells(rows) 

#Then apply a style to a cell by referencing it's number as a java? object: 
setCellStyle(cells[[37]], TABLE_LASTROW_STYLE) 

#You could apply it to a row like this: 
lapply(c(37:43), function(i) setCellStyle(cells[[i]], TABLE_LASTROW_STYLE)) 

#You could apply it more generically to your last row like this: 
lapply(c((dim(df)[1]*dim(df)[2] + 2):(dim(df)[1]*dim(df)[2] + 2 + dim(df)[2] -1)), function(i) setCellStyle(cells[[i]], TABLE_LASTROW_STYLE)) 

io aggiornare il codice qui sopra per un esempio più specifico.

Problemi correlati