-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve #393, upgrade Go module to v2
- Loading branch information
Showing
13 changed files
with
157 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package excelize | ||
|
||
import "strings" | ||
|
||
// GetMergeCells provides a function to get all merged cells from a worksheet currently. | ||
func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) { | ||
var mergeCells []MergeCell | ||
xlsx, err := f.workSheetReader(sheet) | ||
if err != nil { | ||
return mergeCells, err | ||
} | ||
if xlsx.MergeCells != nil { | ||
mergeCells = make([]MergeCell, 0, len(xlsx.MergeCells.Cells)) | ||
|
||
for i := range xlsx.MergeCells.Cells { | ||
ref := xlsx.MergeCells.Cells[i].Ref | ||
axis := strings.Split(ref, ":")[0] | ||
val, _ := f.GetCellValue(sheet, axis) | ||
mergeCells = append(mergeCells, []string{ref, val}) | ||
} | ||
} | ||
|
||
return mergeCells, err | ||
} | ||
|
||
// MergeCell define a merged cell data. | ||
// It consists of the following structure. | ||
// example: []string{"D4:E10", "cell value"} | ||
type MergeCell []string | ||
|
||
// GetCellValue returns merged cell value. | ||
func (m *MergeCell) GetCellValue() string { | ||
return (*m)[1] | ||
} | ||
|
||
// GetStartAxis returns the merge start axis. | ||
// example: "C2" | ||
func (m *MergeCell) GetStartAxis() string { | ||
axis := strings.Split((*m)[0], ":") | ||
return axis[0] | ||
} | ||
|
||
// GetEndAxis returns the merge end axis. | ||
// example: "D4" | ||
func (m *MergeCell) GetEndAxis() string { | ||
axis := strings.Split((*m)[0], ":") | ||
return axis[1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package excelize | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetMergeCells(t *testing.T) { | ||
wants := []struct { | ||
value string | ||
start string | ||
end string | ||
}{{ | ||
value: "A1", | ||
start: "A1", | ||
end: "B1", | ||
}, { | ||
value: "A2", | ||
start: "A2", | ||
end: "A3", | ||
}, { | ||
value: "A4", | ||
start: "A4", | ||
end: "B5", | ||
}, { | ||
value: "A7", | ||
start: "A7", | ||
end: "C10", | ||
}} | ||
|
||
f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx")) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
sheet1 := f.GetSheetName(1) | ||
|
||
mergeCells, err := f.GetMergeCells(sheet1) | ||
if !assert.Len(t, mergeCells, len(wants)) { | ||
t.FailNow() | ||
} | ||
assert.NoError(t, err) | ||
|
||
for i, m := range mergeCells { | ||
assert.Equal(t, wants[i].value, m.GetCellValue()) | ||
assert.Equal(t, wants[i].start, m.GetStartAxis()) | ||
assert.Equal(t, wants[i].end, m.GetEndAxis()) | ||
} | ||
|
||
// Test get merged cells on not exists worksheet. | ||
_, err = f.GetMergeCells("SheetN") | ||
assert.EqualError(t, err, "sheet SheetN is not exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
module github.com/360EntSecGroup-Skylar/excelize | ||
module github.com/360EntSecGroup-Skylar/excelize/v2 | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 | ||
github.com/stretchr/testify v1.3.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.