Skip to content

Commit 0945197

Browse files
committed
[misc] add assert_named_region()
1 parent 0aa8ad7 commit 0945197

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

R/asserts.R

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ assert_sheet_data <- function(x) assert_class(x, c("wbSheetData", "R6"), all =
4141
assert_workbook <- function(x) assert_class(x, c("wbWorkbook", "R6"), all = TRUE)
4242
assert_worksheet <- function(x) assert_class(x, c("wbWorksheet", "R6"), all = TRUE)
4343

44+
assert_named_region <- function(x) {
45+
if (grepl("^[A-Z]{1,3}[0-9]+$", x))
46+
stop("name cannot look like a cell reference.")
47+
}
48+
4449
match_oneof <- function(x, y, or_null = FALSE, several = FALSE, envir = parent.frame()) {
4550
sx <- as.character(substitute(x, envir))
4651

R/class-workbook.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,8 @@ wbWorkbook <- R6::R6Class(
24882488

24892489
if (!is.null(as_named_region)) {
24902490
assert_class(x, "character")
2491-
assert_class(as_named_region, "character") # better check that it is a valid name
2491+
assert_class(as_named_region, "character")
2492+
assert_named_region(name)
24922493

24932494
xml <- xml_node_create(
24942495
"definedName",
@@ -7760,13 +7761,13 @@ wbWorkbook <- R6::R6Class(
77607761
}
77617762
match_dn <- which(sel)
77627763

7764+
assert_named_region(name)
7765+
77637766
if (any(match_dn)) {
77647767
if (overwrite)
77657768
self$workbook$definedNames <- self$workbook$definedNames[-match_dn]
77667769
else
77677770
stop(sprintf("Named region with name '%s' already exists! Use overwrite = TRUE if you want to replace it", name))
7768-
} else if (grepl("^[A-Z]{1,3}[0-9]+$", name)) {
7769-
stop("name cannot look like a cell reference.")
77707771
}
77717772

77727773
rowcols <- dims_to_rowcol(dims, as_integer = TRUE)

0 commit comments

Comments
 (0)