merge_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package excelize
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestMergeCell(t *testing.T) {
  8. f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
  9. if !assert.NoError(t, err) {
  10. t.FailNow()
  11. }
  12. assert.EqualError(t, f.MergeCell("Sheet1", "A", "B"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  13. assert.NoError(t, f.MergeCell("Sheet1", "D9", "D9"))
  14. assert.NoError(t, f.MergeCell("Sheet1", "D9", "E9"))
  15. assert.NoError(t, f.MergeCell("Sheet1", "H14", "G13"))
  16. assert.NoError(t, f.MergeCell("Sheet1", "C9", "D8"))
  17. assert.NoError(t, f.MergeCell("Sheet1", "F11", "G13"))
  18. assert.NoError(t, f.MergeCell("Sheet1", "H7", "B15"))
  19. assert.NoError(t, f.MergeCell("Sheet1", "D11", "F13"))
  20. assert.NoError(t, f.MergeCell("Sheet1", "G10", "K12"))
  21. assert.NoError(t, f.SetCellValue("Sheet1", "G11", "set value in merged cell"))
  22. assert.NoError(t, f.SetCellInt("Sheet1", "H11", 100))
  23. assert.NoError(t, f.SetCellValue("Sheet1", "I11", float64(0.5)))
  24. assert.NoError(t, f.SetCellHyperLink("Sheet1", "J11", "https://github.com/xuri/excelize", "External"))
  25. assert.NoError(t, f.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)"))
  26. value, err := f.GetCellValue("Sheet1", "H11")
  27. assert.Equal(t, "0.5", value)
  28. assert.NoError(t, err)
  29. value, err = f.GetCellValue("Sheet2", "A6") // Merged cell ref is single coordinate.
  30. assert.Equal(t, "", value)
  31. assert.NoError(t, err)
  32. value, err = f.GetCellFormula("Sheet1", "G12")
  33. assert.Equal(t, "SUM(Sheet1!B19,Sheet1!C19)", value)
  34. assert.NoError(t, err)
  35. f.NewSheet("Sheet3")
  36. assert.NoError(t, f.MergeCell("Sheet3", "D11", "F13"))
  37. assert.NoError(t, f.MergeCell("Sheet3", "G10", "K12"))
  38. assert.NoError(t, f.MergeCell("Sheet3", "B1", "D5")) // B1:D5
  39. assert.NoError(t, f.MergeCell("Sheet3", "E1", "F5")) // E1:F5
  40. assert.NoError(t, f.MergeCell("Sheet3", "H2", "I5"))
  41. assert.NoError(t, f.MergeCell("Sheet3", "I4", "J6")) // H2:J6
  42. assert.NoError(t, f.MergeCell("Sheet3", "M2", "N5"))
  43. assert.NoError(t, f.MergeCell("Sheet3", "L4", "M6")) // L2:N6
  44. assert.NoError(t, f.MergeCell("Sheet3", "P4", "Q7"))
  45. assert.NoError(t, f.MergeCell("Sheet3", "O2", "P5")) // O2:Q7
  46. assert.NoError(t, f.MergeCell("Sheet3", "A9", "B12"))
  47. assert.NoError(t, f.MergeCell("Sheet3", "B7", "C9")) // A7:C12
  48. assert.NoError(t, f.MergeCell("Sheet3", "E9", "F10"))
  49. assert.NoError(t, f.MergeCell("Sheet3", "D8", "G12"))
  50. assert.NoError(t, f.MergeCell("Sheet3", "I8", "I12"))
  51. assert.NoError(t, f.MergeCell("Sheet3", "I10", "K10"))
  52. assert.NoError(t, f.MergeCell("Sheet3", "M8", "Q13"))
  53. assert.NoError(t, f.MergeCell("Sheet3", "N10", "O11"))
  54. // Test get merged cells on not exists worksheet.
  55. assert.EqualError(t, f.MergeCell("SheetN", "N10", "O11"), "sheet SheetN is not exist")
  56. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestMergeCell.xlsx")))
  57. f = NewFile()
  58. assert.NoError(t, f.MergeCell("Sheet1", "A2", "B3"))
  59. ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
  60. assert.True(t, ok)
  61. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{nil, nil}}
  62. assert.NoError(t, f.MergeCell("Sheet1", "A2", "B3"))
  63. ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
  64. assert.True(t, ok)
  65. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A1"}}}
  66. assert.EqualError(t, f.MergeCell("Sheet1", "A2", "B3"), `invalid area "A1"`)
  67. ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
  68. assert.True(t, ok)
  69. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}}
  70. assert.EqualError(t, f.MergeCell("Sheet1", "A2", "B3"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  71. }
  72. func TestGetMergeCells(t *testing.T) {
  73. wants := []struct {
  74. value string
  75. start string
  76. end string
  77. }{{
  78. value: "A1",
  79. start: "A1",
  80. end: "B1",
  81. }, {
  82. value: "A2",
  83. start: "A2",
  84. end: "A3",
  85. }, {
  86. value: "A4",
  87. start: "A4",
  88. end: "B5",
  89. }, {
  90. value: "A7",
  91. start: "A7",
  92. end: "C10",
  93. }}
  94. f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx"))
  95. if !assert.NoError(t, err) {
  96. t.FailNow()
  97. }
  98. sheet1 := f.GetSheetName(0)
  99. mergeCells, err := f.GetMergeCells(sheet1)
  100. if !assert.Len(t, mergeCells, len(wants)) {
  101. t.FailNow()
  102. }
  103. assert.NoError(t, err)
  104. for i, m := range mergeCells {
  105. assert.Equal(t, wants[i].value, m.GetCellValue())
  106. assert.Equal(t, wants[i].start, m.GetStartAxis())
  107. assert.Equal(t, wants[i].end, m.GetEndAxis())
  108. }
  109. // Test get merged cells on not exists worksheet.
  110. _, err = f.GetMergeCells("SheetN")
  111. assert.EqualError(t, err, "sheet SheetN is not exist")
  112. }
  113. func TestUnmergeCell(t *testing.T) {
  114. f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx"))
  115. if !assert.NoError(t, err) {
  116. t.FailNow()
  117. }
  118. sheet1 := f.GetSheetName(0)
  119. xlsx, err := f.workSheetReader(sheet1)
  120. assert.NoError(t, err)
  121. mergeCellNum := len(xlsx.MergeCells.Cells)
  122. assert.EqualError(t, f.UnmergeCell("Sheet1", "A", "A"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  123. // unmerge the mergecell that contains A1
  124. assert.NoError(t, f.UnmergeCell(sheet1, "A1", "A1"))
  125. if len(xlsx.MergeCells.Cells) != mergeCellNum-1 {
  126. t.FailNow()
  127. }
  128. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnmergeCell.xlsx")))
  129. f = NewFile()
  130. assert.NoError(t, f.MergeCell("Sheet1", "A2", "B3"))
  131. // Test unmerged area on not exists worksheet.
  132. assert.EqualError(t, f.UnmergeCell("SheetN", "A1", "A1"), "sheet SheetN is not exist")
  133. ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
  134. assert.True(t, ok)
  135. ws.(*xlsxWorksheet).MergeCells = nil
  136. assert.NoError(t, f.UnmergeCell("Sheet1", "H7", "B15"))
  137. ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
  138. assert.True(t, ok)
  139. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{nil, nil}}
  140. assert.NoError(t, f.UnmergeCell("Sheet1", "H15", "B7"))
  141. ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
  142. assert.True(t, ok)
  143. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A1"}}}
  144. assert.EqualError(t, f.UnmergeCell("Sheet1", "A2", "B3"), `invalid area "A1"`)
  145. ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
  146. assert.True(t, ok)
  147. ws.(*xlsxWorksheet).MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}}
  148. assert.EqualError(t, f.UnmergeCell("Sheet1", "A2", "B3"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  149. }