Skip to content

Commit

Permalink
Resolve #393, upgrade Go module to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed May 2, 2019
1 parent 01a418b commit b1f632d
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 148 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Supports saving a file without losing original charts of XLSX. This library need
### Installation

```bash
go get github.com/360EntSecGroup-Skylar/excelize
go get github.com/360EntSecGroup-Skylar/excelize/v2
```

### Create XLSX file
Expand All @@ -34,7 +34,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -64,7 +64,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -103,7 +103,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -140,7 +140,7 @@ import (
_ "image/jpeg"
_ "image/png"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down
10 changes: 5 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 E
### 安装

```bash
go get github.com/360EntSecGroup-Skylar/excelize
go get github.com/360EntSecGroup-Skylar/excelize/v2
```

### 创建 Excel 文档
Expand All @@ -33,7 +33,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -63,7 +63,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -102,7 +102,7 @@ package main
import (
"fmt"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down Expand Up @@ -140,7 +140,7 @@ import (
_ "image/jpeg"
_ "image/png"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
Expand Down
48 changes: 48 additions & 0 deletions cellmerged.go
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]
}
54 changes: 54 additions & 0 deletions cellmerged_test.go
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")
}
2 changes: 1 addition & 1 deletion chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
// import (
// "fmt"
//
// "github.com/360EntSecGroup-Skylar/excelize"
// "github.com/360EntSecGroup-Skylar/excelize/v2"
// )
//
// func main() {
Expand Down
47 changes: 0 additions & 47 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"io/ioutil"
"os"
"strconv"
"strings"
)

// File define a populated XLSX file struct.
Expand Down Expand Up @@ -215,49 +214,3 @@ func (f *File) UpdateLinkedValue() error {
}
return nil
}

// 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]
}
46 changes: 0 additions & 46 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,52 +407,6 @@ func TestMergeCell(t *testing.T) {
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestMergeCell.xlsx")))
}

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")
}

func TestSetCellStyleAlignment(t *testing.T) {
f, err := prepareTestBook1()
if !assert.NoError(t, err) {
Expand Down
5 changes: 3 additions & 2 deletions go.mod
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
)
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
4 changes: 2 additions & 2 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
// _ "image/jpeg"
// _ "image/png"
//
// "github.com/360EntSecGroup-Skylar/excelize"
// "github.com/360EntSecGroup-Skylar/excelize/v2"
// )
//
// func main() {
Expand Down Expand Up @@ -111,7 +111,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
// _ "image/jpeg"
// "io/ioutil"
//
// "github.com/360EntSecGroup-Skylar/excelize"
// "github.com/360EntSecGroup-Skylar/excelize/v2"
// )
//
// func main() {
Expand Down
2 changes: 1 addition & 1 deletion sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
)
Expand Down
Loading

0 comments on commit b1f632d

Please sign in to comment.