3

我正在尝试根据该行中的一列中的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
4

2 回答 2

4

这是你期待的结果吗?

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)
于 2019-05-24T07:44:03.567 回答
2

似乎您正在为这样一个事实而苦苦挣扎:您想根据不同列中的值将条件格式应用于列(这是一个记录的功能 whentype = "expression"但不是 when 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)

基于 mtcars 数据集的测试数据

以下是三个例子:

  1. 根据cyl(inequality)的值有条件地格式化行
  2. 根据first_letter(equality)的值有条件地格式化行
  3. 根据rowname(equality)的值有条件地格式化行
# 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')

这是结果(我把它们贴在一起):

在此处输入图像描述

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

于 2020-02-24T05:23:13.900 回答