|
| 1 | + |
1 | 2 | test_that("write_datatable over tables", {
|
2 | 3 |
|
3 | 4 | overwrite_table_error <- "Cannot overwrite existing table with another table"
|
@@ -91,3 +92,85 @@ test_that("write_data over tables", {
|
91 | 92 | wb$add_data_table(sheet = 1, x = head(iris)[, 1:3], startCol = 1, startRow = 30)
|
92 | 93 | wb$add_data(sheet = 1, x = tail(iris), startCol = 1, startRow = 31, colNames = FALSE)
|
93 | 94 | })
|
| 95 | + |
| 96 | +test_that("Validate Table Names", { |
| 97 | + wb <- wb_add_worksheet(wb_workbook(), "Sheet 1") |
| 98 | + |
| 99 | + ## case |
| 100 | + expect_equal(wb_validate_table_name(wb, "test"), "test") |
| 101 | + expect_equal(wb_validate_table_name(wb, "TEST"), "test") |
| 102 | + expect_equal(wb_validate_table_name(wb, "Test"), "test") |
| 103 | + |
| 104 | + ## length |
| 105 | + expect_error(wb_validate_table_name(wb, paste(sample(LETTERS, size = 300, replace = TRUE), collapse = "")), regexp = "tableName must be less than 255 characters") |
| 106 | + |
| 107 | + ## look like cell ref |
| 108 | + expect_error(wb_validate_table_name(wb, "R1C2"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE) |
| 109 | + expect_error(wb_validate_table_name(wb, "A1"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE) |
| 110 | + |
| 111 | + expect_error(wb_validate_table_name(wb, "R06821C9682"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE) |
| 112 | + expect_error(wb_validate_table_name(wb, "ABD918751"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE) |
| 113 | + |
| 114 | + expect_error(wb_validate_table_name(wb, "A$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE) |
| 115 | + expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE) |
| 116 | + |
| 117 | + tbl_nm <- "性別" |
| 118 | + expect_equal(wb_validate_table_name(wb, tbl_nm), tbl_nm) |
| 119 | +}) |
| 120 | + |
| 121 | +test_that("Existing Table Names", { |
| 122 | + wb <- wb_add_worksheet(wb_workbook(), "Sheet 1") |
| 123 | + |
| 124 | + ## Existing names - case in-sensitive |
| 125 | + wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1") |
| 126 | + expect_error(wb_validate_table_name(wb, "Table1"), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 127 | + expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1", startCol = 10), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 128 | + |
| 129 | + expect_error(wb_validate_table_name(wb, "TABLE1"), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 130 | + expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "TABLE1", startCol = 20), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 131 | + |
| 132 | + expect_error(wb_validate_table_name(wb, "table1"), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 133 | + expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "table1", startCol = 30), regexp = "table with name 'table1' already exists", fixed = TRUE) |
| 134 | +}) |
| 135 | + |
| 136 | +test_that("custom table styles work", { |
| 137 | + |
| 138 | + # at the moment we have no interface to add custom table styles |
| 139 | + wb <- wb_workbook() %>% |
| 140 | + wb_add_worksheet() |
| 141 | + |
| 142 | + # create dxf elements to be used in the table style |
| 143 | + tabCol1 <- create_dxfs_style(bgFill = wb_color(theme = 7)) |
| 144 | + tabCol2 <- create_dxfs_style(bgFill = wb_color(theme = 5)) |
| 145 | + tabBrd1 <- create_dxfs_style(border = TRUE) |
| 146 | + tabCol3 <- create_dxfs_style(bgFill = wb_color(hex = "FFC00000"), font_color = wb_color("white")) |
| 147 | + |
| 148 | + # dont forget to assign them to the workbook |
| 149 | + wb$add_style(tabCol1) |
| 150 | + wb$add_style(tabCol2) |
| 151 | + wb$add_style(tabBrd1) |
| 152 | + wb$add_style(tabCol3) |
| 153 | + |
| 154 | + # tweak a working style with 4 elements |
| 155 | + wb$styles_mgr$styles$tableStyles <- |
| 156 | + sprintf( |
| 157 | + "<tableStyles count=\"1\" defaultTableStyle=\"TableStyleMedium2\" defaultPivotStyle=\"PivotStyleLight16\"> |
| 158 | + <tableStyle name=\"RedTableStyle\" pivot=\"0\" count=\"%s\" xr9:uid=\"{91A57EDA-14C5-4643-B7E3-C78161B6BBA4}\"> |
| 159 | + <tableStyleElement type=\"wholeTable\" dxfId=\"%s\"/> |
| 160 | + <tableStyleElement type=\"headerRow\" dxfId=\"%s\"/> |
| 161 | + <tableStyleElement type=\"firstRowStripe\" dxfId=\"%s\"/> |
| 162 | + <tableStyleElement type=\"secondColumnStripe\" dxfId=\"%s\"/> |
| 163 | + </tableStyle> |
| 164 | + </tableStyles>", |
| 165 | + length(c(tabCol1, tabCol2, tabCol3, tabBrd1)), |
| 166 | + wb$styles_mgr$get_dxf_id("tabBrd1"), |
| 167 | + wb$styles_mgr$get_dxf_id("tabCol3"), |
| 168 | + wb$styles_mgr$get_dxf_id("tabCol1"), |
| 169 | + wb$styles_mgr$get_dxf_id("tabCol2") |
| 170 | + ) |
| 171 | + |
| 172 | + expect_silent(wb$add_data_table(x = mtcars, tableStyle = "RedTableStyle")) |
| 173 | + wb$add_worksheet() |
| 174 | + expect_error(wb$add_data_table(x = mtcars, tableStyle = "RedTableStyle1"), "Invalid table style.") |
| 175 | + |
| 176 | +}) |
0 commit comments