Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for illegal sheetnames #213

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions R/WorkbookClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ Workbook$methods(
vdpi = openxlsx_getOp("vdpi", 300)
) {
if (!missing(sheetName)) {
if (grepl(pattern = ":", x = sheetName)) {
stop("colon not allowed in sheet names in Excel")
if (grepl(pattern = "([/\\*'?\\[:\\]+])",perl = T, x = sheetName)) {
stop("Illegal character in sheet names. Don't use the following [ ] * / \ ? :")
}
}
newSheetIndex <- length(worksheets) + 1L
Expand Down Expand Up @@ -219,11 +219,7 @@ Workbook$methods(
Workbook$methods(
cloneWorksheet = function(sheetName, clonedSheet) {
clonedSheet <- validateSheet(clonedSheet)
if (!missing(sheetName)) {
if (grepl(pattern = ":", x = sheetName)) {
stop("colon not allowed in sheet names in Excel")
}
}

newSheetIndex <- length(worksheets) + 1L
if (newSheetIndex > 1) {
sheetId <-
Expand Down Expand Up @@ -1116,12 +1112,18 @@ Workbook$methods(

Workbook$methods(
validateSheet = function(sheetName) {
if (!missing(sheetName)) {
if (grepl(pattern = "([/\\*'?\\[:\\]+])",perl = T, x = sheetName)) {
stop("Illegal character in sheet names. Don't use the following [ ] * / \ ? :")
}
}

if (!is.numeric(sheetName)) {
if (is.null(sheet_names)) {
stop("Workbook does not contain any worksheets.", call. = FALSE)
}
}

if (is.numeric(sheetName)) {
if (sheetName > length(sheet_names)) {
stop("This Workbook only has ", length(sheet_names),
Expand Down Expand Up @@ -1918,12 +1920,15 @@ Workbook$methods(
if (newSheetName %in% sheet_names) {
stop(sprintf("Sheet %s already exists!", newSheetName))
}


if (grepl(pattern = "([/\\*'?\\[:\\]+])",perl = T, x = newSheetName)) {
stop("Illegal character in sheet names. Don't use the following [ ] * / \ ? :")
}

sheet <- validateSheet(sheet)

oldName <- sheet_names[[sheet]]
sheet_names[[sheet]] <<- newSheetName

## Rename in workbook
sheetId <-
regmatches(
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-Worksheet_naming.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,29 @@ test_that("Worksheet names", {
expect_equal(sheetname,names(wb))
expect_equal("test &quot;A&quot;",wb$sheet_names)
})


test_that("test for illegal characters", {
###

wb <- createWorkbook()
x <- data.frame(a = 1, b = 2)

addWorksheet(wb, "Test")
for(i in c("[", "]", "*", "/", "?", ":")){
sheetname <- paste0('test_',i)
y <- list(a = x, sheetname = x, c = x)
expect_error(addWorksheet(wb, sheetname))
expect_error(cloneWorksheet(wb, "Test", sheetname))
expect_error(renameWorksheet(wb, "Test", sheetname))
expect_error(parse(names(wb)<- sheetname))
expect_error(parse(buildWorkbook(y, asTable = TRUE)))
}

})