我有一本excel工作簿,其中的列包含标志(更新vs.否),该标志查看两个相邻的列并检查两个单元格是否包含相同的数据点。如果没有,则在第一个单元格中将其标记为“更新”,我想突出显示与“更新”单元格相邻的下两个单元格。
工作示例
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列中突出显示任何包含“猫”的单元格,然后在B和C列中以黄色突出显示接下来的两个相邻单元格。
与原始问题一样,整个电子表格中将有很多列,因此帮助我遍历所有特定的“更新”标志列将是一个加分项。我基本上必须在整个excel工作簿中的“更新”文本旁边突出显示相邻的单元格(即B10 =“更新”,然后突出显示C10和D10,F30 =“更新”,然后突出显示G30和H30,依此类推)
非常感谢您的帮助!