1
2
3
4
5
6context("Fill Merged Cells")
7
8
9
10test_that("fill merged cells", {
11  wb <- createWorkbook()
12  addWorksheet(wb, sheetName = "sheet1")
13  writeData(wb = wb, sheet = 1, x = data.frame("A" = 1, "B" = 2))
14  writeData(wb = wb, sheet = 1, x = 2, startRow = 2, startCol = 2)
15  writeData(wb = wb, sheet = 1, x = 3, startRow = 2, startCol = 3)
16  writeData(wb = wb, sheet = 1, x = 4, startRow = 2, startCol = 4)
17  writeData(wb = wb, sheet = 1, x = t(matrix(1:4, 4, 4)), startRow = 3, startCol = 1, colNames = FALSE)
18
19  mergeCells(wb = wb, sheet = 1, cols = 2:4, rows = 1)
20  mergeCells(wb = wb, sheet = 1, cols = 2:4, rows = 3)
21  mergeCells(wb = wb, sheet = 1, cols = 2:4, rows = 4)
22  mergeCells(wb = wb, sheet = 1, cols = 2:4, rows = 5)
23
24  tmp_file <- temp_xlsx()
25  saveWorkbook(wb = wb, file = tmp_file, overwrite = TRUE)
26
27  expect_equal(names(read.xlsx(tmp_file, fillMergedCells = FALSE)), c("A", "B", "X3", "X4"))
28  expect_equal(names(read.xlsx(tmp_file, fillMergedCells = TRUE)), c("A", "B", "B", "B"))
29
30  r1 <- data.frame("A" = rep(1, 5), "B" = rep(2, 5), "X3" = rep(3,5), "X4" = rep(4, 5))
31  r2 <- data.frame("A" = rep(1, 5), "B" = rep(2, 5), "B1" = c(3,2,2,2,3), "B2" = c(4,2,2,2,4))
32  names(r2) <- c("A", "B", "B", "B")
33
34  r2_1 <- r2[1:5, 1:3]
35  names(r2_1) <- c("A", "B", "B")
36
37  expect_equal(read.xlsx(tmp_file, fillMergedCells = FALSE), r1)
38  expect_equal(read.xlsx(tmp_file, fillMergedCells = TRUE), r2)
39
40  expect_equal( read.xlsx(tmp_file, cols = 1:3, fillMergedCells = TRUE), r2_1)
41  expect_equal( read.xlsx(tmp_file, rows = 1:3, fillMergedCells = TRUE), r2[1:2, ])
42  expect_equal( read.xlsx(tmp_file, cols = 1:3, rows = 1:4, fillMergedCells = TRUE), r2_1[1:3,])
43
44})
45