Skip to content

Commit

Permalink
refactor: handler error instead of panic,
Browse files Browse the repository at this point in the history
Exported functions:

SetCellStyle
InsertCol
RemoveCol
RemoveRow
InsertRow
DuplicateRow
DuplicateRowTo
SetRowHeight
GetRowHeight
GetCellValue
GetCellFormula
GetCellHyperLink
SetCellHyperLink
SetCellInt
SetCellBool
SetCellFloat
SetCellStr
SetCellDefault
GetCellStyle
SetCellValue
MergeCell
SetSheetRow
SetRowVisible
GetRowVisible
SetRowOutlineLevel
GetRowOutlineLevel
GetRows
Columns
SearchSheet
AddTable
GetPicture
AutoFilter
GetColVisible
SetColVisible
GetColOutlineLevel
SetColOutlineLevel
SetColWidth
GetColWidth

inner functions:

adjustHelper
adjustMergeCells
adjustAutoFilter
prepareCell
setDefaultTimeStyle
timeToExcelTime
addDrawingChart
addDrawingVML
addDrawingPicture
getTotalRowsCols
checkRow
addDrawingShape
addTable
  • Loading branch information
xuri committed Mar 23, 2019
1 parent 2874d75 commit 40ff5dc
Show file tree
Hide file tree
Showing 23 changed files with 692 additions and 540 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
~$*.xlsx
test/Test*.xlsx
*.out
test/image3.png
*.test
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@

Excelize is a library written in pure Go and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Microsoft Excel™ 2007 and later. Support save file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) and [docs reference](https://xuri.me/excelize/).

**WARNING!**

From version 1.5.0 all row manipulation methods uses Excel row numbering starting with `1` instead of zero-based numbering
which take place in some methods in eraler versions.

## Basic Usage

### Installation
Expand Down Expand Up @@ -123,7 +118,6 @@ func main() {
fmt.Println(err)
}
}

```

### Add picture to XLSX file
Expand Down
4 changes: 0 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 ECMA-376 Office OpenXML 标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的 XLSX 文档。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)、透视表和切片器等复杂样式的文档,还支持向 Excel 文档中插入图片与图表,并且在保存后不会丢失文档原有样式,可以应用于各类报表系统中。使用本类库要求使用的 Go 语言为 1.8 或更高版本,完整的 API 使用文档请访问 [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) 或查看 [参考文档](https://xuri.me/excelize/)

**重要提示**

从版本 1.5.0 开始,所有行操作方法都使用从 `1` 开始的 Excel 行编号,早期版本中某些方法中的基于 `0` 的行编号将不再使用。

## 快速上手

### 安装
Expand Down
35 changes: 21 additions & 14 deletions adjust.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
// TODO: adjustCalcChain, adjustPageBreaks, adjustComments,
// adjustDataValidations, adjustProtectedCells
//
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) {
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
xlsx := f.workSheetReader(sheet)

if dir == rows {
Expand All @@ -39,11 +39,16 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
f.adjustColDimensions(xlsx, num, offset)
}
f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
f.adjustMergeCells(xlsx, dir, num, offset)
f.adjustAutoFilter(xlsx, dir, num, offset)
if err := f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
return err
}
if err := f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
return err
}

checkSheet(xlsx)
checkRow(xlsx)
return nil
}

// adjustColDimensions provides a function to update column dimensions when
Expand Down Expand Up @@ -127,9 +132,9 @@ func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDir

// adjustAutoFilter provides a function to update the auto filter when
// inserting or deleting rows or columns.
func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
if xlsx.AutoFilter == nil {
return
return nil
}

rng := strings.Split(xlsx.AutoFilter.Ref, ":")
Expand All @@ -138,12 +143,12 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o

firstCol, firstRow, err := CellNameToCoordinates(firstCell)
if err != nil {
panic(err)
return err
}

lastCol, lastRow, err := CellNameToCoordinates(lastCell)
if err != nil {
panic(err)
return err
}

if (dir == rows && firstRow == num && offset < 0) || (dir == columns && firstCol == num && lastCol == num) {
Expand All @@ -154,7 +159,7 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o
rowData.Hidden = false
}
}
return
return nil
}

if dir == rows {
Expand All @@ -171,13 +176,14 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o
}

xlsx.AutoFilter.Ref = firstCell + ":" + lastCell
return nil
}

// adjustMergeCells provides a function to update merged cells when inserting
// or deleting rows or columns.
func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
if xlsx.MergeCells == nil {
return
return nil
}

for i, areaData := range xlsx.MergeCells.Cells {
Expand All @@ -187,12 +193,12 @@ func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, o

firstCol, firstRow, err := CellNameToCoordinates(firstCell)
if err != nil {
panic(err)
return err
}

lastCol, lastRow, err := CellNameToCoordinates(lastCell)
if err != nil {
panic(err)
return err
}

adjust := func(v int) int {
Expand Down Expand Up @@ -224,13 +230,14 @@ func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, o
}

if firstCell, err = CoordinatesToCellName(firstCol, firstRow); err != nil {
panic(err)
return err
}

if lastCell, err = CoordinatesToCellName(lastCol, lastRow); err != nil {
panic(err)
return err
}

areaData.Ref = firstCell + ":" + lastCell
}
return nil
}
Loading

0 comments on commit 40ff5dc

Please sign in to comment.