From eba39c41f2a4177ec5d640b1a483a33f17907251 Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Fri, 2 Aug 2024 00:01:32 +0200 Subject: [PATCH] [named_region] add checks for named region string. closes #1095 (#1097) --- R/class-workbook.R | 3 +++ tests/testthat/test-named_regions.R | 10 ++++++++++ 2 files changed, 13 insertions(+) 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") +})