• R/O
  • HTTP
  • SSH
  • HTTPS

excelize: 提交

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets


Commit MetaInfo

修订版b1f632d4084130628f10906ff6a7bb55022e4c08 (tree)
时间2019-05-02 16:57:47
作者xuri <xuri.me@gmai...>
Commiterxuri

Log Message

Resolve #393, upgrade Go module to v2

更改概述

差异

--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ Supports saving a file without losing original charts of XLSX. This library need
2121 ### Installation
2222
2323 ```bash
24-go get github.com/360EntSecGroup-Skylar/excelize
24+go get github.com/360EntSecGroup-Skylar/excelize/v2
2525 ```
2626
2727 ### Create XLSX file
@@ -34,7 +34,7 @@ package main
3434 import (
3535 "fmt"
3636
37- "github.com/360EntSecGroup-Skylar/excelize"
37+ "github.com/360EntSecGroup-Skylar/excelize/v2"
3838 )
3939
4040 func main() {
@@ -64,7 +64,7 @@ package main
6464 import (
6565 "fmt"
6666
67- "github.com/360EntSecGroup-Skylar/excelize"
67+ "github.com/360EntSecGroup-Skylar/excelize/v2"
6868 )
6969
7070 func main() {
@@ -103,7 +103,7 @@ package main
103103 import (
104104 "fmt"
105105
106- "github.com/360EntSecGroup-Skylar/excelize"
106+ "github.com/360EntSecGroup-Skylar/excelize/v2"
107107 )
108108
109109 func main() {
@@ -140,7 +140,7 @@ import (
140140 _ "image/jpeg"
141141 _ "image/png"
142142
143- "github.com/360EntSecGroup-Skylar/excelize"
143+ "github.com/360EntSecGroup-Skylar/excelize/v2"
144144 )
145145
146146 func main() {
--- a/README_zh.md
+++ b/README_zh.md
@@ -20,7 +20,7 @@ Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 E
2020 ### 安装
2121
2222 ```bash
23-go get github.com/360EntSecGroup-Skylar/excelize
23+go get github.com/360EntSecGroup-Skylar/excelize/v2
2424 ```
2525
2626 ### 创建 Excel 文档
@@ -33,7 +33,7 @@ package main
3333 import (
3434 "fmt"
3535
36- "github.com/360EntSecGroup-Skylar/excelize"
36+ "github.com/360EntSecGroup-Skylar/excelize/v2"
3737 )
3838
3939 func main() {
@@ -63,7 +63,7 @@ package main
6363 import (
6464 "fmt"
6565
66- "github.com/360EntSecGroup-Skylar/excelize"
66+ "github.com/360EntSecGroup-Skylar/excelize/v2"
6767 )
6868
6969 func main() {
@@ -102,7 +102,7 @@ package main
102102 import (
103103 "fmt"
104104
105- "github.com/360EntSecGroup-Skylar/excelize"
105+ "github.com/360EntSecGroup-Skylar/excelize/v2"
106106 )
107107
108108 func main() {
@@ -140,7 +140,7 @@ import (
140140 _ "image/jpeg"
141141 _ "image/png"
142142
143- "github.com/360EntSecGroup-Skylar/excelize"
143+ "github.com/360EntSecGroup-Skylar/excelize/v2"
144144 )
145145
146146 func main() {
--- /dev/null
+++ b/cellmerged.go
@@ -0,0 +1,48 @@
1+package excelize
2+
3+import "strings"
4+
5+// GetMergeCells provides a function to get all merged cells from a worksheet currently.
6+func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
7+ var mergeCells []MergeCell
8+ xlsx, err := f.workSheetReader(sheet)
9+ if err != nil {
10+ return mergeCells, err
11+ }
12+ if xlsx.MergeCells != nil {
13+ mergeCells = make([]MergeCell, 0, len(xlsx.MergeCells.Cells))
14+
15+ for i := range xlsx.MergeCells.Cells {
16+ ref := xlsx.MergeCells.Cells[i].Ref
17+ axis := strings.Split(ref, ":")[0]
18+ val, _ := f.GetCellValue(sheet, axis)
19+ mergeCells = append(mergeCells, []string{ref, val})
20+ }
21+ }
22+
23+ return mergeCells, err
24+}
25+
26+// MergeCell define a merged cell data.
27+// It consists of the following structure.
28+// example: []string{"D4:E10", "cell value"}
29+type MergeCell []string
30+
31+// GetCellValue returns merged cell value.
32+func (m *MergeCell) GetCellValue() string {
33+ return (*m)[1]
34+}
35+
36+// GetStartAxis returns the merge start axis.
37+// example: "C2"
38+func (m *MergeCell) GetStartAxis() string {
39+ axis := strings.Split((*m)[0], ":")
40+ return axis[0]
41+}
42+
43+// GetEndAxis returns the merge end axis.
44+// example: "D4"
45+func (m *MergeCell) GetEndAxis() string {
46+ axis := strings.Split((*m)[0], ":")
47+ return axis[1]
48+}
\ No newline at end of file
--- /dev/null
+++ b/cellmerged_test.go
@@ -0,0 +1,54 @@
1+package excelize
2+
3+import (
4+ "path/filepath"
5+ "testing"
6+
7+ "github.com/stretchr/testify/assert"
8+)
9+
10+func TestGetMergeCells(t *testing.T) {
11+ wants := []struct {
12+ value string
13+ start string
14+ end string
15+ }{{
16+ value: "A1",
17+ start: "A1",
18+ end: "B1",
19+ }, {
20+ value: "A2",
21+ start: "A2",
22+ end: "A3",
23+ }, {
24+ value: "A4",
25+ start: "A4",
26+ end: "B5",
27+ }, {
28+ value: "A7",
29+ start: "A7",
30+ end: "C10",
31+ }}
32+
33+ f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx"))
34+ if !assert.NoError(t, err) {
35+ t.FailNow()
36+ }
37+ sheet1 := f.GetSheetName(1)
38+
39+ mergeCells, err := f.GetMergeCells(sheet1)
40+ if !assert.Len(t, mergeCells, len(wants)) {
41+ t.FailNow()
42+ }
43+ assert.NoError(t, err)
44+
45+ for i, m := range mergeCells {
46+ assert.Equal(t, wants[i].value, m.GetCellValue())
47+ assert.Equal(t, wants[i].start, m.GetStartAxis())
48+ assert.Equal(t, wants[i].end, m.GetEndAxis())
49+ }
50+
51+ // Test get merged cells on not exists worksheet.
52+ _, err = f.GetMergeCells("SheetN")
53+ assert.EqualError(t, err, "sheet SheetN is not exist")
54+}
--- a/chart.go
+++ b/chart.go
@@ -308,7 +308,7 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
308308 // import (
309309 // "fmt"
310310 //
311-// "github.com/360EntSecGroup-Skylar/excelize"
311+// "github.com/360EntSecGroup-Skylar/excelize/v2"
312312 // )
313313 //
314314 // func main() {
--- a/excelize.go
+++ b/excelize.go
@@ -19,7 +19,6 @@ import (
1919 "io/ioutil"
2020 "os"
2121 "strconv"
22- "strings"
2322 )
2423
2524 // File define a populated XLSX file struct.
@@ -215,49 +214,3 @@ func (f *File) UpdateLinkedValue() error {
215214 }
216215 return nil
217216 }
218-
219-// GetMergeCells provides a function to get all merged cells from a worksheet
220-// currently.
221-func (f *File) GetMergeCells(sheet string) ([]MergeCell, error) {
222- var mergeCells []MergeCell
223- xlsx, err := f.workSheetReader(sheet)
224- if err != nil {
225- return mergeCells, err
226- }
227- if xlsx.MergeCells != nil {
228- mergeCells = make([]MergeCell, 0, len(xlsx.MergeCells.Cells))
229-
230- for i := range xlsx.MergeCells.Cells {
231- ref := xlsx.MergeCells.Cells[i].Ref
232- axis := strings.Split(ref, ":")[0]
233- val, _ := f.GetCellValue(sheet, axis)
234- mergeCells = append(mergeCells, []string{ref, val})
235- }
236- }
237-
238- return mergeCells, err
239-}
240-
241-// MergeCell define a merged cell data.
242-// It consists of the following structure.
243-// example: []string{"D4:E10", "cell value"}
244-type MergeCell []string
245-
246-// GetCellValue returns merged cell value.
247-func (m *MergeCell) GetCellValue() string {
248- return (*m)[1]
249-}
250-
251-// GetStartAxis returns the merge start axis.
252-// example: "C2"
253-func (m *MergeCell) GetStartAxis() string {
254- axis := strings.Split((*m)[0], ":")
255- return axis[0]
256-}
257-
258-// GetEndAxis returns the merge end axis.
259-// example: "D4"
260-func (m *MergeCell) GetEndAxis() string {
261- axis := strings.Split((*m)[0], ":")
262- return axis[1]
263-}
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -407,52 +407,6 @@ func TestMergeCell(t *testing.T) {
407407 assert.NoError(t, f.SaveAs(filepath.Join("test", "TestMergeCell.xlsx")))
408408 }
409409
410-func TestGetMergeCells(t *testing.T) {
411- wants := []struct {
412- value string
413- start string
414- end string
415- }{{
416- value: "A1",
417- start: "A1",
418- end: "B1",
419- }, {
420- value: "A2",
421- start: "A2",
422- end: "A3",
423- }, {
424- value: "A4",
425- start: "A4",
426- end: "B5",
427- }, {
428- value: "A7",
429- start: "A7",
430- end: "C10",
431- }}
432-
433- f, err := OpenFile(filepath.Join("test", "MergeCell.xlsx"))
434- if !assert.NoError(t, err) {
435- t.FailNow()
436- }
437- sheet1 := f.GetSheetName(1)
438-
439- mergeCells, err := f.GetMergeCells(sheet1)
440- if !assert.Len(t, mergeCells, len(wants)) {
441- t.FailNow()
442- }
443- assert.NoError(t, err)
444-
445- for i, m := range mergeCells {
446- assert.Equal(t, wants[i].value, m.GetCellValue())
447- assert.Equal(t, wants[i].start, m.GetStartAxis())
448- assert.Equal(t, wants[i].end, m.GetEndAxis())
449- }
450-
451- // Test get merged cells on not exists worksheet.
452- _, err = f.GetMergeCells("SheetN")
453- assert.EqualError(t, err, "sheet SheetN is not exist")
454-}
455-
456410 func TestSetCellStyleAlignment(t *testing.T) {
457411 f, err := prepareTestBook1()
458412 if !assert.NoError(t, err) {
--- a/go.mod
+++ b/go.mod
@@ -1,7 +1,8 @@
1-module github.com/360EntSecGroup-Skylar/excelize
1+module github.com/360EntSecGroup-Skylar/excelize/v2
2+
3+go 1.12
24
35 require (
4- github.com/davecgh/go-spew v1.1.1 // indirect
56 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
67 github.com/stretchr/testify v1.3.0
78 )
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,5 @@
1+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
12 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
54 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
65 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
--- a/picture.go
+++ b/picture.go
@@ -51,7 +51,7 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
5151 // _ "image/jpeg"
5252 // _ "image/png"
5353 //
54-// "github.com/360EntSecGroup-Skylar/excelize"
54+// "github.com/360EntSecGroup-Skylar/excelize/v2"
5555 // )
5656 //
5757 // func main() {
@@ -111,7 +111,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
111111 // _ "image/jpeg"
112112 // "io/ioutil"
113113 //
114-// "github.com/360EntSecGroup-Skylar/excelize"
114+// "github.com/360EntSecGroup-Skylar/excelize/v2"
115115 // )
116116 //
117117 // func main() {
--- a/sheet_test.go
+++ b/sheet_test.go
@@ -4,7 +4,7 @@ import (
44 "fmt"
55 "testing"
66
7- "github.com/360EntSecGroup-Skylar/excelize"
7+ "github.com/360EntSecGroup-Skylar/excelize/v2"
88 "github.com/mohae/deepcopy"
99 "github.com/stretchr/testify/assert"
1010 )
--- a/sheetpr_test.go
+++ b/sheetpr_test.go
@@ -7,7 +7,7 @@ import (
77 "github.com/mohae/deepcopy"
88 "github.com/stretchr/testify/assert"
99
10- "github.com/360EntSecGroup-Skylar/excelize"
10+ "github.com/360EntSecGroup-Skylar/excelize/v2"
1111 )
1212
1313 var _ = []excelize.SheetPrOption{
@@ -29,10 +29,10 @@ var _ = []excelize.SheetPrOptionPtr{
2929 }
3030
3131 func ExampleFile_SetSheetPrOptions() {
32- xl := excelize.NewFile()
32+ f := excelize.NewFile()
3333 const sheet = "Sheet1"
3434
35- if err := xl.SetSheetPrOptions(sheet,
35+ if err := f.SetSheetPrOptions(sheet,
3636 excelize.CodeName("code"),
3737 excelize.EnableFormatConditionsCalculation(false),
3838 excelize.Published(false),
@@ -46,7 +46,7 @@ func ExampleFile_SetSheetPrOptions() {
4646 }
4747
4848 func ExampleFile_GetSheetPrOptions() {
49- xl := excelize.NewFile()
49+ f := excelize.NewFile()
5050 const sheet = "Sheet1"
5151
5252 var (
@@ -58,7 +58,7 @@ func ExampleFile_GetSheetPrOptions() {
5858 outlineSummaryBelow excelize.OutlineSummaryBelow
5959 )
6060
61- if err := xl.GetSheetPrOptions(sheet,
61+ if err := f.GetSheetPrOptions(sheet,
6262 &codeName,
6363 &enableFormatConditionsCalculation,
6464 &published,
@@ -110,26 +110,26 @@ func TestSheetPrOptions(t *testing.T) {
110110 val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
111111 val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
112112
113- xl := excelize.NewFile()
113+ f := excelize.NewFile()
114114 // Get the default value
115- assert.NoError(t, xl.GetSheetPrOptions(sheet, def), opt)
115+ assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
116116 // Get again and check
117- assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
117+ assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
118118 if !assert.Equal(t, val1, def, opt) {
119119 t.FailNow()
120120 }
121121 // Set the same value
122- assert.NoError(t, xl.SetSheetPrOptions(sheet, val1), opt)
122+ assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
123123 // Get again and check
124- assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
124+ assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
125125 if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
126126 t.FailNow()
127127 }
128128 // Set a different value
129- assert.NoError(t, xl.SetSheetPrOptions(sheet, test.nonDefault), opt)
130- assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
129+ assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
130+ assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
131131 // Get again and compare
132- assert.NoError(t, xl.GetSheetPrOptions(sheet, val2), opt)
132+ assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
133133 if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
134134 t.FailNow()
135135 }
@@ -138,8 +138,8 @@ func TestSheetPrOptions(t *testing.T) {
138138 t.FailNow()
139139 }
140140 // Restore the default value
141- assert.NoError(t, xl.SetSheetPrOptions(sheet, def), opt)
142- assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt)
141+ assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
142+ assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
143143 if !assert.Equal(t, def, val1) {
144144 t.FailNow()
145145 }
--- a/sheetview_test.go
+++ b/sheetview_test.go
@@ -6,7 +6,7 @@ import (
66
77 "github.com/stretchr/testify/assert"
88
9- "github.com/360EntSecGroup-Skylar/excelize"
9+ "github.com/360EntSecGroup-Skylar/excelize/v2"
1010 )
1111
1212 var _ = []excelize.SheetViewOption{
@@ -35,10 +35,10 @@ var _ = []excelize.SheetViewOptionPtr{
3535 }
3636
3737 func ExampleFile_SetSheetViewOptions() {
38- xl := excelize.NewFile()
38+ f := excelize.NewFile()
3939 const sheet = "Sheet1"
4040
41- if err := xl.SetSheetViewOptions(sheet, 0,
41+ if err := f.SetSheetViewOptions(sheet, 0,
4242 excelize.DefaultGridColor(false),
4343 excelize.RightToLeft(false),
4444 excelize.ShowFormulas(true),
@@ -54,22 +54,22 @@ func ExampleFile_SetSheetViewOptions() {
5454 fmt.Println("Default:")
5555 fmt.Println("- zoomScale: 80")
5656
57- if err := xl.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
57+ if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
5858 panic(err)
5959 }
6060
61- if err := xl.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
61+ if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
6262 panic(err)
6363 }
6464
6565 fmt.Println("Used out of range value:")
6666 fmt.Println("- zoomScale:", zoomScale)
6767
68- if err := xl.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
68+ if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
6969 panic(err)
7070 }
7171
72- if err := xl.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
72+ if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
7373 panic(err)
7474 }
7575
@@ -87,7 +87,7 @@ func ExampleFile_SetSheetViewOptions() {
8787 }
8888
8989 func ExampleFile_GetSheetViewOptions() {
90- xl := excelize.NewFile()
90+ f := excelize.NewFile()
9191 const sheet = "Sheet1"
9292
9393 var (
@@ -100,7 +100,7 @@ func ExampleFile_GetSheetViewOptions() {
100100 topLeftCell excelize.TopLeftCell
101101 )
102102
103- if err := xl.GetSheetViewOptions(sheet, 0,
103+ if err := f.GetSheetViewOptions(sheet, 0,
104104 &defaultGridColor,
105105 &rightToLeft,
106106 &showFormulas,
@@ -121,19 +121,19 @@ func ExampleFile_GetSheetViewOptions() {
121121 fmt.Println("- zoomScale:", zoomScale)
122122 fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
123123
124- if err := xl.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
124+ if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
125125 panic(err)
126126 }
127127
128- if err := xl.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
128+ if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
129129 panic(err)
130130 }
131131
132- if err := xl.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
132+ if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
133133 panic(err)
134134 }
135135
136- if err := xl.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
136+ if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
137137 panic(err)
138138 }
139139
@@ -156,15 +156,15 @@ func ExampleFile_GetSheetViewOptions() {
156156 }
157157
158158 func TestSheetViewOptionsErrors(t *testing.T) {
159- xl := excelize.NewFile()
159+ f := excelize.NewFile()
160160 const sheet = "Sheet1"
161161
162- assert.NoError(t, xl.GetSheetViewOptions(sheet, 0))
163- assert.NoError(t, xl.GetSheetViewOptions(sheet, -1))
164- assert.Error(t, xl.GetSheetViewOptions(sheet, 1))
165- assert.Error(t, xl.GetSheetViewOptions(sheet, -2))
166- assert.NoError(t, xl.SetSheetViewOptions(sheet, 0))
167- assert.NoError(t, xl.SetSheetViewOptions(sheet, -1))
168- assert.Error(t, xl.SetSheetViewOptions(sheet, 1))
169- assert.Error(t, xl.SetSheetViewOptions(sheet, -2))
162+ assert.NoError(t, f.GetSheetViewOptions(sheet, 0))
163+ assert.NoError(t, f.GetSheetViewOptions(sheet, -1))
164+ assert.Error(t, f.GetSheetViewOptions(sheet, 1))
165+ assert.Error(t, f.GetSheetViewOptions(sheet, -2))
166+ assert.NoError(t, f.SetSheetViewOptions(sheet, 0))
167+ assert.NoError(t, f.SetSheetViewOptions(sheet, -1))
168+ assert.Error(t, f.SetSheetViewOptions(sheet, 1))
169+ assert.Error(t, f.SetSheetViewOptions(sheet, -2))
170170 }
Show on old repository browser