R XLConnect - 根据列颜色过滤列
R XLConnect - filtering columns based on column color
在 XLconnect
包(或 R
中的任何其他包)中,是否可以读取 Excel
sheet 中带有颜色的 headers
] 并根据这些颜色在 R
中过滤它们?
比如column headers
对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
在 XLconnect
包(或 R
中的任何其他包)中,是否可以读取 Excel
sheet 中带有颜色的 headers
] 并根据这些颜色在 R
中过滤它们?
比如column headers
对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