Skip to content

Commit 40ff5dc

Browse files
committed
refactor: handler error instead of panic,
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
1 parent 2874d75 commit 40ff5dc

23 files changed

+692
-540
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
~$*.xlsx
22
test/Test*.xlsx
33
*.out
4-
test/image3.png
54
*.test

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
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/).
1717

18-
**WARNING!**
19-
20-
From version 1.5.0 all row manipulation methods uses Excel row numbering starting with `1` instead of zero-based numbering
21-
which take place in some methods in eraler versions.
22-
2318
## Basic Usage
2419

2520
### Installation
@@ -123,7 +118,6 @@ func main() {
123118
fmt.Println(err)
124119
}
125120
}
126-
127121
```
128122

129123
### Add picture to XLSX file

README_zh.md

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
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/)
1717

18-
**重要提示**
19-
20-
从版本 1.5.0 开始,所有行操作方法都使用从 `1` 开始的 Excel 行编号,早期版本中某些方法中的基于 `0` 的行编号将不再使用。
21-
2218
## 快速上手
2319

2420
### 安装

adjust.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030
// TODO: adjustCalcChain, adjustPageBreaks, adjustComments,
3131
// adjustDataValidations, adjustProtectedCells
3232
//
33-
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) {
33+
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
3434
xlsx := f.workSheetReader(sheet)
3535

3636
if dir == rows {
@@ -39,11 +39,16 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
3939
f.adjustColDimensions(xlsx, num, offset)
4040
}
4141
f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
42-
f.adjustMergeCells(xlsx, dir, num, offset)
43-
f.adjustAutoFilter(xlsx, dir, num, offset)
42+
if err := f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
43+
return err
44+
}
45+
if err := f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
46+
return err
47+
}
4448

4549
checkSheet(xlsx)
4650
checkRow(xlsx)
51+
return nil
4752
}
4853

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

128133
// adjustAutoFilter provides a function to update the auto filter when
129134
// inserting or deleting rows or columns.
130-
func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
135+
func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
131136
if xlsx.AutoFilter == nil {
132-
return
137+
return nil
133138
}
134139

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

139144
firstCol, firstRow, err := CellNameToCoordinates(firstCell)
140145
if err != nil {
141-
panic(err)
146+
return err
142147
}
143148

144149
lastCol, lastRow, err := CellNameToCoordinates(lastCell)
145150
if err != nil {
146-
panic(err)
151+
return err
147152
}
148153

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

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

173178
xlsx.AutoFilter.Ref = firstCell + ":" + lastCell
179+
return nil
174180
}
175181

176182
// adjustMergeCells provides a function to update merged cells when inserting
177183
// or deleting rows or columns.
178-
func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
184+
func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
179185
if xlsx.MergeCells == nil {
180-
return
186+
return nil
181187
}
182188

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

188194
firstCol, firstRow, err := CellNameToCoordinates(firstCell)
189195
if err != nil {
190-
panic(err)
196+
return err
191197
}
192198

193199
lastCol, lastRow, err := CellNameToCoordinates(lastCell)
194200
if err != nil {
195-
panic(err)
201+
return err
196202
}
197203

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

226232
if firstCell, err = CoordinatesToCellName(firstCol, firstRow); err != nil {
227-
panic(err)
233+
return err
228234
}
229235

230236
if lastCell, err = CoordinatesToCellName(lastCol, lastRow); err != nil {
231-
panic(err)
237+
return err
232238
}
233239

234240
areaData.Ref = firstCell + ":" + lastCell
235241
}
242+
return nil
236243
}

0 commit comments

Comments
 (0)