我正在用xlsx
軟件包創建一個格式有點複雜的Excel工作表。xlsx R軟件包覆蓋以前的格式
問題是當我已經格式化了一個單元格並且想要在其上添加一些內容時 - 那麼格式化會回到默認值,除了我添加的新東西。
一個解決方案是指定每個不同的情況並將完整的格式應用於它。特定案件的數量可能會隨着大型表單失控。
我猜想必須有一步一步地添加格式,但還沒有在文檔中找到任何關於它的內容。
我目前的做事方式重複的例子:
require(xlsx)
# Some random data
n <- 20L
set.seed(1L)
df <- data.frame(species = sample(c("Cat", "Dog", "Unkown"), n, replace = TRUE),
speed = abs(rnorm(n)) * 20L)
# Create workbook
dfwb <- createWorkbook(type = "xlsx")
sheet <- createSheet(dfwb, sheetName = "ani")
addDataFrame(df, sheet, startRow = 1, startColumn = 1, row.names = FALSE)
# Change text of Cat to "red"
row <- getRows(sheet, rowIndex = which(df[, "species"] == "Cat") + 1L)
cel <- getCells(row, colIndex = 1)
redh_style <- CellStyle(dfwb) + Font(dfwb, color = "red")
for (i in names(cel)) {
setCellStyle(cel[[i]], redh_style)
}
# Highlight all rows where speed exceeds 18
row <- getRows(sheet, rowIndex = which(df[, "speed"] > 18) + 1L)
cel <- getCells(row, colIndex = 1:2)
high_style <- CellStyle(dfwb) + Fill(foregroundColor="#E2E6EB")
for (i in names(cel)) {
setCellStyle(cel[[i]], high_style)
}
# Save
setwd("c:/temp/csvm/")
saveWorkbook(dfwb, "so_cat.xlsx")
最後,一些先前紅色字體的是回黑色。
詩篇。我已嘗試其他包,但想堅持xlsx
。 XLConnect
不允許直接從R中進行某些類型的格式化,並且存在使openxlsx
運行的技術困難。
尼斯。必須學習'減少'。我現在將這樣執行我的工作,但我會等待,看看在接受之前是否有更方便用戶的東西。 – snoram