diff --git a/R/class-workbook.R b/R/class-workbook.R index f16401af6..ba765465f 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -9395,6 +9395,9 @@ wbWorkbook <- R6::R6Class( ) { name <- replace_legal_chars(name) + if (!grepl("^[\\p{L}_][^\\s]*$", name, perl = TRUE)) + stop("named region must begin with a letter or an underscore and not contain whitespace(s).") + # special names ## print diff --git a/tests/testthat/test-named_regions.R b/tests/testthat/test-named_regions.R index 4c0dc8240..c85d49846 100644 --- a/tests/testthat/test-named_regions.R +++ b/tests/testthat/test-named_regions.R @@ -524,3 +524,13 @@ test_that("local_sheet works", { expect_equal(exp, got) }) + +test_that("named region checks work", { + wb <- wb_workbook()$add_worksheet() + + expect_error(wb$add_named_region(dims = "A1", name = 1), "letter or an underscore") + expect_error(wb$add_named_region(dims = "B1", name = "1"), "letter or an underscore") + expect_silent(wb$add_named_region(dims = "C1", name = stringi::stri_unescape_unicode("\\u00e4"))) + expect_error(wb$add_named_region(dims = "D1", name = "a b"), "letter or an underscore") + expect_error(wb$add_named_region(dims = "D1", name = "A1"), "cell reference") +})