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

Partial matching + table name error message. #923

Merged
merged 4 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions R/class-workbook-utils.R
Expand Up @@ -26,31 +26,27 @@ wb_validate_table_name <- function(wb, tableName) {
# TODO add a strict = getOption("openxlsx2.tableName.strict", FALSE)
# param to force these to allow to stopping
if (nchar(tableName) > 255) {
stop("tableName must be less than 255 characters.", call. = FALSE)
stop("`table_name` must be less than 255 characters.", call. = FALSE)
}

if (grepl("$", tableName, fixed = TRUE)) {
stop("'$' character cannot exist in a tableName", call. = FALSE)
}

if (grepl(" ", tableName, fixed = TRUE)) {
stop("spaces cannot exist in a table name", call. = FALSE)
if (grepl("\\$|\\s", tableName)) {
stop("`table_name` cannot contain spaces or the '$' character.", call. = FALSE)
}

# if (!grepl("^[A-Za-z_]", tableName, perl = TRUE))
# stop("tableName must begin with a letter or an underscore")
# stop("`table_name` must begin with a letter or an underscore", call. = FALSE)

if (grepl("R[0-9]+C[0-9]+", tableName, perl = TRUE, ignore.case = TRUE)) {
stop("tableName cannot be the same as a cell reference, such as R1C1", call. = FALSE)
stop("`table_name` cannot be the same as a cell reference, such as R1C1.", call. = FALSE)
}

if (grepl("^[A-Z]{1,3}[0-9]+$", tableName, ignore.case = TRUE)) {
stop("tableName cannot be the same as a cell reference", call. = FALSE)
stop("`table_name` cannot be the same as a cell reference.", call. = FALSE)
}

# only place where self is needed
if (tableName %in% wb$tables$tab_name) {
stop(sprintf("table with name '%s' already exists", tableName), call. = FALSE)
stop(sprintf("`table_name = '%s'` already exists.", tableName), call. = FALSE)
}

tableName
Expand Down
22 changes: 11 additions & 11 deletions R/write.R
Expand Up @@ -469,7 +469,7 @@ write_data2 <- function(

wb$add_font(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
color = wb_color(hex = "FF0000FF"),
name = wb_get_base_font(wb)$name$val,
u = "single"
Expand All @@ -486,7 +486,7 @@ write_data2 <- function(

wb$add_cell_style(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
applyNumberFormat = "1",
quotePrefix = "1",
numFmtId = "49"
Expand All @@ -505,7 +505,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_numeric
)
}
Expand All @@ -518,7 +518,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_dt
)
}
Expand All @@ -530,7 +530,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_posix
)
}
Expand All @@ -542,7 +542,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_hms
)
}
Expand All @@ -555,7 +555,7 @@ write_data2 <- function(
# message("currency: ", dim_sel)

wb$add_numfmt(
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_currency
)
}
Expand All @@ -566,7 +566,7 @@ write_data2 <- function(
# message("accounting: ", dim_sel)

wb$add_numfmt(
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_accounting
)
}
Expand All @@ -578,7 +578,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_percentage
)
}
Expand All @@ -590,7 +590,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_scientific
)
}
Expand All @@ -602,7 +602,7 @@ write_data2 <- function(

wb$add_numfmt(
sheet = sheetno,
dim = dim_sel,
dims = dim_sel,
numfmt = numfmt_comma
)
}
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-class-workbook.R
Expand Up @@ -825,10 +825,10 @@ test_that("numfmt in pivot tables works", {
filter = c("Location", "Status"), data = "Units")$
add_pivot_table(df, dims = "A3", rows = "Plant",
filter = c("Location", "Status"), data = "Units",
param = list(numfmt = c(formatCode = "#,###0"), sort_row = "ascending"))$
params = list(numfmt = c(formatCode = "#,###0"), sort_row = "ascending"))$
add_pivot_table(df, dims = "A3", rows = "Plant",
filter = c("Location", "Status"), data = "Units",
param = list(numfmt = c(numfmt = 10), sort_row = "descending"))
params = list(numfmt = c(numfmt = 10), sort_row = "descending"))

exp <- c(
"<dataField name=\"Sum of Units\" fld=\"3\" baseField=\"0\" baseItem=\"0\"/>",
Expand All @@ -848,11 +848,11 @@ test_that("numfmt in pivot tables works", {

df <- wb_data(wb)
wb$add_pivot_table(df, dims = "A3", rows = "cyl", cols = "gear",
data = c("vs", "am"), param = list(sort_row = 1, sort_col = -2))
data = c("vs", "am"), params = list(sort_row = 1, sort_col = -2))

wb$add_pivot_table(df, dims = "A3", rows = "gear",
filter = c("cyl"), data = c("vs", "am"),
param = list(sort_row = "descending"))
params = list(sort_row = "descending"))


exp <- c(
Expand All @@ -864,14 +864,14 @@ test_that("numfmt in pivot tables works", {

expect_warning(
wb$add_pivot_table(df, dims = "A3", rows = "cyl", cols = "gear",
data = c("vs", "am"), param = list(sort_row = 1, sort_col = -7)),
data = c("vs", "am"), params = list(sort_row = 1, sort_col = -7)),
"invalid sort position found"
)

expect_error(
wb$add_pivot_table(df, dims = "A3", rows = "cyl", cols = "gear",
data = c("vs", "am"),
param = list(numfmt = c(numfmt = 10))),
params = list(numfmt = c(numfmt = 10))),
"length of numfmt and data does not match"
)

Expand All @@ -892,8 +892,8 @@ test_that("numfmt in pivot tables works", {
rows = c("cyl", "am"),
cols = c("gear", "carb"),
data = c("disp", "mpg"),
param = list(sort_row = 1,
sort_col = -2)
params = list(sort_row = 1,
sort_col = -2)
)

exp <- c("", "ascending", "", "", "", "", "", "", "", "descending", "")
Expand Down
28 changes: 14 additions & 14 deletions tests/testthat/test-tables.R
Expand Up @@ -107,17 +107,17 @@ test_that("Validate Table Names", {
expect_equal(wb_validate_table_name(wb, "Test"), "test")

## length
expect_error(wb_validate_table_name(wb, paste(sample(LETTERS, size = 300, replace = TRUE), collapse = "")), regexp = "tableName must be less than 255 characters")
expect_error(wb_validate_table_name(wb, paste(sample(LETTERS, size = 300, replace = TRUE), collapse = "")), regexp = "`table_name` must be less than 255 characters")

## look like cell ref
expect_error(wb_validate_table_name(wb, "R1C2"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "A1"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "R1C2"), regexp = "`table_name` cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "A1"), regexp = "`table_name` cannot be the same as a cell reference", fixed = TRUE)

expect_error(wb_validate_table_name(wb, "R06821C9682"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "ABD918751"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "R06821C9682"), regexp = "`table_name` cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "ABD918751"), regexp = "`table_name` cannot be the same as a cell reference", fixed = TRUE)

expect_error(wb_validate_table_name(wb, "A$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "A$100"), regexp = "`table_name` cannot contain spaces or the '$' character", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "`table_name` cannot contain spaces or the '$' character", fixed = TRUE)

tbl_nm <- "性別"
expect_equal(wb_validate_table_name(wb, tbl_nm), tbl_nm)
Expand All @@ -128,14 +128,14 @@ test_that("Existing Table Names", {

## Existing names - case in-sensitive
wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1")
expect_error(wb_validate_table_name(wb, "Table1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1", startCol = 10), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "Table1"), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), table_name = "Table1", startCol = 10), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)

expect_error(wb_validate_table_name(wb, "TABLE1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "TABLE1", startCol = 20), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "TABLE1"), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "TABLE1", startCol = 20), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)

expect_error(wb_validate_table_name(wb, "table1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "table1", startCol = 30), regexp = "table with name 'table1' already exists", fixed = TRUE)
expect_error(wb_validate_table_name(wb, "table1"), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "table1", startCol = 30), regexp = "`table_name = 'table1'` already exists", fixed = TRUE)
})

test_that("custom table styles work", {
Expand All @@ -150,7 +150,7 @@ test_that("custom table styles work", {
tabBrd1 <- create_dxfs_style(border = TRUE)
tabCol3 <- create_dxfs_style(bgFill = wb_color(hex = "FFC00000"), font_color = wb_color("white"))

# dont forget to assign them to the workbook
# don't forget to assign them to the workbook
wb$add_style(tabCol1)
wb$add_style(tabCol2)
wb$add_style(tabBrd1)
Expand Down