1

我有一个 excel 工作簿,其中的列包含标志(更新与否),它查看两个相邻的列并检查两个单元格是否包含相同的数据点。如果不是,它在第​​一个单元格中被标记为“更新”,我想突出显示与“更新”单元格相邻的下两个单元格。

工作示例

library(openxlsx)

animals <- c("Cat","Mice","Dog","Bird","Cat","Cat")
colors <- c("Grey","White","Black","Blue","Black","Orange")
store <- c("Pet Shop","Online","Pets Mart","Walmart","Online","Pets Mart")

df <- cbind(animals,colors,store)

setwd('C:/Users/WORKING DIRECTORY')

write.xlsx(df, file ="test_test.xlsx",row.names = FALSE, sheetName= "Data")

openxlsx 代码以突出显示文本值

df <- loadWorkbook(file = "test_test.xlsx")

highlighting <- createStyle(bgFill = "#fff400")

conditionalFormatting(df, "Data", cols = 1:3, rows = 2:7, type = "contains", rule = "A2=Cat", style = highlighting)

saveWorkbook(df, file = "Test_Output.xlsx", overwrite =  TRUE)

在上面的代码中,我试图在 A 列中突出显示包含“Cat”的任何单元格,然后在 B 列和 C 列中以黄色突出显示接下来的两个相邻单元格。

与原始问题一样,我将在整个电子表格中有很多列,因此帮助我遍历所有特定的“更新”标志列将是一个奖励。我基本上必须在整个excel工作簿中突出显示“更新”文本旁边的相邻单元格(即B10 =“更新”然后突出显示C10和D10,F30 =“更新”然后突出显示G30和H30,等等)

非常感谢您的帮助!

4

0 回答 0