Skip to content

Commit

Permalink
This fixed worksheet protection issue
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
xuri committed Jan 2, 2023
1 parent f58dabd commit b39626f
Show file tree
Hide file tree
Showing 65 changed files with 498 additions and 378 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2016-2022 The excelize Authors.
Copyright (c) 2016-2023 The excelize Authors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ import (

func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Create a new sheet.
index := f.NewSheet("Sheet2")
index, err := f.NewSheet("Sheet2")
if err != nil {
fmt.Println(err)
return
}
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
Expand Down Expand Up @@ -122,6 +131,11 @@ import (

func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for idx, row := range [][]interface{}{
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
Expand Down Expand Up @@ -196,14 +210,14 @@ func main() {
fmt.Println(err)
}
// Insert a picture to worksheet with scaling.
enable, disable, scale := true, false, 0.5
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
&excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
fmt.Println(err)
}
// Insert a picture offset in the cell with printing support.
enable, disable := true, false
if err := f.AddPicture("Sheet1", "H2", "image.gif",
&excelize.PictureOptions{
&excelize.GraphicOptions{
PrintObject: &enable,
LockAspectRatio: false,
OffsetX: 15,
Expand Down
22 changes: 18 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ import (

func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// 创建一个工作表
index := f.NewSheet("Sheet2")
index, err := f.NewSheet("Sheet2")
if err != nil {
fmt.Println(err)
return
}
// 设置单元格的值
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
Expand Down Expand Up @@ -122,6 +131,11 @@ import (

func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for idx, row := range [][]interface{}{
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
Expand Down Expand Up @@ -196,14 +210,14 @@ func main() {
fmt.Println(err)
}
// 在工作表中插入图片,并设置图片的缩放比例
enable, disable, scale := true, false, 0.5
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
&excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
fmt.Println(err)
}
// 在工作表中插入图片,并设置图片的打印属性
enable, disable := true, false
if err := f.AddPicture("Sheet1", "H2", "image.gif",
&excelize.PictureOptions{
&excelize.GraphicOptions{
PrintObject: &enable,
LockAspectRatio: false,
OffsetX: 15,
Expand Down
4 changes: 2 additions & 2 deletions adjust.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
Expand All @@ -7,7 +7,7 @@
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.15 or later.
// data. This library needs Go version 1.16 or later.

package excelize

Expand Down
4 changes: 2 additions & 2 deletions calc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
Expand All @@ -7,7 +7,7 @@
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.15 or later.
// data. This library needs Go version 1.16 or later.

package excelize

Expand Down
4 changes: 2 additions & 2 deletions calcchain.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
Expand All @@ -7,7 +7,7 @@
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.15 or later.
// data. This library needs Go version 1.16 or later.

package excelize

Expand Down
42 changes: 26 additions & 16 deletions cell.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
Expand All @@ -7,7 +7,7 @@
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.15 or later.
// data. This library needs Go version 1.16 or later.

package excelize

Expand Down Expand Up @@ -679,6 +679,11 @@ type FormulaOpts struct {
//
// func main() {
// f := excelize.NewFile()
// defer func() {
// if err := f.Close(); err != nil {
// fmt.Println(err)
// }
// }()
// for idx, row := range [][]interface{}{{"A", "B", "C"}, {1, 2}} {
// if err := f.SetSheetRow("Sheet1", fmt.Sprintf("A%d", idx+1), &row); err != nil {
// fmt.Println(err)
Expand Down Expand Up @@ -1044,6 +1049,11 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
//
// func main() {
// f := excelize.NewFile()
// defer func() {
// if err := f.Close(); err != nil {
// fmt.Println(err)
// }
// }()
// if err := f.SetRowHeight("Sheet1", 1, 35); err != nil {
// fmt.Println(err)
// return
Expand Down Expand Up @@ -1395,39 +1405,39 @@ func (f *File) mergeCellsParser(ws *xlsxWorksheet, cell string) (string, error)

// checkCellInRangeRef provides a function to determine if a given cell reference
// in a range.
func (f *File) checkCellInRangeRef(cell, reference string) (bool, error) {
func (f *File) checkCellInRangeRef(cell, rangeRef string) (bool, error) {
col, row, err := CellNameToCoordinates(cell)
if err != nil {
return false, err
}

if rng := strings.Split(reference, ":"); len(rng) != 2 {
if rng := strings.Split(rangeRef, ":"); len(rng) != 2 {
return false, err
}
coordinates, err := rangeRefToCoordinates(reference)
coordinates, err := rangeRefToCoordinates(rangeRef)
if err != nil {
return false, err
}

return cellInRef([]int{col, row}, coordinates), err
return cellInRange([]int{col, row}, coordinates), err
}

// cellInRef provides a function to determine if a given range is within a
// cellInRange provides a function to determine if a given range is within a
// range.
func cellInRef(cell, ref []int) bool {
func cellInRange(cell, ref []int) bool {
return cell[0] >= ref[0] && cell[0] <= ref[2] && cell[1] >= ref[1] && cell[1] <= ref[3]
}

// isOverlap find if the given two rectangles overlap or not.
func isOverlap(rect1, rect2 []int) bool {
return cellInRef([]int{rect1[0], rect1[1]}, rect2) ||
cellInRef([]int{rect1[2], rect1[1]}, rect2) ||
cellInRef([]int{rect1[0], rect1[3]}, rect2) ||
cellInRef([]int{rect1[2], rect1[3]}, rect2) ||
cellInRef([]int{rect2[0], rect2[1]}, rect1) ||
cellInRef([]int{rect2[2], rect2[1]}, rect1) ||
cellInRef([]int{rect2[0], rect2[3]}, rect1) ||
cellInRef([]int{rect2[2], rect2[3]}, rect1)
return cellInRange([]int{rect1[0], rect1[1]}, rect2) ||
cellInRange([]int{rect1[2], rect1[1]}, rect2) ||
cellInRange([]int{rect1[0], rect1[3]}, rect2) ||
cellInRange([]int{rect1[2], rect1[3]}, rect2) ||
cellInRange([]int{rect2[0], rect2[1]}, rect1) ||
cellInRange([]int{rect2[2], rect2[1]}, rect1) ||
cellInRange([]int{rect2[0], rect2[3]}, rect1) ||
cellInRange([]int{rect2[2], rect2[3]}, rect1)
}

// parseSharedFormula generate dynamic part of shared formula for target cell
Expand Down
13 changes: 11 additions & 2 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestConcurrency(t *testing.T) {
assert.NoError(t, f.SetCellStyle("Sheet1", "A3", "A3", style))
// Concurrency add picture
assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
&PictureOptions{
&GraphicOptions{
OffsetX: 10,
OffsetY: 10,
Hyperlink: "https://github.com/xuri/excelize",
Expand Down Expand Up @@ -475,11 +475,20 @@ func TestGetCellFormula(t *testing.T) {

func ExampleFile_SetCellFloat() {
f := NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
x := 3.14159265
if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
fmt.Println(err)
}
val, _ := f.GetCellValue("Sheet1", "A1")
val, err := f.GetCellValue("Sheet1", "A1")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(val)
// Output: 3.14
}
Expand Down
Loading

0 comments on commit b39626f

Please sign in to comment.