Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R读取excel colorinfo_R_Excel_Xlconnect_Readxl_R Xlsx - Fatal编程技术网

使用R读取excel colorinfo

使用R读取excel colorinfo,r,excel,xlconnect,readxl,r-xlsx,R,Excel,Xlconnect,Readxl,R Xlsx,有没有办法用R从excel文件中读取单元格的颜色索引 虽然我可以使用诸如XLConnect或XLSX之类的软件包设置单元格颜色,但我没有找到从现有工作簿中提取颜色信息的方法。R-Bloggers提供了一个功能,可以为您完成这项工作。我把答案写在这里以备将来参考 使用xlsx软件包读取excel文件: library(xlsx) wb <- loadWorkbook("test.xlsx") sheet1 <- getSheets(wb)[[1]] # get all row

有没有办法用R从excel文件中读取单元格的颜色索引


虽然我可以使用诸如
XLConnect
XLSX
之类的软件包设置单元格颜色,但我没有找到从现有工作簿中提取颜色信息的方法。

R-Bloggers提供了一个功能,可以为您完成这项工作。我把答案写在这里以备将来参考

使用
xlsx
软件包读取excel文件:

library(xlsx)
wb     <- loadWorkbook("test.xlsx")
sheet1 <- getSheets(wb)[[1]]

# get all rows
rows  <- getRows(sheet1)
cells <- getCells(rows)
错误
将处理没有背景颜色的单元格

使用
sapply
可以获得所有单元格的背景色:

styles <- sapply(cells, getCellStyle) #This will get the styles
sapply(styles, cellColor)
您还可以通过了解RGb代码对其进行分类/识别:

mycolor <- list(green = "00ff00", red = "ff0000")
m     <- match(sapply(styles, cellColor), mycolor)
labs  <-names(mycolor)[m]

mycolor这是一个老问题,但也许它可以帮助将来的人

POI(java)库中有一个奇怪的行为(至少在我的计算机上)。它没有得到正确的颜色。的答案中提供的代码在颜色为基本颜色(索引颜色)时运行良好,但在颜色为(例如)灰度时不起作用。您可以使用
getTint()
函数使用以下代码。色调是一个介于-1(暗)和1(亮)之间的数字,将其与RGB(
getRgb()
)函数相结合,可以完全恢复颜色

cell_color <- function(style){
  fg  <- style$getFillForegroundXSSFColor()

  hex <- tryCatch(fg$getRgb(), error = function(e) NULL)
  hex <- paste0("#", paste(hex, collapse = ""))
  tint <- tryCatch(fg$getTint(), error = function(e) NULL)

  if(!is.null(tint) & !is.null(hex)){   # Tint varies between -1 (dark) and 1 (light)
    rgb_col <- col2rgb(col = hex)

    if(tint < 0) rgb_col <- (1-abs(tint))*rgb_col
    if(tint > 0) rgb_col <- rgb_col + (255-rgb_col)*tint

    hex <- rgb(red = rgb_col[1, 1], 
               green = rgb_col[2, 1], 
               blue = rgb_col[3, 1], 
               maxColorValue = 255)
  }

  return(hex)
}

单元格颜色根据Excel:R:83、G:141、B:213转换为:#538DD5。。。但是,函数返回#F4F3EC。请告知。谢谢
cell_color <- function(style){
  fg  <- style$getFillForegroundXSSFColor()

  hex <- tryCatch(fg$getRgb(), error = function(e) NULL)
  hex <- paste0("#", paste(hex, collapse = ""))
  tint <- tryCatch(fg$getTint(), error = function(e) NULL)

  if(!is.null(tint) & !is.null(hex)){   # Tint varies between -1 (dark) and 1 (light)
    rgb_col <- col2rgb(col = hex)

    if(tint < 0) rgb_col <- (1-abs(tint))*rgb_col
    if(tint > 0) rgb_col <- rgb_col + (255-rgb_col)*tint

    hex <- rgb(red = rgb_col[1, 1], 
               green = rgb_col[2, 1], 
               blue = rgb_col[3, 1], 
               maxColorValue = 255)
  }

  return(hex)
}