From 58b5dae5eb4948a3cde238ced1ae05db159749f5 Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 11 Nov 2022 01:50:07 +0800 Subject: Support update column style when inserting or deleting columns - Go Modules dependencies upgrade - Unify internal variable name - Unit test updated --- adjust.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'adjust.go') diff --git a/adjust.go b/adjust.go index 65e82fc..bf89927 100644 --- a/adjust.go +++ b/adjust.go @@ -70,6 +70,50 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) return nil } +// adjustCols provides a function to update column style when inserting or +// deleting columns. +func (f *File) adjustCols(ws *xlsxWorksheet, col, offset int) error { + if ws.Cols == nil { + return nil + } + for i := 0; i < len(ws.Cols.Col); i++ { + if offset > 0 { + if ws.Cols.Col[i].Max+1 == col { + ws.Cols.Col[i].Max += offset + continue + } + if ws.Cols.Col[i].Min >= col { + ws.Cols.Col[i].Min += offset + ws.Cols.Col[i].Max += offset + continue + } + if ws.Cols.Col[i].Min < col && ws.Cols.Col[i].Max >= col { + ws.Cols.Col[i].Max += offset + } + } + if offset < 0 { + if ws.Cols.Col[i].Min == col && ws.Cols.Col[i].Max == col { + if len(ws.Cols.Col) > 1 { + ws.Cols.Col = append(ws.Cols.Col[:i], ws.Cols.Col[i+1:]...) + } else { + ws.Cols.Col = nil + } + i-- + continue + } + if ws.Cols.Col[i].Min > col { + ws.Cols.Col[i].Min += offset + ws.Cols.Col[i].Max += offset + continue + } + if ws.Cols.Col[i].Min <= col && ws.Cols.Col[i].Max >= col { + ws.Cols.Col[i].Max += offset + } + } + } + return nil +} + // adjustColDimensions provides a function to update column dimensions when // inserting or deleting rows or columns. func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error { @@ -91,7 +135,7 @@ func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error { } } } - return nil + return f.adjustCols(ws, col, offset) } // adjustRowDimensions provides a function to update row dimensions when -- cgit v1.2.1