From 8c04e7c5d9f5ddbb7d5a5fe15f7623863f63c1ac Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 30 Jan 2024 08:16:04 -0500 Subject: [PATCH 1/3] partial matching --- R/write.R | 22 +++++++++++----------- tests/testthat/test-class-workbook.R | 16 ++++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/R/write.R b/R/write.R index 34171eea4..245a96531 100644 --- a/R/write.R +++ b/R/write.R @@ -468,7 +468,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" @@ -485,7 +485,7 @@ write_data2 <- function( wb$add_cell_style( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, applyNumberFormat = "1", quotePrefix = "1", numFmtId = "49" @@ -504,7 +504,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_numeric ) } @@ -517,7 +517,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_dt ) } @@ -529,7 +529,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_posix ) } @@ -541,7 +541,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_hms ) } @@ -554,7 +554,7 @@ write_data2 <- function( # message("currency: ", dim_sel) wb$add_numfmt( - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_currency ) } @@ -565,7 +565,7 @@ write_data2 <- function( # message("accounting: ", dim_sel) wb$add_numfmt( - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_accounting ) } @@ -577,7 +577,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_percentage ) } @@ -589,7 +589,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_scientific ) } @@ -601,7 +601,7 @@ write_data2 <- function( wb$add_numfmt( sheet = sheetno, - dim = dim_sel, + dims = dim_sel, numfmt = numfmt_comma ) } diff --git a/tests/testthat/test-class-workbook.R b/tests/testthat/test-class-workbook.R index 477d2f410..9aeb8d021 100644 --- a/tests/testthat/test-class-workbook.R +++ b/tests/testthat/test-class-workbook.R @@ -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( "", @@ -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( @@ -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" ) @@ -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", "") From 0e2e86f0a862f683aeff33f7950339b874239708 Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 30 Jan 2024 08:22:51 -0500 Subject: [PATCH 2/3] table_name error message --- R/class-workbook-utils.R | 14 +++++++------- tests/testthat/test-tables.R | 28 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/R/class-workbook-utils.R b/R/class-workbook-utils.R index 1e3291a2c..9cb493011 100644 --- a/R/class-workbook-utils.R +++ b/R/class-workbook-utils.R @@ -26,31 +26,31 @@ 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) + stop("'$' character cannot exist in a `table_name`.", call. = FALSE) } if (grepl(" ", tableName, fixed = TRUE)) { - stop("spaces cannot exist in a table name", call. = FALSE) + stop("spaces cannot exist in `table_name`.", 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 diff --git a/tests/testthat/test-tables.R b/tests/testthat/test-tables.R index 5089422c5..bba8c605d 100644 --- a/tests/testthat/test-tables.R +++ b/tests/testthat/test-tables.R @@ -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 = "'$' character cannot exist in a `table_name`", fixed = TRUE) + expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "'$' character cannot exist in a `table_name`", fixed = TRUE) tbl_nm <- "性別" expect_equal(wb_validate_table_name(wb, tbl_nm), tbl_nm) @@ -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", { @@ -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) From b03c7518acb698a41ca49e7ac3f89ccb23f64d5e Mon Sep 17 00:00:00 2001 From: olivroy Date: Tue, 30 Jan 2024 08:46:34 -0500 Subject: [PATCH 3/3] improve error message to be consistent with surrounding ones --- R/class-workbook-utils.R | 8 ++------ tests/testthat/test-tables.R | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/R/class-workbook-utils.R b/R/class-workbook-utils.R index 9cb493011..1a8732703 100644 --- a/R/class-workbook-utils.R +++ b/R/class-workbook-utils.R @@ -29,12 +29,8 @@ wb_validate_table_name <- function(wb, tableName) { stop("`table_name` must be less than 255 characters.", call. = FALSE) } - if (grepl("$", tableName, fixed = TRUE)) { - stop("'$' character cannot exist in a `table_name`.", call. = FALSE) - } - - if (grepl(" ", tableName, fixed = TRUE)) { - stop("spaces cannot exist in `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)) diff --git a/tests/testthat/test-tables.R b/tests/testthat/test-tables.R index bba8c605d..6c7b863fb 100644 --- a/tests/testthat/test-tables.R +++ b/tests/testthat/test-tables.R @@ -116,8 +116,8 @@ test_that("Validate Table Names", { 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 `table_name`", fixed = TRUE) - expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "'$' character cannot exist in a `table_name`", 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)