Skip to content

Commit 09485b3

Browse files
committed
Improve code coverage unit tests
1 parent 5c87eff commit 09485b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+383
-320
lines changed

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2016-2019, 360 Enterprise Security Group, Endpoint Security, Inc.
4-
Copyright (c) 2011-2017, Geoffrey J. Teale (complying with the tealeg/xlsx license)
3+
Copyright (c) 2016-2020, 360 Enterprise Security Group, Endpoint Security, Inc.
4+
Copyright (c) 2011-2017, Geoffrey J. Teale
55
All rights reserved.
66

77
Redistribution and use in source and binary forms, with or without

README.md

+25-45
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ Here is a minimal example usage that will create XLSX file.
3131
```go
3232
package main
3333

34-
import (
35-
"fmt"
36-
37-
"github.com/360EntSecGroup-Skylar/excelize"
38-
)
34+
import "github.com/360EntSecGroup-Skylar/excelize"
3935

4036
func main() {
4137
f := excelize.NewFile()
@@ -47,9 +43,8 @@ func main() {
4743
// Set active sheet of the workbook.
4844
f.SetActiveSheet(index)
4945
// Save xlsx file by the given path.
50-
err := f.SaveAs("./Book1.xlsx")
51-
if err != nil {
52-
fmt.Println(err)
46+
if err := f.SaveAs("Book1.xlsx"); err != nil {
47+
println(err.Error())
5348
}
5449
}
5550
```
@@ -61,32 +56,28 @@ The following constitutes the bare to read a XLSX document.
6156
```go
6257
package main
6358

64-
import (
65-
"fmt"
66-
67-
"github.com/360EntSecGroup-Skylar/excelize"
68-
)
59+
import "github.com/360EntSecGroup-Skylar/excelize"
6960

7061
func main() {
71-
f, err := excelize.OpenFile("./Book1.xlsx")
62+
f, err := excelize.OpenFile("Book1.xlsx")
7263
if err != nil {
73-
fmt.Println(err)
64+
println(err.Error())
7465
return
7566
}
7667
// Get value from cell by given worksheet name and axis.
7768
cell, err := f.GetCellValue("Sheet1", "B2")
7869
if err != nil {
79-
fmt.Println(err)
70+
println(err.Error())
8071
return
8172
}
82-
fmt.Println(cell)
73+
println(cell)
8374
// Get all the rows in the Sheet1.
8475
rows, err := f.GetRows("Sheet1")
8576
for _, row := range rows {
8677
for _, colCell := range row {
87-
fmt.Print(colCell, "\t")
78+
print(colCell, "\t")
8879
}
89-
fmt.Println()
80+
println()
9081
}
9182
}
9283
```
@@ -100,11 +91,7 @@ With Excelize chart generation and management is as easy as a few lines of code.
10091
```go
10192
package main
10293

103-
import (
104-
"fmt"
105-
106-
"github.com/360EntSecGroup-Skylar/excelize"
107-
)
94+
import "github.com/360EntSecGroup-Skylar/excelize"
10895

10996
func main() {
11097
categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
@@ -116,15 +103,13 @@ func main() {
116103
for k, v := range values {
117104
f.SetCellValue("Sheet1", k, v)
118105
}
119-
err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
120-
if err != nil {
121-
fmt.Println(err)
106+
if err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`); err != nil {
107+
println(err.Error())
122108
return
123109
}
124110
// Save xlsx file by the given path.
125-
err = f.SaveAs("./Book1.xlsx")
126-
if err != nil {
127-
fmt.Println(err)
111+
if err := f.SaveAs("Book1.xlsx"); err != nil {
112+
println(err.Error())
128113
}
129114
}
130115
```
@@ -135,7 +120,6 @@ func main() {
135120
package main
136121

137122
import (
138-
"fmt"
139123
_ "image/gif"
140124
_ "image/jpeg"
141125
_ "image/png"
@@ -144,30 +128,26 @@ import (
144128
)
145129

146130
func main() {
147-
f, err := excelize.OpenFile("./Book1.xlsx")
131+
f, err := excelize.OpenFile("Book1.xlsx")
148132
if err != nil {
149-
fmt.Println(err)
133+
println(err.Error())
150134
return
151135
}
152136
// Insert a picture.
153-
err = f.AddPicture("Sheet1", "A2", "./image1.png", "")
154-
if err != nil {
155-
fmt.Println(err)
137+
if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
138+
println(err.Error())
156139
}
157140
// Insert a picture to worksheet with scaling.
158-
err = f.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
159-
if err != nil {
160-
fmt.Println(err)
141+
if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
142+
println(err.Error())
161143
}
162144
// Insert a picture offset in the cell with printing support.
163-
err = f.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
164-
if err != nil {
165-
fmt.Println(err)
145+
if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {
146+
println(err.Error())
166147
}
167148
// Save the xlsx file with the origin path.
168-
err = f.Save()
169-
if err != nil {
170-
fmt.Println(err)
149+
if err = f.Save(); err != nil {
150+
println(err.Error())
171151
}
172152
}
173153
```

README_zh.md

+25-46
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ go get github.com/360EntSecGroup-Skylar/excelize
3030
```go
3131
package main
3232

33-
import (
34-
"fmt"
35-
36-
"github.com/360EntSecGroup-Skylar/excelize"
37-
)
33+
import "github.com/360EntSecGroup-Skylar/excelize"
3834

3935
func main() {
4036
f := excelize.NewFile()
@@ -46,9 +42,8 @@ func main() {
4642
// 设置工作簿的默认工作表
4743
f.SetActiveSheet(index)
4844
// 根据指定路径保存文件
49-
err := f.SaveAs("./Book1.xlsx")
50-
if err != nil {
51-
fmt.Println(err)
45+
if err := f.SaveAs("Book1.xlsx"); err != nil {
46+
println(err.Error())
5247
}
5348
}
5449
```
@@ -60,32 +55,28 @@ func main() {
6055
```go
6156
package main
6257

63-
import (
64-
"fmt"
65-
66-
"github.com/360EntSecGroup-Skylar/excelize"
67-
)
58+
import "github.com/360EntSecGroup-Skylar/excelize"
6859

6960
func main() {
70-
f, err := excelize.OpenFile("./Book1.xlsx")
61+
f, err := excelize.OpenFile("Book1.xlsx")
7162
if err != nil {
72-
fmt.Println(err)
63+
println(err.Error())
7364
return
7465
}
7566
// 获取工作表中指定单元格的值
7667
cell, err := f.GetCellValue("Sheet1", "B2")
7768
if err != nil {
78-
fmt.Println(err)
69+
println(err.Error())
7970
return
8071
}
81-
fmt.Println(cell)
72+
println(cell)
8273
// 获取 Sheet1 上所有单元格
8374
rows, err := f.GetRows("Sheet1")
8475
for _, row := range rows {
8576
for _, colCell := range row {
86-
fmt.Print(colCell, "\t")
77+
print(colCell, "\t")
8778
}
88-
fmt.Println()
79+
println()
8980
}
9081
}
9182
```
@@ -99,11 +90,7 @@ func main() {
9990
```go
10091
package main
10192

102-
import (
103-
"fmt"
104-
105-
"github.com/360EntSecGroup-Skylar/excelize"
106-
)
93+
import "github.com/360EntSecGroup-Skylar/excelize"
10794

10895
func main() {
10996
categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"}
@@ -115,18 +102,15 @@ func main() {
115102
for k, v := range values {
116103
f.SetCellValue("Sheet1", k, v)
117104
}
118-
err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
119-
if err != nil {
120-
fmt.Println(err)
105+
if err := f.AddChart("Sheet1", "E1", `{"type":"col3DClustered","series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`); err != nil {
106+
println(err.Error())
121107
return
122108
}
123109
// 根据指定路径保存文件
124-
err = f.SaveAs("./Book1.xlsx")
125-
if err != nil {
126-
fmt.Println(err)
110+
if err := f.SaveAs("Book1.xlsx"); err != nil {
111+
println(err.Error())
127112
}
128113
}
129-
130114
```
131115

132116
### 向 Excel 文档中插入图片
@@ -135,7 +119,6 @@ func main() {
135119
package main
136120

137121
import (
138-
"fmt"
139122
_ "image/gif"
140123
_ "image/jpeg"
141124
_ "image/png"
@@ -144,30 +127,26 @@ import (
144127
)
145128

146129
func main() {
147-
f, err := excelize.OpenFile("./Book1.xlsx")
130+
f, err := excelize.OpenFile("Book1.xlsx")
148131
if err != nil {
149-
fmt.Println(err)
132+
println(err.Error())
150133
return
151134
}
152135
// 插入图片
153-
err = f.AddPicture("Sheet1", "A2", "./image1.png", "")
154-
if err != nil {
155-
fmt.Println(err)
136+
if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
137+
println(err.Error())
156138
}
157139
// 在工作表中插入图片,并设置图片的缩放比例
158-
err = f.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
159-
if err != nil {
160-
fmt.Println(err)
140+
if err := f.AddPicture("Sheet1", "D2", "image.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
141+
println(err.Error())
161142
}
162143
// 在工作表中插入图片,并设置图片的打印属性
163-
err = f.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
164-
if err != nil {
165-
fmt.Println(err)
144+
if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {
145+
println(err.Error())
166146
}
167147
// 保存文件
168-
err = f.Save()
169-
if err != nil {
170-
fmt.Println(err)
148+
if err = f.Save(); err != nil {
149+
println(err.Error())
171150
}
172151
}
173152
```

adjust.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
22
// this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44
//
@@ -213,6 +213,8 @@ func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
213213
return coordinates, err
214214
}
215215

216+
// sortCoordinates provides a function to correct the coordinate area, such
217+
// correct C1:B3 to B1:C3.
216218
func sortCoordinates(coordinates []int) error {
217219
if len(coordinates) != 4 {
218220
return errors.New("coordinates length must be 4")

adjust_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ func TestCoordinatesToAreaRef(t *testing.T) {
114114
assert.NoError(t, err)
115115
assert.EqualValues(t, ref, "A1:A1")
116116
}
117+
118+
func TestSortCoordinates(t *testing.T) {
119+
assert.EqualError(t, sortCoordinates(make([]int, 3)), "coordinates length must be 4")
120+
}

calcchain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
22
// this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44
//

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
22
// this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44
//

cellmerged.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
22
// this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44
//
@@ -43,13 +43,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) error {
4343
return err
4444
}
4545
// Correct the coordinate area, such correct C1:B3 to B1:C3.
46-
if rect1[2] < rect1[0] {
47-
rect1[0], rect1[2] = rect1[2], rect1[0]
48-
}
49-
50-
if rect1[3] < rect1[1] {
51-
rect1[1], rect1[3] = rect1[3], rect1[1]
52-
}
46+
_ = sortCoordinates(rect1)
5347

5448
hcell, _ = CoordinatesToCellName(rect1[0], rect1[1])
5549
vcell, _ = CoordinatesToCellName(rect1[2], rect1[3])
@@ -123,12 +117,8 @@ func (f *File) UnmergeCell(sheet string, hcell, vcell string) error {
123117
return err
124118
}
125119

126-
if rect1[2] < rect1[0] {
127-
rect1[0], rect1[2] = rect1[2], rect1[0]
128-
}
129-
if rect1[3] < rect1[1] {
130-
rect1[1], rect1[3] = rect1[3], rect1[1]
131-
}
120+
// Correct the coordinate area, such correct C1:B3 to B1:C3.
121+
_ = sortCoordinates(rect1)
132122

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

chart.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
22
// this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44
//
@@ -657,6 +657,7 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
657657
//
658658
// major_grid_lines
659659
// minor_grid_lines
660+
// tick_label_skip
660661
// reverse_order
661662
// maximum
662663
// minimum
@@ -666,7 +667,6 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
666667
// major_grid_lines
667668
// minor_grid_lines
668669
// major_unit
669-
// tick_label_skip
670670
// reverse_order
671671
// maximum
672672
// minimum

0 commit comments

Comments
 (0)