Skip to content

Commit 3ca3156

Browse files
authored
Merge pull request qax-os#313 from sairoutine/feature/get_merge_cells
Add GetMergeCells
2 parents 90221bd + 3012df0 commit 3ca3156

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

excelize.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,43 @@ func (f *File) adjustAutoFilterHelper(xlsx *xlsxWorksheet, column, rowIndex, off
411411
}
412412
}
413413
}
414+
415+
// GetMergeCells provides a function to get all merged cells from a worksheet currently.
416+
func (f *File) GetMergeCells(sheet string) []MergeCell {
417+
mergeCells := []MergeCell{}
418+
419+
xlsx := f.workSheetReader(sheet)
420+
if xlsx.MergeCells != nil {
421+
for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
422+
ref := xlsx.MergeCells.Cells[i].Ref
423+
axis := strings.Split(ref, ":")[0]
424+
mergeCells = append(mergeCells, []string{ref, f.GetCellValue(sheet, axis)})
425+
}
426+
}
427+
428+
return mergeCells
429+
}
430+
431+
// MergeCell define a merged cell data.
432+
// It consists of the following structure.
433+
// example: []string{"D4:E10", "cell value"}
434+
type MergeCell []string
435+
436+
// GetCellValue returns merged cell value.
437+
func (m *MergeCell) GetCellValue() string {
438+
return (*m)[1]
439+
}
440+
441+
// GetStartAxis returns the merge start axis.
442+
// example: "C2"
443+
func (m *MergeCell) GetStartAxis() string {
444+
axis := strings.Split((*m)[0], ":")
445+
return axis[0]
446+
}
447+
448+
// GetEndAxis returns the merge end axis.
449+
// example: "D4"
450+
func (m *MergeCell) GetEndAxis() string {
451+
axis := strings.Split((*m)[0], ":")
452+
return axis[1]
453+
}

excelize_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,59 @@ func TestMergeCell(t *testing.T) {
374374
}
375375
}
376376

377+
func TestGetMergeCells(t *testing.T) {
378+
wants := []struct {
379+
value string
380+
start string
381+
end string
382+
}{
383+
{
384+
value: "A1",
385+
start: "A1",
386+
end: "B1",
387+
},
388+
{
389+
value: "A2",
390+
start: "A2",
391+
end: "A3",
392+
},
393+
{
394+
value: "A4",
395+
start: "A4",
396+
end: "B5",
397+
},
398+
{
399+
value: "A7",
400+
start: "A7",
401+
end: "C10",
402+
},
403+
}
404+
405+
xlsx, err := OpenFile("./test/MergeCell.xlsx")
406+
if err != nil {
407+
t.Error(err)
408+
}
409+
410+
mergeCells := xlsx.GetMergeCells("Sheet1")
411+
if len(mergeCells) != len(wants) {
412+
t.Fatalf("Expected count of merge cells %d, but got %d\n", len(wants), len(mergeCells))
413+
}
414+
415+
for i, m := range mergeCells {
416+
if wants[i].value != m.GetCellValue() {
417+
t.Fatalf("Expected merged cell value %s, but got %s\n", wants[i].value, m.GetCellValue())
418+
}
419+
420+
if wants[i].start != m.GetStartAxis() {
421+
t.Fatalf("Expected merged cell value %s, but got %s\n", wants[i].start, m.GetStartAxis())
422+
}
423+
424+
if wants[i].end != m.GetEndAxis() {
425+
t.Fatalf("Expected merged cell value %s, but got %s\n", wants[i].end, m.GetEndAxis())
426+
}
427+
}
428+
}
429+
377430
func TestSetCellStyleAlignment(t *testing.T) {
378431
xlsx, err := OpenFile("./test/Book2.xlsx")
379432
if err != nil {

test/MergeCell.xlsx

8.38 KB
Binary file not shown.

0 commit comments

Comments
 (0)