-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathErrorHandler.R
More file actions
42 lines (35 loc) · 1.15 KB
/
ErrorHandler.R
File metadata and controls
42 lines (35 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
library(openxlsx)
highlight_csv_to_xlsx <- function(df, highlight_entries, output_file) {
wb <- createWorkbook()
sheet_name <- "Error Based"
addWorksheet(wb, sheet_name)
writeData(wb, sheet = sheet_name, x = df)
highlight_style <- createStyle(fgFill = "yellow")
i <- 1
while (i <= length(highlight_entries) - 1) {
column_name <- highlight_entries[[i]]
invalid_value <- highlight_entries[[i+1]]
col_index <- which(names(df) == column_name)
if (length(col_index) == 0) {
stop(sprintf("Column '%s' not found in the dataframe. Available columns: %s",
column_name, paste(names(df), collapse = ", ")))
}
row_indices <- which(df[[col_index]] %in% invalid_value)
if (length(row_indices) > 0) {
addStyle(
wb,
sheet = sheet_name,
style = highlight_style,
rows = row_indices + 1,
cols = col_index,
gridExpand = TRUE,
stack = TRUE
)
} else {
warning(sprintf("No rows match the highlight criteria for column '%s'.", column_name))
}
i <- i + 2
}
return(wb)
saveWorkbook(wb, output_file, overwrite = TRUE)
}