Skip to content

Commit

Permalink
Resolve #527, unmerge an area (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdvd authored and xuri committed Dec 13, 2019
1 parent 5d8365c commit 4c433c5
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cellmerged.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@ func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
return mergeCells, err
}

// UnmergeCell provides a function to unmerge a given coordinate area.
// For example unmerge area D3:E9 on Sheet1:
//
// err := f.UnmergeCell("Sheet1", "D3", "E9")
//
// Attention: overlapped areas will also be unmerged.
func (f *File) UnmergeCell(sheet string, hcell, vcell string) error {
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
coordinates, err := f.areaRefToCoordinates(hcell + ":" + vcell)
if err != nil {
return err
}
x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]

if x2 < x1 {
x1, x2 = x2, x1
}
if y2 < y1 {
y1, y2 = y2, y1
}
hcell, _ = CoordinatesToCellName(x1, y1)
vcell, _ = CoordinatesToCellName(x2, y2)

// return nil since no MergeCells in the sheet
if xlsx.MergeCells == nil {
return nil
}

ref := hcell + ":" + vcell
i := 0
for _, cellData := range xlsx.MergeCells.Cells {
cc := strings.Split(cellData.Ref, ":")
c1, _ := checkCellInArea(hcell, cellData.Ref)
c2, _ := checkCellInArea(vcell, cellData.Ref)
c3, _ := checkCellInArea(cc[0], ref)
c4, _ := checkCellInArea(cc[1], ref)
// skip the overlapped mergecell
if c1 || c2 || c3 || c4 {
continue
}
xlsx.MergeCells.Cells[i] = cellData
i++
}
xlsx.MergeCells.Cells = xlsx.MergeCells.Cells[:i]
return nil
}

// MergeCell define a merged cell data.
// It consists of the following structure.
// example: []string{"D4:E10", "cell value"}
Expand Down
36 changes: 36 additions & 0 deletions cellmerged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,39 @@ func TestGetMergeCells(t *testing.T) {
_, err = f.GetMergeCells("SheetN")
assert.EqualError(t, err, "sheet SheetN is not exist")
}

func TestUnmergeCell(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx"))
if !assert.NoError(t, err) {
t.FailNow()
}
sheet1 := f.GetSheetName(1)

xlsx, err := f.workSheetReader(sheet1)
assert.NoError(t, err)

mergeCellNum := len(xlsx.MergeCells.Cells)

assert.EqualError(t, f.UnmergeCell("Sheet1", "A", "A"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)

// unmerge the mergecell that contains A1
err = f.UnmergeCell(sheet1, "A1", "A1")
assert.NoError(t, err)

if len(xlsx.MergeCells.Cells) != mergeCellNum-1 {
t.FailNow()
}

// unmerge area A7:D3(A3:D7)
// this will unmerge all since this area overlaps with all others
err = f.UnmergeCell(sheet1, "D7", "A3")
assert.NoError(t, err)

if len(xlsx.MergeCells.Cells) != 0 {
t.FailNow()
}

// Test unmerged area on not exists worksheet.
err = f.UnmergeCell("SheetN", "A1", "A1")
assert.EqualError(t, err, "sheet SheetN is not exist")
}

0 comments on commit 4c433c5

Please sign in to comment.