openxlsx-根据另一列中的文本填充行

时间:2019-04-10 15:01:37

标签: r openxlsx

我正在尝试根据该行的一列中的文本用openxlsx填充excel工作簿中的一行。例如,如果单元格C3包含A,则突出显示第3行。

理想情况下,它不是conditionalFormatting。我希望它是基于单元格中文本的行的实际颜色,但是conditionalFormatting似乎是唯一的选择。如果我能够将其设置为实际的颜色,请告诉我。

下面的代码显示,您可以根据该行中单元格的value突出显示一行,但是如果您尝试使用文本,则不能。另外,如果将type设置为contains,则它似乎不接受任何样式。

wb <- createWorkbook()

addWorksheet(wb, "Dependent on")
addWorksheet(wb, "containsText")


negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
posStyle <- createStyle(fontColour = "#006100", bgFill = "#C6EFCE")


## highlight row based on value in column 2
writeData(wb, "Dependent on", data.frame(x = 1:10, y = runif(10)), startRow = 15)
conditionalFormatting(wb, "Dependent on", cols=1:2, rows=16:25, rule="$B16<0.5", style = negStyle)
conditionalFormatting(wb, "Dependent on", cols=1:2, rows=16:25, rule="$B16>=0.5", style = posStyle)


## cells containing text
fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
writeData(wb, "containsText", data.frame(letters = sapply(1:10, fn), numbers = 1:10))
conditionalFormatting(wb, "containsText", cols = 1:2, rows = 1:10, type = "contains", rule = "A")


openXL(wb) ## opens a temp version

2 个答案:

答案 0 :(得分:2)

您似乎在为要根据不同列中的值对列应用条件格式而苦恼(这是type = "expression"时的已记录功能)但不是type = "contains"时),而是希望该值是文本而不是数字。

不过,type = "expression"允许的表达式可以包含文本值!如果您实际上确实需要根据包含 文本的另一列来有条件地突出显示,则此方法将不起作用,但是如果您仅需要另一列来完全匹配文本,则无法使用。

library(tidyverse)
library(openxlsx)

# prepare test data
test_data <-
  mtcars %>%
  rownames_to_column() %>%
  as_tibble() %>%
  select(rowname, cyl) %>%
  mutate(first_letter = substr(rowname, 1, 1))

# load the data into a few worksheets
test_wb <- createWorkbook()
addWorksheet(test_wb, 'by_number')
addWorksheet(test_wb, 'by_letter')
addWorksheet(test_wb, 'by_word')
writeData(test_wb, 'by_number', test_data)
writeData(test_wb, 'by_letter', test_data)
writeData(test_wb, 'by_word', test_data)

# create the test style
flagged_style <- createStyle(
  fontColour = '#274e13', bgFill = '#b6d7a8')
all_rows <- 2:(nrow(test_data) + 1)

Test data based on the mtcars dataset

以下是三个示例:

  1. 根据cyl的值(不等式)有条件地格式化行
  2. 根据first_letter(等于)的值有条件地设置行的格式
  3. 根据rowname(等于)的值有条件地设置行的格式
# apply formatting...

# ... by numeric value (inqueality)
conditionalFormatting(test_wb, 'by_number',
  cols = 1:3, rows = all_rows, rule = '$B2<=6',
  style = flagged_style)

# ... by text equality (letter)
conditionalFormatting(test_wb, 'by_letter',
  cols = 1:3, rows = all_rows, rule = '$C2=="M"',
  style = flagged_style)

# ... by text equality (word)
conditionalFormatting(test_wb, 'by_word',
  cols = 1:3, rows = all_rows, rule = '$A2=="Merc 230"',
  style = flagged_style)

saveWorkbook(test_wb, 'test.xlsx')

以下是结果(我将它们彼此粘贴在一起):

enter image description here

如果确实需要“包含”规则,而不是严格相等,则可能需要使用虚拟列。不幸的是,我认为您将无法删除该格式,因为条件格式会作为规则存储在Excel工作簿中,而不是在您saveWorkbook()时被“烘焙”。

答案 1 :(得分:0)

这是您期望的结果吗?

library(openxlsx)


fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
a <- data.frame(letters = sapply(1:10, fn), numbers = 1:10)

wb <- createWorkbook()

addWorksheet(wb, "containsText")
writeData(wb = wb, sheet = "containsText", x = a)
color <- createStyle(fgFill = "#00CCFF") #BLUE
addStyle(wb = wb, sheet = "containsText", style = color, rows = which(grepl(a$letters, pattern = "A")) + 1, cols = 1)
saveWorkbook(wb = wb, file = "test_color.xlsx", overwrite = TRUE)