Skip to content

Commit b39626f

Browse files
committed
This fixed worksheet protection issue
- Update example code in the documentation - Update unit tests - Rename `PictureOptions` to `GraphicOptions` - Adjust partial options fields data types for the `PictureOptions` and `Shape` structure - Update dependencies module
1 parent f58dabd commit b39626f

Some content is hidden

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

65 files changed

+498
-378
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2016-2022 The excelize Authors.
3+
Copyright (c) 2016-2023 The excelize Authors.
44
All rights reserved.
55

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

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ import (
4444

4545
func main() {
4646
f := excelize.NewFile()
47+
defer func() {
48+
if err := f.Close(); err != nil {
49+
fmt.Println(err)
50+
}
51+
}()
4752
// Create a new sheet.
48-
index := f.NewSheet("Sheet2")
53+
index, err := f.NewSheet("Sheet2")
54+
if err != nil {
55+
fmt.Println(err)
56+
return
57+
}
4958
// Set value of a cell.
5059
f.SetCellValue("Sheet2", "A2", "Hello world.")
5160
f.SetCellValue("Sheet1", "B2", 100)
@@ -122,6 +131,11 @@ import (
122131

123132
func main() {
124133
f := excelize.NewFile()
134+
defer func() {
135+
if err := f.Close(); err != nil {
136+
fmt.Println(err)
137+
}
138+
}()
125139
for idx, row := range [][]interface{}{
126140
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
127141
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
@@ -196,14 +210,14 @@ func main() {
196210
fmt.Println(err)
197211
}
198212
// Insert a picture to worksheet with scaling.
199-
enable, disable, scale := true, false, 0.5
200213
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
201-
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
214+
&excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
202215
fmt.Println(err)
203216
}
204217
// Insert a picture offset in the cell with printing support.
218+
enable, disable := true, false
205219
if err := f.AddPicture("Sheet1", "H2", "image.gif",
206-
&excelize.PictureOptions{
220+
&excelize.GraphicOptions{
207221
PrintObject: &enable,
208222
LockAspectRatio: false,
209223
OffsetX: 15,

README_zh.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ import (
4444

4545
func main() {
4646
f := excelize.NewFile()
47+
defer func() {
48+
if err := f.Close(); err != nil {
49+
fmt.Println(err)
50+
}
51+
}()
4752
// 创建一个工作表
48-
index := f.NewSheet("Sheet2")
53+
index, err := f.NewSheet("Sheet2")
54+
if err != nil {
55+
fmt.Println(err)
56+
return
57+
}
4958
// 设置单元格的值
5059
f.SetCellValue("Sheet2", "A2", "Hello world.")
5160
f.SetCellValue("Sheet1", "B2", 100)
@@ -122,6 +131,11 @@ import (
122131

123132
func main() {
124133
f := excelize.NewFile()
134+
defer func() {
135+
if err := f.Close(); err != nil {
136+
fmt.Println(err)
137+
}
138+
}()
125139
for idx, row := range [][]interface{}{
126140
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
127141
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
@@ -196,14 +210,14 @@ func main() {
196210
fmt.Println(err)
197211
}
198212
// 在工作表中插入图片,并设置图片的缩放比例
199-
enable, disable, scale := true, false, 0.5
200213
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
201-
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
214+
&excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
202215
fmt.Println(err)
203216
}
204217
// 在工作表中插入图片,并设置图片的打印属性
218+
enable, disable := true, false
205219
if err := f.AddPicture("Sheet1", "H2", "image.gif",
206-
&excelize.PictureOptions{
220+
&excelize.GraphicOptions{
207221
PrintObject: &enable,
208222
LockAspectRatio: false,
209223
OffsetX: 15,

adjust.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2023 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
//
@@ -7,7 +7,7 @@
77
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
88
// Supports complex components by high compatibility, and provided streaming
99
// API for generating or reading data from a worksheet with huge amounts of
10-
// data. This library needs Go version 1.15 or later.
10+
// data. This library needs Go version 1.16 or later.
1111

1212
package excelize
1313

calc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2023 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
//
@@ -7,7 +7,7 @@
77
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
88
// Supports complex components by high compatibility, and provided streaming
99
// API for generating or reading data from a worksheet with huge amounts of
10-
// data. This library needs Go version 1.15 or later.
10+
// data. This library needs Go version 1.16 or later.
1111

1212
package excelize
1313

calcchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2023 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
//
@@ -7,7 +7,7 @@
77
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
88
// Supports complex components by high compatibility, and provided streaming
99
// API for generating or reading data from a worksheet with huge amounts of
10-
// data. This library needs Go version 1.15 or later.
10+
// data. This library needs Go version 1.16 or later.
1111

1212
package excelize
1313

cell.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
1+
// Copyright 2016 - 2023 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
//
@@ -7,7 +7,7 @@
77
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
88
// Supports complex components by high compatibility, and provided streaming
99
// API for generating or reading data from a worksheet with huge amounts of
10-
// data. This library needs Go version 1.15 or later.
10+
// data. This library needs Go version 1.16 or later.
1111

1212
package excelize
1313

@@ -679,6 +679,11 @@ type FormulaOpts struct {
679679
//
680680
// func main() {
681681
// f := excelize.NewFile()
682+
// defer func() {
683+
// if err := f.Close(); err != nil {
684+
// fmt.Println(err)
685+
// }
686+
// }()
682687
// for idx, row := range [][]interface{}{{"A", "B", "C"}, {1, 2}} {
683688
// if err := f.SetSheetRow("Sheet1", fmt.Sprintf("A%d", idx+1), &row); err != nil {
684689
// fmt.Println(err)
@@ -1044,6 +1049,11 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
10441049
//
10451050
// func main() {
10461051
// f := excelize.NewFile()
1052+
// defer func() {
1053+
// if err := f.Close(); err != nil {
1054+
// fmt.Println(err)
1055+
// }
1056+
// }()
10471057
// if err := f.SetRowHeight("Sheet1", 1, 35); err != nil {
10481058
// fmt.Println(err)
10491059
// return
@@ -1395,39 +1405,39 @@ func (f *File) mergeCellsParser(ws *xlsxWorksheet, cell string) (string, error)
13951405

13961406
// checkCellInRangeRef provides a function to determine if a given cell reference
13971407
// in a range.
1398-
func (f *File) checkCellInRangeRef(cell, reference string) (bool, error) {
1408+
func (f *File) checkCellInRangeRef(cell, rangeRef string) (bool, error) {
13991409
col, row, err := CellNameToCoordinates(cell)
14001410
if err != nil {
14011411
return false, err
14021412
}
14031413

1404-
if rng := strings.Split(reference, ":"); len(rng) != 2 {
1414+
if rng := strings.Split(rangeRef, ":"); len(rng) != 2 {
14051415
return false, err
14061416
}
1407-
coordinates, err := rangeRefToCoordinates(reference)
1417+
coordinates, err := rangeRefToCoordinates(rangeRef)
14081418
if err != nil {
14091419
return false, err
14101420
}
14111421

1412-
return cellInRef([]int{col, row}, coordinates), err
1422+
return cellInRange([]int{col, row}, coordinates), err
14131423
}
14141424

1415-
// cellInRef provides a function to determine if a given range is within a
1425+
// cellInRange provides a function to determine if a given range is within a
14161426
// range.
1417-
func cellInRef(cell, ref []int) bool {
1427+
func cellInRange(cell, ref []int) bool {
14181428
return cell[0] >= ref[0] && cell[0] <= ref[2] && cell[1] >= ref[1] && cell[1] <= ref[3]
14191429
}
14201430

14211431
// isOverlap find if the given two rectangles overlap or not.
14221432
func isOverlap(rect1, rect2 []int) bool {
1423-
return cellInRef([]int{rect1[0], rect1[1]}, rect2) ||
1424-
cellInRef([]int{rect1[2], rect1[1]}, rect2) ||
1425-
cellInRef([]int{rect1[0], rect1[3]}, rect2) ||
1426-
cellInRef([]int{rect1[2], rect1[3]}, rect2) ||
1427-
cellInRef([]int{rect2[0], rect2[1]}, rect1) ||
1428-
cellInRef([]int{rect2[2], rect2[1]}, rect1) ||
1429-
cellInRef([]int{rect2[0], rect2[3]}, rect1) ||
1430-
cellInRef([]int{rect2[2], rect2[3]}, rect1)
1433+
return cellInRange([]int{rect1[0], rect1[1]}, rect2) ||
1434+
cellInRange([]int{rect1[2], rect1[1]}, rect2) ||
1435+
cellInRange([]int{rect1[0], rect1[3]}, rect2) ||
1436+
cellInRange([]int{rect1[2], rect1[3]}, rect2) ||
1437+
cellInRange([]int{rect2[0], rect2[1]}, rect1) ||
1438+
cellInRange([]int{rect2[2], rect2[1]}, rect1) ||
1439+
cellInRange([]int{rect2[0], rect2[3]}, rect1) ||
1440+
cellInRange([]int{rect2[2], rect2[3]}, rect1)
14311441
}
14321442

14331443
// parseSharedFormula generate dynamic part of shared formula for target cell

cell_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestConcurrency(t *testing.T) {
4343
assert.NoError(t, f.SetCellStyle("Sheet1", "A3", "A3", style))
4444
// Concurrency add picture
4545
assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
46-
&PictureOptions{
46+
&GraphicOptions{
4747
OffsetX: 10,
4848
OffsetY: 10,
4949
Hyperlink: "https://github.com/xuri/excelize",
@@ -475,11 +475,20 @@ func TestGetCellFormula(t *testing.T) {
475475

476476
func ExampleFile_SetCellFloat() {
477477
f := NewFile()
478+
defer func() {
479+
if err := f.Close(); err != nil {
480+
fmt.Println(err)
481+
}
482+
}()
478483
x := 3.14159265
479484
if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
480485
fmt.Println(err)
481486
}
482-
val, _ := f.GetCellValue("Sheet1", "A1")
487+
val, err := f.GetCellValue("Sheet1", "A1")
488+
if err != nil {
489+
fmt.Println(err)
490+
return
491+
}
483492
fmt.Println(val)
484493
// Output: 3.14
485494
}

0 commit comments

Comments
 (0)