openxlsx - 根据另一列中的文本填充行行
openxlsx - fill row row based on text in another column
我正在尝试根据 excel 工作簿中某一行的某一列中的文本,用 openxlsx
填充该行。例如,如果单元格 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
这是您期待的结果吗?
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)
您似乎正在努力根据 different 列中的值将条件格式应用于列(这是一个记录的功能,当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)
这里有三个例子:
- 根据
cyl
的值有条件地格式化行(不等式)
- 根据
first_letter
的值(相等)有条件地格式化行
- 根据
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')
这是结果(我把它们贴在一起了):
如果您确实需要 "contains" 规则,而不是严格相等,您可能需要使用虚拟列。不幸的是,我认为您无法在应用格式后将其删除,因为条件格式通常存储在 Excel 工作簿中,而不是 "baked in" 当您 saveWorkbook()
.
我正在尝试根据 excel 工作簿中某一行的某一列中的文本,用 openxlsx
填充该行。例如,如果单元格 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
这是您期待的结果吗?
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)
您似乎正在努力根据 different 列中的值将条件格式应用于列(这是一个记录的功能,当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)
这里有三个例子:
- 根据
cyl
的值有条件地格式化行(不等式) - 根据
first_letter
的值(相等)有条件地格式化行 - 根据
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')
这是结果(我把它们贴在一起了):
如果您确实需要 "contains" 规则,而不是严格相等,您可能需要使用虚拟列。不幸的是,我认为您无法在应用格式后将其删除,因为条件格式通常存储在 Excel 工作簿中,而不是 "baked in" 当您 saveWorkbook()
.