cellmerged.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package excelize
  2. import "strings"
  3. // GetMergeCells provides a function to get all merged cells from a worksheet currently.
  4. func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
  5. var mergeCells []MergeCell
  6. xlsx, err := f.workSheetReader(sheet)
  7. if err != nil {
  8. return mergeCells, err
  9. }
  10. if xlsx.MergeCells != nil {
  11. mergeCells = make([]MergeCell, 0, len(xlsx.MergeCells.Cells))
  12. for i := range xlsx.MergeCells.Cells {
  13. ref := xlsx.MergeCells.Cells[i].Ref
  14. axis := strings.Split(ref, ":")[0]
  15. val, _ := f.GetCellValue(sheet, axis)
  16. mergeCells = append(mergeCells, []string{ref, val})
  17. }
  18. }
  19. return mergeCells, err
  20. }
  21. // MergeCell define a merged cell data.
  22. // It consists of the following structure.
  23. // example: []string{"D4:E10", "cell value"}
  24. type MergeCell []string
  25. // GetCellValue returns merged cell value.
  26. func (m *MergeCell) GetCellValue() string {
  27. return (*m)[1]
  28. }
  29. // GetStartAxis returns the merge start axis.
  30. // example: "C2"
  31. func (m *MergeCell) GetStartAxis() string {
  32. axis := strings.Split((*m)[0], ":")
  33. return axis[0]
  34. }
  35. // GetEndAxis returns the merge end axis.
  36. // example: "D4"
  37. func (m *MergeCell) GetEndAxis() string {
  38. axis := strings.Split((*m)[0], ":")
  39. return axis[1]
  40. }