我正在用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