|
| 1 | +#' @title Writes an Excel file with the selection list. |
| 2 | +#' |
| 3 | +#' @description The selection list based on selected data from okplan file and |
| 4 | +#' uses standardize_columns to select, format and style columns. |
| 5 | +#' |
| 6 | +#' @param data The data with units that should be tested. |
| 7 | +#' @param filename The name of the Excel file that should be written. |
| 8 | +#' @param filepath The path to the Excel file that should be written. |
| 9 | +#' @param sheet The name of the Excel sheet with the list. |
| 10 | +#' @param calculate_sum \[logical\] Should a line with the sum be appended. Defaults to TRUE. |
| 11 | +#' @param dbsource The name of the dbtable in OK_column_standards that should |
| 12 | +#' be used for standardizing the columns. |
| 13 | +#' @export |
| 14 | + |
| 15 | + |
| 16 | +write_ok_selection_list <- function(data, |
| 17 | + sheet, |
| 18 | + filename, |
| 19 | + filepath, |
| 20 | + calculate_sum = TRUE, |
| 21 | + dbsource) { |
| 22 | + # ARGUMENT CHECKING ---- |
| 23 | + # Object to store check-results |
| 24 | + checks <- checkmate::makeAssertCollection() |
| 25 | + |
| 26 | + # Perform checks |
| 27 | + # for (i in 1:length(data)) { |
| 28 | + checkmate::assert_data_frame(data, max.rows = (1048576 - 1), max.cols = 16384, add = checks) |
| 29 | + # } |
| 30 | + checkmate::assert_character(sheet, min.chars = 1, min.len = 1, max.len = length(data), unique = TRUE, add = checks) |
| 31 | + checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks) |
| 32 | + checkmate::assert_directory_exists(filepath, add = checks) |
| 33 | + checkmate::assert_logical(calculate_sum, any.missing = FALSE, min.len = 1, add = checks) |
| 34 | + checkmate::assert_character(dbsource, min.len = 1, add = checks) |
| 35 | + checkmate::assert_choice(dbsource, |
| 36 | + choices = unique(OKplan::OK_column_standards[, "table_db"]), |
| 37 | + add = checks) |
| 38 | + |
| 39 | + # Report check-results |
| 40 | + checkmate::reportAssertions(checks) |
| 41 | + |
| 42 | + # GENERATE EXCEL WORKBOOK ---- |
| 43 | + okwb <- openxlsx::createWorkbook() |
| 44 | + |
| 45 | + # for (i in 1:length(data)) { |
| 46 | + # i <- 1 |
| 47 | + # STANDARDIZE COLUMNS ---- |
| 48 | + # column names |
| 49 | + okdata <- NVIdb::standardize_columns(data, |
| 50 | + standards = OKplan::OK_column_standards, |
| 51 | + dbsource = dbsource, |
| 52 | + property = "colnames") |
| 53 | + |
| 54 | + # order columns and keep only designated columns |
| 55 | + okdata <- NVIdb::standardize_columns(data = okdata, |
| 56 | + standards = OKplan::OK_column_standards, |
| 57 | + dbsource = dbsource, |
| 58 | + property = "colorder", exclude = TRUE) |
| 59 | + |
| 60 | + # INCLUDE EXTRA INFORMATION ---- |
| 61 | + # Append sum |
| 62 | + if(isTRUE(calculate_sum)) { |
| 63 | + okdata <- append_sum_line(data = okdata, column = c("ant_prover"), position = "left") |
| 64 | + } |
| 65 | + |
| 66 | + # Append date generated |
| 67 | + okdata <- append_date_generated_line(okdata) |
| 68 | + |
| 69 | + |
| 70 | + # STYLE EXCEL SHEET ---- |
| 71 | + NVIpretty::add_formatted_worksheet(data = okdata, |
| 72 | + workbook = okwb, |
| 73 | + sheet = sheet, |
| 74 | + wrapHeadlineText = TRUE, |
| 75 | + collabels = TRUE, |
| 76 | + colwidths = TRUE, |
| 77 | + standards = OKplan::OK_column_standards, |
| 78 | + dbsource = dbsource) |
| 79 | + |
| 80 | + |
| 81 | + if(isTRUE(calculate_sum)) { |
| 82 | + style_sum_line(workbook = okwb, sheet = sheet, data = okdata) |
| 83 | + } |
| 84 | + # } |
| 85 | + # SAVE EXCEL WORKBOOK ---- |
| 86 | + openxlsx::saveWorkbook(wb = okwb, file = paste0(filepath, filename), overwrite = TRUE) |
| 87 | + |
| 88 | +} |
0 commit comments