cellmerged.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 - 2019 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 files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.10 or later.
  9. package excelize
  10. import "strings"
  11. // GetMergeCells provides a function to get all merged cells from a worksheet
  12. // currently.
  13. func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
  14. var mergeCells []MergeCell
  15. xlsx, err := f.workSheetReader(sheet)
  16. if err != nil {
  17. return mergeCells, err
  18. }
  19. if xlsx.MergeCells != nil {
  20. mergeCells = make([]MergeCell, 0, len(xlsx.MergeCells.Cells))
  21. for i := range xlsx.MergeCells.Cells {
  22. ref := xlsx.MergeCells.Cells[i].Ref
  23. axis := strings.Split(ref, ":")[0]
  24. val, _ := f.GetCellValue(sheet, axis)
  25. mergeCells = append(mergeCells, []string{ref, val})
  26. }
  27. }
  28. return mergeCells, err
  29. }
  30. // UnmergeCell provides a function to unmerge a given coordinate area.
  31. // For example unmerge area D3:E9 on Sheet1:
  32. //
  33. // err := f.UnmergeCell("Sheet1", "D3", "E9")
  34. //
  35. // Attention: overlapped areas will also be unmerged.
  36. func (f *File) UnmergeCell(sheet string, hcell, vcell string) error {
  37. xlsx, err := f.workSheetReader(sheet)
  38. if err != nil {
  39. return err
  40. }
  41. coordinates, err := f.areaRefToCoordinates(hcell + ":" + vcell)
  42. if err != nil {
  43. return err
  44. }
  45. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  46. if x2 < x1 {
  47. x1, x2 = x2, x1
  48. }
  49. if y2 < y1 {
  50. y1, y2 = y2, y1
  51. }
  52. hcell, _ = CoordinatesToCellName(x1, y1)
  53. vcell, _ = CoordinatesToCellName(x2, y2)
  54. // return nil since no MergeCells in the sheet
  55. if xlsx.MergeCells == nil {
  56. return nil
  57. }
  58. ref := hcell + ":" + vcell
  59. i := 0
  60. for _, cellData := range xlsx.MergeCells.Cells {
  61. cc := strings.Split(cellData.Ref, ":")
  62. c1, _ := checkCellInArea(hcell, cellData.Ref)
  63. c2, _ := checkCellInArea(vcell, cellData.Ref)
  64. c3, _ := checkCellInArea(cc[0], ref)
  65. c4, _ := checkCellInArea(cc[1], ref)
  66. // skip the overlapped mergecell
  67. if c1 || c2 || c3 || c4 {
  68. continue
  69. }
  70. xlsx.MergeCells.Cells[i] = cellData
  71. i++
  72. }
  73. xlsx.MergeCells.Cells = xlsx.MergeCells.Cells[:i]
  74. return nil
  75. }
  76. // MergeCell define a merged cell data.
  77. // It consists of the following structure.
  78. // example: []string{"D4:E10", "cell value"}
  79. type MergeCell []string
  80. // GetCellValue returns merged cell value.
  81. func (m *MergeCell) GetCellValue() string {
  82. return (*m)[1]
  83. }
  84. // GetStartAxis returns the merge start axis.
  85. // example: "C2"
  86. func (m *MergeCell) GetStartAxis() string {
  87. axis := strings.Split((*m)[0], ":")
  88. return axis[0]
  89. }
  90. // GetEndAxis returns the merge end axis.
  91. // example: "D4"
  92. func (m *MergeCell) GetEndAxis() string {
  93. axis := strings.Split((*m)[0], ":")
  94. return axis[1]
  95. }