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{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-wbWorkbook-add_style_across}{}}} -\subsection{Method \code{add_style_across()}}{ -add style across columns and/or rows -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{wbWorkbook$add_style_across( - sheet = current_sheet(), - from = "A1", - cols = NULL, - rows = NULL -)}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{sheet}}{sheet} - -\item{\code{from}}{cell} - -\item{\code{cols}}{cols} - -\item{\code{rows}}{rows} -} -\if{html}{\out{
}} -} -\subsection{Returns}{ -The \code{wbWorkbook} object -} -} -\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-wbWorkbook-to_df}{}}} \subsection{Method \code{to_df()}}{ @@ -3020,6 +2989,36 @@ The \code{wbWorksheetObject}, invisibly } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-wbWorkbook-set_cell_style_across}{}}} +\subsection{Method \code{set_cell_style_across()}}{ +set style across columns and/or rows +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{wbWorkbook$set_cell_style_across( + sheet = current_sheet(), + cols = NULL, + rows = NULL +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{sheet}}{sheet} + +\item{\code{cols}}{cols} + +\item{\code{rows}}{rows} + +\item{\code{style}}{style} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +The \code{wbWorkbook} object +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-wbWorkbook-add_named_style}{}}} \subsection{Method \code{add_named_style()}}{ diff --git a/man/wb_add_dxfs_style.Rd b/man/wb_add_dxfs_style.Rd index 81b0140f1..66f8b78a2 100644 --- a/man/wb_add_dxfs_style.Rd +++ b/man/wb_add_dxfs_style.Rd @@ -71,7 +71,6 @@ wb <- wb_workbook() \%>\% \seealso{ Other workbook styling functions: \code{\link{base_font-wb}}, -\code{\link{wb_add_style_across}()}, \code{\link{wb_add_style}()} } \concept{workbook styling functions} diff --git a/man/wb_add_style.Rd b/man/wb_add_style.Rd index 1d829f6c5..d5d498708 100644 --- a/man/wb_add_style.Rd +++ b/man/wb_add_style.Rd @@ -38,7 +38,6 @@ wb <- wb_workbook() \%>\% wb_add_style(yellow) Other workbook styling functions: \code{\link{base_font-wb}}, -\code{\link{wb_add_dxfs_style}()}, -\code{\link{wb_add_style_across}()} +\code{\link{wb_add_dxfs_style}()} } \concept{workbook styling functions} diff --git a/man/wb_add_style_across.Rd b/man/wb_add_style_across.Rd deleted file mode 100644 index b0c7afc05..000000000 --- a/man/wb_add_style_across.Rd +++ /dev/null @@ -1,41 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/class-workbook-wrappers.R -\name{wb_add_style_across} -\alias{wb_add_style_across} -\title{Set the column / row style in a worksheet} -\usage{ -wb_add_style_across( - wb, - sheet = current_sheet(), - from = "A1", - cols = NULL, - rows = NULL -) -} -\arguments{ -\item{wb}{A workbook} - -\item{sheet}{A worksheet} - -\item{from}{The cell containing the style that should be applied column-wise/row-wise} - -\item{cols}{The columns the style will be applied to, either "A:D" or 1:4} - -\item{rows}{The rows the style will be applied to} -} -\description{ -Apply the style from one cell across columns/rows of the worksheet. This affects only empty cells. -} -\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) -} -\seealso{ -Other workbook styling functions: -\code{\link{base_font-wb}}, -\code{\link{wb_add_dxfs_style}()}, -\code{\link{wb_add_style}()} -} -\concept{workbook styling functions} diff --git a/man/wb_cell_style.Rd b/man/wb_cell_style.Rd index 5001ca476..aea41eabb 100644 --- a/man/wb_cell_style.Rd +++ b/man/wb_cell_style.Rd @@ -4,20 +4,33 @@ \alias{wb_cell_style} \alias{wb_get_cell_style} \alias{wb_set_cell_style} +\alias{wb_set_cell_style_across} \title{Apply styling to a cell region} \usage{ wb_get_cell_style(wb, sheet = current_sheet(), dims) wb_set_cell_style(wb, sheet = current_sheet(), dims, style) + +wb_set_cell_style_across( + wb, + sheet = current_sheet(), + style, + cols = NULL, + rows = NULL +) } \arguments{ -\item{wb}{A \code{wbWorkbook} object} +\item{wb}{A workbook} -\item{sheet}{sheet} +\item{sheet}{A worksheet} \item{dims}{A cell range in the worksheet} -\item{style}{A style} +\item{style}{A style or a cell with a certain style} + +\item{cols}{The columns the style will be applied to, either "A:D" or 1:4} + +\item{rows}{The rows the style will be applied to} } \value{ wb_get_cell_style returns the style id as character @@ -37,6 +50,10 @@ numfmt <- wb$get_cell_style(dims = "B1") # assign style to a1 wb$set_cell_style(dims = "A1", style = numfmt) +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) } \seealso{ Other styles: diff --git a/tests/testthat/test-class-workbook-wrappers.R b/tests/testthat/test-class-workbook-wrappers.R index ca81087ed..c23e09566 100644 --- a/tests/testthat/test-class-workbook-wrappers.R +++ b/tests/testthat/test-class-workbook-wrappers.R @@ -645,7 +645,7 @@ test_that("wb_add_style() is a wrapper", { }) -# wb_add_style_across() --------------------------------------------------- +# wb_set_cell_style_across() ---------------------------------------------- test_that("wb_add_style() is a wrapper", { @@ -654,7 +654,7 @@ test_that("wb_add_style() is a wrapper", { wb_add_fill(dims = "C3", color = wb_color("yellow")) expect_wrapper( - "add_style_across", + "set_cell_style_across", wb = wb, params = list(from = "C3", cols = "C:D", rows = 3:4) ) diff --git a/tests/testthat/test-wb_styles.R b/tests/testthat/test-wb_styles.R index 1c2269b6d..3a2bbe35f 100644 --- a/tests/testthat/test-wb_styles.R +++ b/tests/testthat/test-wb_styles.R @@ -842,7 +842,7 @@ test_that("apply styles across columns and rows", { 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) + wb_set_cell_style_across(style = "C3", cols = "C:D", rows = 3:4) exp <- c( "",