Skip to content

Commit 4cff302

Browse files
committed
[formula] add name argument to wb_add_formula()
1 parent 2186798 commit 4cff302

6 files changed

+19
-2
lines changed

R/class-workbook-wrappers.R

+3
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ wb_remove_timeline <- function(
696696
#' @param remove_cell_style Should we keep the cell style?
697697
#' @param enforce enforce dims
698698
#' @param shared shared formula
699+
#' @param name The name of a named region if specified.
699700
#' @param ... additional arguments
700701
#' @return The workbook, invisibly.
701702
#' @family workbook wrappers
@@ -738,6 +739,7 @@ wb_add_formula <- function(
738739
remove_cell_style = FALSE,
739740
enforce = FALSE,
740741
shared = FALSE,
742+
name = NULL,
741743
...
742744
) {
743745
assert_workbook(wb)
@@ -753,6 +755,7 @@ wb_add_formula <- function(
753755
remove_cell_style = remove_cell_style,
754756
enforce = enforce,
755757
shared = shared,
758+
name = name,
756759
... = ...
757760
)
758761
}

R/class-workbook.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,7 @@ wbWorkbook <- R6::R6Class(
24662466
#' @param remove_cell_style if writing into existing cells, should the cell style be removed?
24672467
#' @param enforce enforce dims
24682468
#' @param shared shared formula
2469+
#' @param name name
24692470
#' @return The `wbWorkbook` object
24702471
add_formula = function(
24712472
sheet = current_sheet(),
@@ -2479,12 +2480,13 @@ wbWorkbook <- R6::R6Class(
24792480
remove_cell_style = FALSE,
24802481
enforce = FALSE,
24812482
shared = FALSE,
2483+
name = NULL,
24822484
...
24832485
) {
24842486

24852487
standardize_case_names(...)
24862488

2487-
if (!is.null(names(x))) {
2489+
if (is.character(x) && !is.null(names(x)) && is.null(name)) {
24882490
assert_class(x, "character")
24892491
assert_named_region(names(x))
24902492

@@ -2511,7 +2513,8 @@ wbWorkbook <- R6::R6Class(
25112513
applyCellStyle = apply_cell_style,
25122514
removeCellStyle = remove_cell_style,
25132515
enforce = enforce,
2514-
shared = shared
2516+
shared = shared,
2517+
name = name
25152518
)
25162519
invisible(self)
25172520
},

R/write.R

+2
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ do_write_formula <- function(
11891189
remove_cell_style = FALSE,
11901190
enforce = FALSE,
11911191
shared = FALSE,
1192+
name = NULL,
11921193
...
11931194
) {
11941195
standardize_case_names(...)
@@ -1318,6 +1319,7 @@ do_write_formula <- function(
13181319
array = array,
13191320
col_names = FALSE,
13201321
row_names = FALSE,
1322+
name = name,
13211323
apply_cell_style = apply_cell_style,
13221324
remove_cell_style = remove_cell_style,
13231325
enforce = enforce,

man/wbWorkbook.Rd

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wb_add_formula.Rd

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-formulas.R

+3
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,7 @@ test_that("registering formulas works", {
212212
expect_message(wb$add_formula(x = c(YESTERDAY = fml)), "formula registered to the workbook")
213213
expect_equal(wb$get_named_regions()$value, fml)
214214

215+
wb <- wb_add_formula(wb, x = "YESTERDAY()", name = "YSTRDY", array = TRUE)
216+
expect_equal(wb$get_named_regions()$name, c("YESTERDAY", "YSTRDY"))
217+
215218
})

0 commit comments

Comments
 (0)