1

I'm currently reading an Excel file in R, and I am trying to apply a function to all the gray (grey) highlighted cells. Is it possible to read a workbook with R and detect these highlighted cells?

Tom Kim
  • 39
  • 3
  • You should look into the [tidyxl](https://cran.r-project.org/web/packages/tidyxl/tidyxl.pdf) package, specifically the `xlsx_formats` function. It should allow you to read an Excel file with the formatting. – C. Braun Apr 09 '18 at 16:44

1 Answers1

2

Try using the xlsx package. For example:

library(xlsx)
df <- loadWorkbook("test.xlsx")
sheet1 <- getSheets(df)[[1]]
rows  <- getRows(sheet1)
cells <- getCells(rows)

styles <- sapply(cells, getCellStyle)

cellColor <- function(style) {
  fg  <- style$getFillForegroundXSSFColor()
  rgb <- tryCatch(fg$getRgb(), error = function(e) NULL)
  rgb <- paste(rgb, collapse = "")
  return(rgb)

}

myCellColors <- sapply(styles, cellColor). 

Edited from here: https://www.r-bloggers.com/when-life-gives-you-coloured-cells-make-categories/

DJV
  • 4,743
  • 3
  • 19
  • 34