diff options
author | xuri <xuri.me@gmail.com> | 2022-09-07 00:18:16 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2022-09-07 00:18:16 +0800 |
commit | 0c5cdfec1868f31f6e355cdcb0a91220bad80522 (patch) | |
tree | 58ad95b35e5ab305bf969a168f065acf894fc1eb /adjust.go | |
parent | 961a3e89330ab2cd5257e04384813a7e53ea3744 (diff) |
This closes #1293, add new function `GetColStyle`
- Fix generate workbook corruption after insert cols/rows in some case
- Update unit tests
- Update dependencies module
Diffstat (limited to 'adjust.go')
-rw-r--r-- | adjust.go | 28 |
1 files changed, 19 insertions, 9 deletions
@@ -74,13 +74,18 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) // inserting or deleting rows or columns. func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error { for rowIdx := range ws.SheetData.Row { + for _, v := range ws.SheetData.Row[rowIdx].C { + if cellCol, _, _ := CellNameToCoordinates(v.R); col <= cellCol { + if newCol := cellCol + offset; newCol > 0 && newCol > MaxColumns { + return ErrColumnNumber + } + } + } + } + for rowIdx := range ws.SheetData.Row { for colIdx, v := range ws.SheetData.Row[rowIdx].C { - cellCol, cellRow, _ := CellNameToCoordinates(v.R) - if col <= cellCol { + if cellCol, cellRow, _ := CellNameToCoordinates(v.R); col <= cellCol { if newCol := cellCol + offset; newCol > 0 { - if newCol > MaxColumns { - return ErrColumnNumber - } ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow) } } @@ -92,12 +97,17 @@ func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error { // adjustRowDimensions provides a function to update row dimensions when // inserting or deleting rows or columns. func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) error { - for i := range ws.SheetData.Row { + totalRows := len(ws.SheetData.Row) + if totalRows == 0 { + return nil + } + lastRow := &ws.SheetData.Row[totalRows-1] + if newRow := lastRow.R + offset; lastRow.R >= row && newRow > 0 && newRow >= TotalRows { + return ErrMaxRows + } + for i := 0; i < len(ws.SheetData.Row); i++ { r := &ws.SheetData.Row[i] if newRow := r.R + offset; r.R >= row && newRow > 0 { - if newRow >= TotalRows { - return ErrMaxRows - } f.adjustSingleRowDimensions(r, newRow) } } |