From 40ff5dc1a7d7aa42f5db9cf9dfe858cc3820b44e Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 23 Mar 2019 20:08:06 +0800 Subject: 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 --- adjust.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'adjust.go') diff --git a/adjust.go b/adjust.go index e69eee1..009860b 100644 --- a/adjust.go +++ b/adjust.go @@ -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 { @@ -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 @@ -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, ":") @@ -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) { @@ -154,7 +159,7 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o rowData.Hidden = false } } - return + return nil } if dir == rows { @@ -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 { @@ -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 { @@ -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 } -- cgit v1.2.1