【发布时间】:2017-08-24 20:56:17
【问题描述】:
在XLconnect 包(或R 中的任何其他包)中,是否可以读取带有headers 中颜色的Excel 表并根据这些颜色在R 中过滤它们?
例如column headers for A, C & E 是用绿色填充的,在R中读取后,是否可以根据该颜色对其进行过滤?
谢谢
【问题讨论】:
在XLconnect 包(或R 中的任何其他包)中,是否可以读取带有headers 中颜色的Excel 表并根据这些颜色在R 中过滤它们?
例如column headers for A, C & E 是用绿色填充的,在R中读取后,是否可以根据该颜色对其进行过滤?
谢谢
【问题讨论】:
是的,我相信它是:
阅读R,使用xlsx 包并解压:
library(xlsx)
wb <- loadWorkbook("test.xlsx")
sheet1 <- getSheets(wb)[[1]]
比获取行和单元格:
# get all rows
rows <- getRows(sheet1)
cells <- getCells(rows)
# quick look at the values
sapply(cells, getCellValue)
# 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1 11.1
# "x" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
现在颜色信息在哪里?在cell style:
styles <- sapply(cells, getCellStyle)
现在有一个函数可以为您提供单元格RGB 值:
注意:以下行为单元格提供背景颜色style$getFillForegroundXSSFColor()
cellColor <- function(style) {
fg <- style$getFillForegroundXSSFColor()
rgb <- tryCatch(fg$getRgb(), error = function(e) NULL)
rgb <- paste(rgb, collapse = "")
return(rgb)
}
想了解更多信息吗?去here
【讨论】: