Resolve #393, upgrade Go module to v2

This commit is contained in:
xuri 2019-04-30 18:39:27 +08:00
parent 01a418bda8
commit b1f632d408
13 changed files with 157 additions and 148 deletions

View File

@ -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
@ -34,7 +34,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -64,7 +64,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -103,7 +103,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -140,7 +140,7 @@ import (
_ "image/jpeg"
_ "image/png"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {

View File

@ -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 文档
@ -33,7 +33,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -63,7 +63,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -102,7 +102,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {
@ -140,7 +140,7 @@ import (
_ "image/jpeg"
_ "image/png"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
func main() {

48
cellmerged.go Normal file
View File

@ -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
cellmerged_test.go Normal file
View File

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

View File

@ -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() {

View File

@ -19,7 +19,6 @@ import (
"io/ioutil"
"os"
"strconv"
"strings"
)
// File define a populated XLSX file struct.
@ -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]
}

View File

@ -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) {

5
go.mod
View File

@ -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
go.sum
View File

@ -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=

View File

@ -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() {
@ -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() {

View File

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

View File

@ -7,7 +7,7 @@ import (
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
var _ = []excelize.SheetPrOption{
@ -29,10 +29,10 @@ var _ = []excelize.SheetPrOptionPtr{
}
func ExampleFile_SetSheetPrOptions() {
xl := excelize.NewFile()
f := excelize.NewFile()
const sheet = "Sheet1"
if err := xl.SetSheetPrOptions(sheet,
if err := f.SetSheetPrOptions(sheet,
excelize.CodeName("code"),
excelize.EnableFormatConditionsCalculation(false),
excelize.Published(false),
@ -46,7 +46,7 @@ func ExampleFile_SetSheetPrOptions() {
}
func ExampleFile_GetSheetPrOptions() {
xl := excelize.NewFile()
f := excelize.NewFile()
const sheet = "Sheet1"
var (
@ -58,7 +58,7 @@ func ExampleFile_GetSheetPrOptions() {
outlineSummaryBelow excelize.OutlineSummaryBelow
)
if err := xl.GetSheetPrOptions(sheet,
if err := f.GetSheetPrOptions(sheet,
&codeName,
&enableFormatConditionsCalculation,
&published,
@ -110,26 +110,26 @@ func TestSheetPrOptions(t *testing.T) {
val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
xl := excelize.NewFile()
f := excelize.NewFile()
// Get the default value
assert.NoError(t, xl.GetSheetPrOptions(sheet, def), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
// Get again and check
assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, val1, def, opt) {
t.FailNow()
}
// Set the same value
assert.NoError(t, xl.SetSheetPrOptions(sheet, val1), opt)
assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
// Get again and check
assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
t.FailNow()
}
// Set a different value
assert.NoError(t, xl.SetSheetPrOptions(sheet, test.nonDefault), opt)
assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
// Get again and compare
assert.NoError(t, xl.GetSheetPrOptions(sheet, val2), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
t.FailNow()
}
@ -138,8 +138,8 @@ func TestSheetPrOptions(t *testing.T) {
t.FailNow()
}
// Restore the default value
assert.NoError(t, xl.SetSheetPrOptions(sheet, def), opt)
assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, def, val1) {
t.FailNow()
}

View File

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
var _ = []excelize.SheetViewOption{
@ -35,10 +35,10 @@ var _ = []excelize.SheetViewOptionPtr{
}
func ExampleFile_SetSheetViewOptions() {
xl := excelize.NewFile()
f := excelize.NewFile()
const sheet = "Sheet1"
if err := xl.SetSheetViewOptions(sheet, 0,
if err := f.SetSheetViewOptions(sheet, 0,
excelize.DefaultGridColor(false),
excelize.RightToLeft(false),
excelize.ShowFormulas(true),
@ -54,22 +54,22 @@ func ExampleFile_SetSheetViewOptions() {
fmt.Println("Default:")
fmt.Println("- zoomScale: 80")
if err := xl.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
panic(err)
}
if err := xl.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
panic(err)
}
fmt.Println("Used out of range value:")
fmt.Println("- zoomScale:", zoomScale)
if err := xl.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
panic(err)
}
if err := xl.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
panic(err)
}
@ -87,7 +87,7 @@ func ExampleFile_SetSheetViewOptions() {
}
func ExampleFile_GetSheetViewOptions() {
xl := excelize.NewFile()
f := excelize.NewFile()
const sheet = "Sheet1"
var (
@ -100,7 +100,7 @@ func ExampleFile_GetSheetViewOptions() {
topLeftCell excelize.TopLeftCell
)
if err := xl.GetSheetViewOptions(sheet, 0,
if err := f.GetSheetViewOptions(sheet, 0,
&defaultGridColor,
&rightToLeft,
&showFormulas,
@ -121,19 +121,19 @@ func ExampleFile_GetSheetViewOptions() {
fmt.Println("- zoomScale:", zoomScale)
fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
if err := xl.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
panic(err)
}
if err := xl.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
panic(err)
}
if err := xl.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
panic(err)
}
if err := xl.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
panic(err)
}
@ -156,15 +156,15 @@ func ExampleFile_GetSheetViewOptions() {
}
func TestSheetViewOptionsErrors(t *testing.T) {
xl := excelize.NewFile()
f := excelize.NewFile()
const sheet = "Sheet1"
assert.NoError(t, xl.GetSheetViewOptions(sheet, 0))
assert.NoError(t, xl.GetSheetViewOptions(sheet, -1))
assert.Error(t, xl.GetSheetViewOptions(sheet, 1))
assert.Error(t, xl.GetSheetViewOptions(sheet, -2))
assert.NoError(t, xl.SetSheetViewOptions(sheet, 0))
assert.NoError(t, xl.SetSheetViewOptions(sheet, -1))
assert.Error(t, xl.SetSheetViewOptions(sheet, 1))
assert.Error(t, xl.SetSheetViewOptions(sheet, -2))
assert.NoError(t, f.GetSheetViewOptions(sheet, 0))
assert.NoError(t, f.GetSheetViewOptions(sheet, -1))
assert.Error(t, f.GetSheetViewOptions(sheet, 1))
assert.Error(t, f.GetSheetViewOptions(sheet, -2))
assert.NoError(t, f.SetSheetViewOptions(sheet, 0))
assert.NoError(t, f.SetSheetViewOptions(sheet, -1))
assert.Error(t, f.SetSheetViewOptions(sheet, 1))
assert.Error(t, f.SetSheetViewOptions(sheet, -2))
}