2017-03-09 28 views
2

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

最后,一些先前红色字体的是回黑色。

enter image description here

诗篇。我已尝试其他包,但想坚持xlsxXLConnect不允许直接从R中进行某些类型的格式化,并且存在使openxlsx运行的技术困难。

回答

1

下面是一种方法。主要思想是为每个单元格建立一个并行的格式列表,其中每个列表元素都是一个单元格。这允许您根据需要追加格式化属性。最后,我们将这个格式列表应用到每个单元格。

首先,我们建立了一个空的列表:

# Set up blank list of formats 
fmts <- list() 

现在,我们根据第一标准的格式,将所述字体属性到fmts列表选择的细胞:

# Change text of Cat to "red" 
row <- getRows(sheet, rowIndex = which(df[, "species"] == "Cat") + 1L) 
cel <- getCells(row, colIndex = 1) 

for (i in names(cel)) { 
    if (i %in% names(fmts)) { 
    fmts[[i]] <- c(fmts[[i]], list(Font(dfwb, color = "red"))) 
    } else { 
    fmts[[i]] <- list(CellStyle(dfwb), Font(dfwb, color = "red")) 
    } 
} 

下一页,做后台:

# Highlight all rows where speed exceeds 18 
row <- getRows(sheet, rowIndex = which(df[, "speed"] > 18) + 1L) 
cel <- getCells(row, colIndex = 1:2) 

for (i in names(cel)) { 
    if (i %in% names(fmts)) { 
    fmts[[i]] <- c(fmts[[i]], list(Fill(foregroundColor="#E2E6EB"))) 
    } else { 
    fmts[[i]] <- list(CellStyle(dfwb), Fill(foregroundColor="#E2E6EB")) 
    } 
} 

当我们检查fmts,我们注意到,一些要素只有两个项目(基本单元的风格,再加上字体或背景),有些有三个(基本单元格样式,字体和背景):

str(fmts, m = 1) 
# List of 16 
# $ 2.1 :List of 3 
# $ 6.1 :List of 3 
# $ 11.1:List of 2 
# $ 12.1:List of 3 
# $ 13.1:List of 2 
# $ 2.2 :List of 2 
# $ 5.1 :List of 2 
# $ 5.2 :List of 2 
# $ 6.2 :List of 2 
# $ 9.1 :List of 2 
# $ 9.2 :List of 2 
# $ 12.2:List of 2 
# $ 15.1:List of 2 
# $ 15.2:List of 2 
# $ 19.1:List of 2 
# $ 19.2:List of 2 

最后,我们遍历fmts并应用样式。该Reduce功能进来有用:

# Apply formatting 
for (i in names(fmts)) { 
    idx <- as.numeric(unlist(strsplit(i, "\\."))) 
    cel <- getCells(getRows(sheet, rowIndex = idx[1]), colIndex = idx[2]) 
    setCellStyle(cel[[i]], 
    Reduce(`+.CellStyle`, fmts[[i]]) 
) 
} 

输出:

enter image description here

+0

尼斯。必须学习'减少'。我现在将这样执行我的工作,但我会等待,看看在接受之前是否有更方便用户的东西。 – snoram