Skip to content

Commit

Permalink
rename to wb_set_cell_style_across()
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Dec 13, 2023
1 parent 379b11e commit 941a5a7
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 155 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 18 additions & 22 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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.
Expand Down
106 changes: 57 additions & 49 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -7158,14 +7110,70 @@ 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
dims <- unname(unlist(dims))

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)
},
Expand Down
1 change: 0 additions & 1 deletion man/base_font-wb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 31 additions & 32 deletions man/wbWorkbook.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/wb_add_dxfs_style.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/wb_add_style.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions man/wb_add_style_across.Rd

This file was deleted.

Loading

0 comments on commit 941a5a7

Please sign in to comment.