突出显示整行而不是单元格

Highlight entire row instead of cell

下面这段代码在Count大于32时高亮单元格,我要的是整行高亮

df = data.frame(Year = c(2018,2019,2020,2018,2019,2020,2018,2019,2020),
                Country = c("Germany","Germany","Germany", "Japan", "Japan", "Japan",  "Thailand", "Thailand", "Thailand"), 
                Count = c(17, 15, 60, 23, 25, 60, 50, 18, 31))

wb = createWorkbook()
addWorksheet(wb, "Master")
writeDataTable(wb, "Master", df, tableStyle = "TableStyleLight9")

yellow_style = createStyle(fgFill = "#FFFF00")
y = which(colnames(df) == "Count")
x = which(abs(df$Count) > 32)
addStyle(wb, sheet = "Master", style = yellow_style, rows = x+1, col = y)
saveWorkbook(wb, "Master.xlsx", overwrite = TRUE)

要突出显示特定列之前的行,请使用 1:n 索引:

library(openxlsx)

wb = createWorkbook()
addWorksheet(wb, "Master")
writeDataTable(wb, "Master", df, tableStyle = "TableStyleLight9")

yellow_style = createStyle(fgFill = "#FFFF00")

x = which(abs(df$Count) > 32) + 1
y = 1:which(colnames(df) == "Count") # <==== here, 1 to up to column "count"

addStyle(wb, sheet = "Master", style = yellow_style,
         rows = x, col = y, gridExpand = TRUE)

saveWorkbook(wb, "Master.xlsx", overwrite = TRUE)


要突出显示整行,请为列使用一些大数字:

wb = createWorkbook()
addWorksheet(wb, "Master")
writeDataTable(wb, "Master", df, tableStyle = "TableStyleLight9")

yellow_style = createStyle(fgFill = "#FFFF00")

x = which(abs(df$Count) > 32) + 1
y = which(colnames(df) == "Count"):10 # <==== here, I am using 10 as example

addStyle(wb, sheet = "Master", style = yellow_style,
         rows = x, col = y, gridExpand = TRUE)

saveWorkbook(wb, "Master.xlsx", overwrite = TRUE)

GitHub #439

有一个开放的功能请求