You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7KB

  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. // MergeCell define a merged cell data.
  31. // It consists of the following structure.
  32. // example: []string{"D4:E10", "cell value"}
  33. type MergeCell []string
  34. // GetCellValue returns merged cell value.
  35. func (m *MergeCell) GetCellValue() string {
  36. return (*m)[1]
  37. }
  38. // GetStartAxis returns the merge start axis.
  39. // example: "C2"
  40. func (m *MergeCell) GetStartAxis() string {
  41. axis := strings.Split((*m)[0], ":")
  42. return axis[0]
  43. }
  44. // GetEndAxis returns the merge end axis.
  45. // example: "D4"
  46. func (m *MergeCell) GetEndAxis() string {
  47. axis := strings.Split((*m)[0], ":")
  48. return axis[1]
  49. }