@@ -13,6 +13,8 @@ import (
1313 "errors"
1414 "math"
1515 "strings"
16+
17+ "github.com/mohae/deepcopy"
1618)
1719
1820// Define the default cell size and EMU unit of measurement.
@@ -59,7 +61,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
5961//
6062// err := f.SetColVisible("Sheet1", "D", false)
6163//
62- // Hide the columns from D to F (included)
64+ // Hide the columns from D to F (included):
6365//
6466// err := f.SetColVisible("Sheet1", "D:F", false)
6567//
@@ -87,23 +89,31 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
8789 return err
8890 }
8991 colData := xlsxCol {
90- Min : min ,
91- Max : max ,
92- Width : 9 , // default width
93- Hidden : ! visible ,
92+ Min : min ,
93+ Max : max ,
94+ Width : 9 , // default width
95+ Hidden : ! visible ,
9496 CustomWidth : true ,
9597 }
96- if xlsx .Cols != nil {
97- xlsx .Cols .Col = append (xlsx .Cols .Col , colData )
98- } else {
98+ if xlsx .Cols == nil {
9999 cols := xlsxCols {}
100100 cols .Col = append (cols .Col , colData )
101101 xlsx .Cols = & cols
102- }
102+ return nil
103+ }
104+ xlsx .Cols .Col = flatCols (colData , xlsx .Cols .Col , func (fc , c xlsxCol ) xlsxCol {
105+ fc .BestFit = c .BestFit
106+ fc .Collapsed = c .Collapsed
107+ fc .CustomWidth = c .CustomWidth
108+ fc .OutlineLevel = c .OutlineLevel
109+ fc .Phonetic = c .Phonetic
110+ fc .Style = c .Style
111+ fc .Width = c .Width
112+ return fc
113+ })
103114 return nil
104115}
105116
106-
107117// GetColOutlineLevel provides a function to get outline level of a single
108118// column by given worksheet name and column name. For example, get outline
109119// level of column D in Sheet1:
@@ -162,16 +172,16 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
162172 xlsx .Cols = & cols
163173 return err
164174 }
165- for v := range xlsx .Cols .Col {
166- if xlsx . Cols . Col [ v ]. Min <= colNum && colNum <= xlsx . Cols . Col [ v ]. Max {
167- colData = xlsx . Cols . Col [ v ]
168- }
169- }
170- colData . Min = colNum
171- colData . Max = colNum
172- colData . OutlineLevel = level
173- colData . CustomWidth = true
174- xlsx . Cols . Col = append ( xlsx . Cols . Col , colData )
175+ xlsx . Cols . Col = flatCols ( colData , xlsx .Cols .Col , func ( fc , c xlsxCol ) xlsxCol {
176+ fc . BestFit = c . BestFit
177+ fc . Collapsed = c . Collapsed
178+ fc . CustomWidth = c . CustomWidth
179+ fc . Hidden = c . Hidden
180+ fc . Phonetic = c . Phonetic
181+ fc . Style = c . Style
182+ fc . Width = c . Width
183+ return fc
184+ } )
175185 return err
176186}
177187
@@ -214,21 +224,21 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
214224 if xlsx .Cols == nil {
215225 xlsx .Cols = & xlsxCols {}
216226 }
217- var find bool
218- for idx , col := range xlsx . Cols . Col {
219- if col . Min == min && col . Max == max {
220- xlsx . Cols . Col [ idx ]. Style = styleID
221- find = true
222- }
223- }
224- if ! find {
225- xlsx . Cols . Col = append ( xlsx . Cols . Col , xlsxCol {
226- Min : min ,
227- Max : max ,
228- Width : 9 ,
229- Style : styleID ,
230- })
231- }
227+ xlsx . Cols . Col = flatCols ( xlsxCol {
228+ Min : min ,
229+ Max : max ,
230+ Width : 9 ,
231+ Style : styleID ,
232+ }, xlsx . Cols . Col , func ( fc , c xlsxCol ) xlsxCol {
233+ fc . BestFit = c . BestFit
234+ fc . Collapsed = c . Collapsed
235+ fc . CustomWidth = c . CustomWidth
236+ fc . Hidden = c . Hidden
237+ fc . OutlineLevel = c . OutlineLevel
238+ fc . Phonetic = c . Phonetic
239+ fc . Width = c . Width
240+ return fc
241+ })
232242 return nil
233243}
234244
@@ -261,16 +271,55 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error
261271 Width : width ,
262272 CustomWidth : true ,
263273 }
264- if xlsx .Cols != nil {
265- xlsx .Cols .Col = append (xlsx .Cols .Col , col )
266- } else {
274+ if xlsx .Cols == nil {
267275 cols := xlsxCols {}
268276 cols .Col = append (cols .Col , col )
269277 xlsx .Cols = & cols
278+ return err
270279 }
280+ xlsx .Cols .Col = flatCols (col , xlsx .Cols .Col , func (fc , c xlsxCol ) xlsxCol {
281+ fc .BestFit = c .BestFit
282+ fc .Collapsed = c .Collapsed
283+ fc .Hidden = c .Hidden
284+ fc .OutlineLevel = c .OutlineLevel
285+ fc .Phonetic = c .Phonetic
286+ fc .Style = c .Style
287+ return fc
288+ })
271289 return err
272290}
273291
292+ // flatCols provides a method for the column's operation functions to flatten
293+ // and check the worksheet columns.
294+ func flatCols (col xlsxCol , cols []xlsxCol , replacer func (fc , c xlsxCol ) xlsxCol ) []xlsxCol {
295+ fc := []xlsxCol {}
296+ for i := col .Min ; i <= col .Max ; i ++ {
297+ c := deepcopy .Copy (col ).(xlsxCol )
298+ c .Min , c .Max = i , i
299+ fc = append (fc , c )
300+ }
301+ inFlat := func (colID int , cols []xlsxCol ) (int , bool ) {
302+ for idx , c := range cols {
303+ if c .Max == colID && c .Min == colID {
304+ return idx , true
305+ }
306+ }
307+ return - 1 , false
308+ }
309+ for _ , column := range cols {
310+ for i := column .Min ; i <= column .Max ; i ++ {
311+ if idx , ok := inFlat (i , fc ); ok {
312+ fc [idx ] = replacer (fc [idx ], column )
313+ continue
314+ }
315+ c := deepcopy .Copy (column ).(xlsxCol )
316+ c .Min , c .Max = i , i
317+ fc = append (fc , c )
318+ }
319+ }
320+ return fc
321+ }
322+
274323// positionObjectPixels calculate the vertices that define the position of a
275324// graphical object within the worksheet in pixels.
276325//
0 commit comments