diff --git a/NAMESPACE b/NAMESPACE index 1fef5baee..ed1396dde 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -69,7 +69,6 @@ export(wb_add_plot) export(wb_add_slicer) export(wb_add_sparklines) export(wb_add_style) -export(wb_add_style_across) export(wb_add_thread) export(wb_add_worksheet) export(wb_clean_sheet) @@ -117,6 +116,7 @@ export(wb_set_active_sheet) export(wb_set_base_font) export(wb_set_bookview) export(wb_set_cell_style) +export(wb_set_cell_style_across) export(wb_set_col_widths) export(wb_set_creators) export(wb_set_grid_lines) diff --git a/R/class-workbook-wrappers.R b/R/class-workbook-wrappers.R index 1315daa68..092dbe056 100644 --- a/R/class-workbook-wrappers.R +++ b/R/class-workbook-wrappers.R @@ -2627,27 +2627,6 @@ wb_add_style <- function(wb, style = NULL, style_name = NULL) { wb$clone()$add_style(style, style_name) } -#' Set the column / row style in a worksheet -#' -#' Apply the style from one cell across columns/rows of the worksheet. This affects only empty cells. -#' -#' @param wb A workbook -#' @param sheet A worksheet -#' @param from The cell containing the style that should be applied column-wise/row-wise -#' @param cols The columns the style will be applied to, either "A:D" or 1:4 -#' @param rows The rows the style will be applied to -#' @family workbook styling functions -#' @examples -#' wb <- wb_workbook() %>% -#' wb_add_worksheet() %>% -#' wb_add_fill(dims = "C3", color = wb_color("yellow")) %>% -#' wb_add_style_across(from = "C3", cols = "C:D", rows = 3:4) -#' @export -wb_add_style_across <- function(wb, sheet = current_sheet(), from = "A1", cols = NULL, rows = NULL) { - assert_workbook(wb) - wb$clone()$add_style_across(sheet = sheet, from = from, cols = cols, rows = rows) -} - #' Apply styling to a cell region #' #' @name wb_cell_style @@ -2673,7 +2652,7 @@ wb_get_cell_style <- function(wb, sheet = current_sheet(), dims) { } #' @rdname wb_cell_style -#' @param style A style +#' @param style A style or a cell with a certain style #' @return wb_set_cell_style returns the workbook invisibly. #' @export wb_set_cell_style <- function(wb, sheet = current_sheet(), dims, style) { @@ -2682,6 +2661,23 @@ wb_set_cell_style <- function(wb, sheet = current_sheet(), dims, style) { wb$clone(deep = TRUE)$set_cell_style(sheet, dims, style) } +#' @rdname wb_cell_style +#' @param wb A workbook +#' @param sheet A worksheet +#' @param style A style or a cell with a certain style +#' @param cols The columns the style will be applied to, either "A:D" or 1:4 +#' @param rows The rows the style will be applied to +#' @examples +#' wb <- wb_workbook() %>% +#' wb_add_worksheet() %>% +#' wb_add_fill(dims = "C3", color = wb_color("yellow")) %>% +#' wb_set_cell_style_across(style = "C3", cols = "C:D", rows = 3:4) +#' @export +wb_set_cell_style_across <- function(wb, sheet = current_sheet(), style, cols = NULL, rows = NULL) { + assert_workbook(wb) + wb$clone(deep = TRUE)$set_cell_style_across(sheet = sheet, style = style, cols = cols, rows = rows) +} + #' Modify borders in a cell region of a worksheet #' #' wb wrapper to create borders for cell regions. diff --git a/R/class-workbook.R b/R/class-workbook.R index 83ed798e6..79d125fb4 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -1832,54 +1832,6 @@ wbWorkbook <- R6::R6Class( invisible(self) }, - #' @description add style across columns and/or rows - #' @param sheet sheet - #' @param from cell - #' @param cols cols - #' @param rows rows - #' @return The `wbWorkbook` object - add_style_across = function(sheet = current_sheet(), from = "A1", cols = NULL, rows = NULL) { - - sheet <- private$get_sheet_index(sheet) - styid <- self$get_cell_style(dims = from, sheet = sheet) - - if (!is.null(rows)) { - if (is.character(rows)) # row2int - rows <- as.integer(dims_to_rowcol(rows)[[2]]) - - dims <- wb_dims(rows, "A") - cells <- unname(unlist(dims_to_dataframe(dims, fill = TRUE))) - cc <- self$worksheets[[sheet]]$sheet_data$cc - - cells <- cells[!cells %in% cc$r] - if (length(cells) > 0) { - private$do_cell_init(sheet, dims) - self$set_cell_style(sheet = sheet, dims = cells, style = styid) - } - - rows_df <- self$worksheets[[sheet]]$sheet_data$row_attr - sel <- rows_df$r %in% as.character(rows) - - rows_df$customFormat[sel] <- "1" - rows_df$s[sel] <- styid - self$worksheets[[sheet]]$sheet_data$row_attr <- rows_df - - } - - if (!is.null(cols)) { - - cols <- col2int(cols) - - cols_df <- wb_create_columns(self, sheet, cols) - sel <- cols_df$min %in% as.character(cols) - cols_df$style[sel] <- styid - self$worksheets[[sheet]]$fold_cols(cols_df) - - } - - invisible(self) - }, - ### to dataframe ---- #' @description to_df #' @param sheet Either sheet name or index. When missing the first sheet in the workbook is selected. @@ -7158,6 +7110,11 @@ wbWorkbook <- R6::R6Class( dims <- dims_to_dataframe(dims, fill = TRUE) sheet <- private$get_sheet_index(sheet) + if (grepl(letters, tolower(style))) + styid <- self$get_cell_style(dims = style, sheet = sheet) + else + styid <- style + private$do_cell_init(sheet, dims) # if a range is passed (e.g. "A1:B2") we need to get every cell @@ -7165,7 +7122,58 @@ wbWorkbook <- R6::R6Class( sel <- self$worksheets[[sheet]]$sheet_data$cc$r %in% dims - self$worksheets[[sheet]]$sheet_data$cc$c_s[sel] <- style + self$worksheets[[sheet]]$sheet_data$cc$c_s[sel] <- styid + + invisible(self) + }, + + #' @description set style across columns and/or rows + #' @param sheet sheet + #' @param style style + #' @param cols cols + #' @param rows rows + #' @return The `wbWorkbook` object + set_cell_style_across = function(sheet = current_sheet(), style, cols = NULL, rows = NULL) { + + sheet <- private$get_sheet_index(sheet) + if (grepl(letters, tolower(style))) + styid <- self$get_cell_style(dims = style, sheet = sheet) + else + styid <- style + + if (!is.null(rows)) { + if (is.character(rows)) # row2int + rows <- as.integer(dims_to_rowcol(rows)[[2]]) + + dims <- wb_dims(rows, "A") + cells <- unname(unlist(dims_to_dataframe(dims, fill = TRUE))) + cc <- self$worksheets[[sheet]]$sheet_data$cc + + cells <- cells[!cells %in% cc$r] + if (length(cells) > 0) { + private$do_cell_init(sheet, dims) + self$set_cell_style(sheet = sheet, dims = cells, style = styid) + } + + rows_df <- self$worksheets[[sheet]]$sheet_data$row_attr + sel <- rows_df$r %in% as.character(rows) + + rows_df$customFormat[sel] <- "1" + rows_df$s[sel] <- styid + self$worksheets[[sheet]]$sheet_data$row_attr <- rows_df + + } + + if (!is.null(cols)) { + + cols <- col2int(cols) + + cols_df <- wb_create_columns(self, sheet, cols) + sel <- cols_df$min %in% as.character(cols) + cols_df$style[sel] <- styid + self$worksheets[[sheet]]$fold_cols(cols_df) + + } invisible(self) }, diff --git a/man/base_font-wb.Rd b/man/base_font-wb.Rd index 7488bbe02..6baa8095a 100644 --- a/man/base_font-wb.Rd +++ b/man/base_font-wb.Rd @@ -54,7 +54,6 @@ wb_get_base_font(wb) \seealso{ Other workbook styling functions: \code{\link{wb_add_dxfs_style}()}, -\code{\link{wb_add_style_across}()}, \code{\link{wb_add_style}()} Other workbook wrappers: diff --git a/man/wbWorkbook.Rd b/man/wbWorkbook.Rd index 702ecfcea..9ea568ebf 100644 --- a/man/wbWorkbook.Rd +++ b/man/wbWorkbook.Rd @@ -116,7 +116,6 @@ worksheet names.} \item \href{#method-wbWorkbook-add_slicer}{\code{wbWorkbook$add_slicer()}} \item \href{#method-wbWorkbook-add_formula}{\code{wbWorkbook$add_formula()}} \item \href{#method-wbWorkbook-add_style}{\code{wbWorkbook$add_style()}} -\item \href{#method-wbWorkbook-add_style_across}{\code{wbWorkbook$add_style_across()}} \item \href{#method-wbWorkbook-to_df}{\code{wbWorkbook$to_df()}} \item \href{#method-wbWorkbook-load}{\code{wbWorkbook$load()}} \item \href{#method-wbWorkbook-save}{\code{wbWorkbook$save()}} @@ -185,6 +184,7 @@ worksheet names.} \item \href{#method-wbWorkbook-add_cell_style}{\code{wbWorkbook$add_cell_style()}} \item \href{#method-wbWorkbook-get_cell_style}{\code{wbWorkbook$get_cell_style()}} \item \href{#method-wbWorkbook-set_cell_style}{\code{wbWorkbook$set_cell_style()}} +\item \href{#method-wbWorkbook-set_cell_style_across}{\code{wbWorkbook$set_cell_style_across()}} \item \href{#method-wbWorkbook-add_named_style}{\code{wbWorkbook$add_named_style()}} \item \href{#method-wbWorkbook-add_dxfs_style}{\code{wbWorkbook$add_dxfs_style()}} \item \href{#method-wbWorkbook-clone_sheet_style}{\code{wbWorkbook$clone_sheet_style()}} @@ -749,37 +749,6 @@ The \code{wbWorkbook} object } } \if{html}{\out{