context("Test wrappers") test_that("int2col and col2int", { nums <- 2:27 chrs <- c("B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA") expect_equal(chrs, int2col(nums)) expect_equal(nums, col2int(chrs)) }) test_that("deleteDataColumn basics", { wb <- createWorkbook() addWorksheet(wb, "tester") for (i in seq(5)) { mat <- data.frame(x = rep(paste0(int2col(i), i), 10)) writeData(wb, sheet = 1, startRow = 1, startCol = i, mat) writeFormula(wb, sheet = 1, startRow = 12, startCol = i, x = sprintf("=COUNTA(%s2:%s11)", int2col(i), int2col(i))) } expect_equal( setdiff(wb$worksheets[[1]]$sheet_data$f, NA), c("=COUNTA(A2:A11)", "=COUNTA(B2:B11)", "=COUNTA(C2:C11)", "=COUNTA(D2:D11)", "=COUNTA(E2:E11)") ) deleteDataColumn(wb, 1, col = 3) expect_equal(read.xlsx(wb), data.frame(x = rep("A1", 10), x = "B2", x = "D4", x = "E5", # no C3! check.names = FALSE)) expect_equal( setdiff(wb$worksheets[[1]]$sheet_data$f, NA), c("=COUNTA(A2:A11)", "=COUNTA(B2:B11)", "=COUNTA(C2:C11)", "=COUNTA(D2:D11)") ) deleteDataColumn(wb, 1, col = 2) expect_equal(read.xlsx(wb), data.frame(x = rep("A1", 10), x = "D4", x = "E5", # no B2! check.names = FALSE)) expect_equal( setdiff(wb$worksheets[[1]]$sheet_data$f, NA), c("=COUNTA(A2:A11)", "=COUNTA(B2:B11)", "=COUNTA(C2:C11)") ) deleteDataColumn(wb, 1, col = 1) expect_equal(read.xlsx(wb), data.frame(x = rep("D4", 10), x = "E5", # no A1! check.names = FALSE)) expect_equal( setdiff(wb$worksheets[[1]]$sheet_data$f, NA), c("=COUNTA(A2:A11)", "=COUNTA(B2:B11)") ) }) test_that("deleteDataColumn with more complicated formulae", { # works with more complicated formula as well! wb <- createWorkbook() addWorksheet(wb, "tester") writeData(wb, sheet = 1, startRow = 1, startCol = 1, x = matrix(c(1, 1), ncol = 1), colNames = FALSE) for (c in 2:10) writeFormula(wb, 1, sprintf("%s1 + 1", int2col(c - 1)), startRow = 1, startCol = c) for (c in 2:10) writeFormula(wb, 1, sprintf("%s1 + %s2", int2col(c), int2col(c - 1)), startRow = 2, startCol = c) for (c in 2:10) writeFormula(wb, 1, sprintf("%s2 + %s2", int2col(c), int2col(c + 1)), startRow = 3, startCol = c) deleteDataColumn(wb, 1, 3) # saveWorkbook(wb, "tester.xlsx") # and inspect by hand: expect lots of #REF! expect_equal(read.xlsx(wb), data.frame(`1` = 1, check.names = FALSE)) expect_equal( wb$worksheets[[1]]$sheet_data$f, c(NA, "A1 + 1", "#REF!1 + 1", "C1 + 1", "D1 + 1", "E1 + 1", "F1 + 1", "G1 + 1", "H1 + 1", NA, "B1 + A2", "C1 + #REF!2", "D1 + C2", "E1 + D2", "F1 + E2", "G1 + F2", "H1 + G2", "I1 + H2", "B2 + #REF!2", "C2 + D2", "D2 + E2", "E2 + F2", "F2 + G2", "G2 + H2", "H2 + I2", "I2 + J2") ) }) test_that("deleteDataColumn with wide data", { wb <- createWorkbook() addWorksheet(wb, "tester") ncols <- 30 nrows <- 100 df <- as.data.frame(matrix(seq(ncols * nrows), ncol = ncols)) colnames(df) <- int2col(seq(ncols)) writeData(wb, sheet = 1, startRow = 1, startCol = 1, x = df, colNames = TRUE) expect_equal(read.xlsx(wb), df) deleteDataColumn(wb, 1, 2) expect_equal(read.xlsx(wb), df[, -2]) deleteDataColumn(wb, 1, 100) expect_equal(read.xlsx(wb), df[, -2]) deleteDataColumn(wb, 1, 55) expect_equal(read.xlsx(wb), df[, -c(2, 56)]) # 56 b.c. one col was already taken out deleteDataColumn(wb, 1, 1) expect_equal(read.xlsx(wb), df[, -c(1, 2, 56)]) # delete all data for (i in seq(ncols - 2)) deleteDataColumn(wb, 1, 1) expect_warning(read.xlsx(wb), "No data found on worksheet") }) test_that("deleteDataColumn with formatting data", { wb <- createWorkbook() addWorksheet(wb, "tester") df <- data.frame(x = 1:10, y = letters[1:10], z = 10:1) writeData(wb, sheet = 1, startRow = 1, startCol = 1, x = df, colNames = TRUE) st <- openxlsx::createStyle(textDecoration = "Bold", fontSize = 20, fontColour = "red") openxlsx::addStyle(wb, 1, style = st, rows = 1, cols = seq(ncol(df))) sst <- wb$styleObjects[[1]] sst$rows <- c(1, 1) sst$cols <- c(1, 2) deleteDataColumn(wb, 1, 2) expect_length(wb$styleObjects, 1) expect_equal(wb$styleObjects[[1]], sst) })