@@ -13,6 +13,8 @@ import (
13
13
"errors"
14
14
"math"
15
15
"strings"
16
+
17
+ "github.com/mohae/deepcopy"
16
18
)
17
19
18
20
// Define the default cell size and EMU unit of measurement.
@@ -59,7 +61,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
59
61
//
60
62
// err := f.SetColVisible("Sheet1", "D", false)
61
63
//
62
- // Hide the columns from D to F (included)
64
+ // Hide the columns from D to F (included):
63
65
//
64
66
// err := f.SetColVisible("Sheet1", "D:F", false)
65
67
//
@@ -87,23 +89,31 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
87
89
return err
88
90
}
89
91
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 ,
94
96
CustomWidth : true ,
95
97
}
96
- if xlsx .Cols != nil {
97
- xlsx .Cols .Col = append (xlsx .Cols .Col , colData )
98
- } else {
98
+ if xlsx .Cols == nil {
99
99
cols := xlsxCols {}
100
100
cols .Col = append (cols .Col , colData )
101
101
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
+ })
103
114
return nil
104
115
}
105
116
106
-
107
117
// GetColOutlineLevel provides a function to get outline level of a single
108
118
// column by given worksheet name and column name. For example, get outline
109
119
// level of column D in Sheet1:
@@ -162,16 +172,16 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
162
172
xlsx .Cols = & cols
163
173
return err
164
174
}
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
+ } )
175
185
return err
176
186
}
177
187
@@ -214,21 +224,21 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
214
224
if xlsx .Cols == nil {
215
225
xlsx .Cols = & xlsxCols {}
216
226
}
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
+ })
232
242
return nil
233
243
}
234
244
@@ -261,16 +271,55 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error
261
271
Width : width ,
262
272
CustomWidth : true ,
263
273
}
264
- if xlsx .Cols != nil {
265
- xlsx .Cols .Col = append (xlsx .Cols .Col , col )
266
- } else {
274
+ if xlsx .Cols == nil {
267
275
cols := xlsxCols {}
268
276
cols .Col = append (cols .Col , col )
269
277
xlsx .Cols = & cols
278
+ return err
270
279
}
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
+ })
271
289
return err
272
290
}
273
291
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
+
274
323
// positionObjectPixels calculate the vertices that define the position of a
275
324
// graphical object within the worksheet in pixels.
276
325
//
0 commit comments