merge.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.15 or later.
  11. package excelize
  12. import (
  13. "fmt"
  14. "strings"
  15. )
  16. // MergeCell provides a function to merge cells by given coordinate area and
  17. // sheet name. For example create a merged cell of D3:E9 on Sheet1:
  18. //
  19. // err := f.MergeCell("Sheet1", "D3", "E9")
  20. //
  21. // If you create a merged cell that overlaps with another existing merged cell,
  22. // those merged cells that already exist will be removed.
  23. //
  24. // B1(x1,y1) D1(x2,y1)
  25. // +------------------------+
  26. // | |
  27. // A4(x3,y3) | C4(x4,y3) |
  28. // +------------------------+ |
  29. // | | | |
  30. // | |B5(x1,y2) | D5(x2,y2)|
  31. // | +------------------------+
  32. // | |
  33. // |A8(x3,y4) C8(x4,y4)|
  34. // +------------------------+
  35. //
  36. func (f *File) MergeCell(sheet, hcell, vcell string) error {
  37. rect1, err := f.areaRefToCoordinates(hcell + ":" + vcell)
  38. if err != nil {
  39. return err
  40. }
  41. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  42. _ = sortCoordinates(rect1)
  43. hcell, _ = CoordinatesToCellName(rect1[0], rect1[1])
  44. vcell, _ = CoordinatesToCellName(rect1[2], rect1[3])
  45. ws, err := f.workSheetReader(sheet)
  46. if err != nil {
  47. return err
  48. }
  49. ref := hcell + ":" + vcell
  50. if ws.MergeCells != nil {
  51. for i := 0; i < len(ws.MergeCells.Cells); i++ {
  52. cellData := ws.MergeCells.Cells[i]
  53. if cellData == nil {
  54. continue
  55. }
  56. cc := strings.Split(cellData.Ref, ":")
  57. if len(cc) != 2 {
  58. return fmt.Errorf("invalid area %q", cellData.Ref)
  59. }
  60. rect2, err := f.areaRefToCoordinates(cellData.Ref)
  61. if err != nil {
  62. return err
  63. }
  64. // Delete the merged cells of the overlapping area.
  65. if isOverlap(rect1, rect2) {
  66. ws.MergeCells.Cells = append(ws.MergeCells.Cells[:i], ws.MergeCells.Cells[i+1:]...)
  67. i--
  68. if rect1[0] > rect2[0] {
  69. rect1[0], rect2[0] = rect2[0], rect1[0]
  70. }
  71. if rect1[2] < rect2[2] {
  72. rect1[2], rect2[2] = rect2[2], rect1[2]
  73. }
  74. if rect1[1] > rect2[1] {
  75. rect1[1], rect2[1] = rect2[1], rect1[1]
  76. }
  77. if rect1[3] < rect2[3] {
  78. rect1[3], rect2[3] = rect2[3], rect1[3]
  79. }
  80. hcell, _ = CoordinatesToCellName(rect1[0], rect1[1])
  81. vcell, _ = CoordinatesToCellName(rect1[2], rect1[3])
  82. ref = hcell + ":" + vcell
  83. }
  84. }
  85. ws.MergeCells.Cells = append(ws.MergeCells.Cells, &xlsxMergeCell{Ref: ref})
  86. } else {
  87. ws.MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: ref}}}
  88. }
  89. ws.MergeCells.Count = len(ws.MergeCells.Cells)
  90. return err
  91. }
  92. // UnmergeCell provides a function to unmerge a given coordinate area.
  93. // For example unmerge area D3:E9 on Sheet1:
  94. //
  95. // err := f.UnmergeCell("Sheet1", "D3", "E9")
  96. //
  97. // Attention: overlapped areas will also be unmerged.
  98. func (f *File) UnmergeCell(sheet string, hcell, vcell string) error {
  99. ws, err := f.workSheetReader(sheet)
  100. if err != nil {
  101. return err
  102. }
  103. rect1, err := f.areaRefToCoordinates(hcell + ":" + vcell)
  104. if err != nil {
  105. return err
  106. }
  107. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  108. _ = sortCoordinates(rect1)
  109. // return nil since no MergeCells in the sheet
  110. if ws.MergeCells == nil {
  111. return nil
  112. }
  113. i := 0
  114. for _, cellData := range ws.MergeCells.Cells {
  115. if cellData == nil {
  116. continue
  117. }
  118. cc := strings.Split(cellData.Ref, ":")
  119. if len(cc) != 2 {
  120. return fmt.Errorf("invalid area %q", cellData.Ref)
  121. }
  122. rect2, err := f.areaRefToCoordinates(cellData.Ref)
  123. if err != nil {
  124. return err
  125. }
  126. if isOverlap(rect1, rect2) {
  127. continue
  128. }
  129. ws.MergeCells.Cells[i] = cellData
  130. i++
  131. }
  132. ws.MergeCells.Cells = ws.MergeCells.Cells[:i]
  133. ws.MergeCells.Count = len(ws.MergeCells.Cells)
  134. if ws.MergeCells.Count == 0 {
  135. ws.MergeCells = nil
  136. }
  137. return nil
  138. }
  139. // GetMergeCells provides a function to get all merged cells from a worksheet
  140. // currently.
  141. func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
  142. var mergeCells []MergeCell
  143. ws, err := f.workSheetReader(sheet)
  144. if err != nil {
  145. return mergeCells, err
  146. }
  147. if ws.MergeCells != nil {
  148. mergeCells = make([]MergeCell, 0, len(ws.MergeCells.Cells))
  149. for i := range ws.MergeCells.Cells {
  150. ref := ws.MergeCells.Cells[i].Ref
  151. axis := strings.Split(ref, ":")[0]
  152. val, _ := f.GetCellValue(sheet, axis)
  153. mergeCells = append(mergeCells, []string{ref, val})
  154. }
  155. }
  156. return mergeCells, err
  157. }
  158. // MergeCell define a merged cell data.
  159. // It consists of the following structure.
  160. // example: []string{"D4:E10", "cell value"}
  161. type MergeCell []string
  162. // GetCellValue returns merged cell value.
  163. func (m *MergeCell) GetCellValue() string {
  164. return (*m)[1]
  165. }
  166. // GetStartAxis returns the merge start axis.
  167. // example: "C2"
  168. func (m *MergeCell) GetStartAxis() string {
  169. axis := strings.Split((*m)[0], ":")
  170. return axis[0]
  171. }
  172. // GetEndAxis returns the merge end axis.
  173. // example: "D4"
  174. func (m *MergeCell) GetEndAxis() string {
  175. axis := strings.Split((*m)[0], ":")
  176. return axis[1]
  177. }