【问题标题】:R XLConnect - filtering columns based on column colorR XLConnect - 根据列颜色过滤列
【发布时间】:2017-08-24 20:56:17
【问题描述】:

XLconnect 包(或R 中的任何其他包)中,是否可以读取带有headers 中颜色的Excel 表并根据这些颜色在R 中过滤它们?

例如column headers for A, C & E 是用绿色填充的,在R中读取后,是否可以根据该颜色对其进行过滤?

谢谢

【问题讨论】:

    标签: r excel xlconnect


    【解决方案1】:

    是的,我相信它是: 阅读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

    【讨论】:

    • 我的回答太棒了!
    • 没问题 :-) 请不要忘记选择答案 :-D
    • 很遗憾我的声望不到15!真的很抱歉
    • 开个玩笑,不用担心很高兴为您提供帮助:-)
    • 不正确的@user1703914。由于这是您的帖子,尽管您不能对任何答案进行投票,您可以 select any answer 这对您最有帮助(单击灰色刻度线)。
    猜你喜欢
    • 2023-03-11
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2013-09-06
    • 2021-08-16
    • 1970-01-01
    • 2019-12-23
    相关资源
    最近更新 更多