@@ -149,11 +149,12 @@ func (f *File) setContentTypes(index int) {
149
149
150
150
// setSheet provides a function to update sheet property by given index.
151
151
func (f * File ) setSheet (index int , name string ) {
152
- var xlsx xlsxWorksheet
153
- xlsx .Dimension .Ref = "A1"
154
- xlsx .SheetViews .SheetView = append (xlsx .SheetViews .SheetView , xlsxSheetView {
155
- WorkbookViewID : 0 ,
156
- })
152
+ xlsx := xlsxWorksheet {
153
+ Dimension : & xlsxDimension {Ref : "A1" },
154
+ SheetViews : xlsxSheetViews {
155
+ SheetView : []xlsxSheetView {{WorkbookViewID : 0 }},
156
+ },
157
+ }
157
158
path := "xl/worksheets/sheet" + strconv .Itoa (index ) + ".xml"
158
159
f .sheetMap [trimSheetName (name )] = path
159
160
f .Sheet [path ] = & xlsx
@@ -222,6 +223,9 @@ func (f *File) SetActiveSheet(index int) {
222
223
wb := f .workbookReader ()
223
224
for activeTab , sheet := range wb .Sheets .Sheet {
224
225
if sheet .SheetID == index {
226
+ if wb .BookViews == nil {
227
+ wb .BookViews = & xlsxBookViews {}
228
+ }
225
229
if len (wb .BookViews .WorkBookView ) > 0 {
226
230
wb .BookViews .WorkBookView [0 ].ActiveTab = activeTab
227
231
} else {
@@ -253,16 +257,13 @@ func (f *File) SetActiveSheet(index int) {
253
257
func (f * File ) GetActiveSheetIndex () int {
254
258
wb := f .workbookReader ()
255
259
if wb != nil {
256
- view := wb .BookViews .WorkBookView
257
- sheets := wb .Sheets .Sheet
258
- var activeTab int
259
- if len (view ) > 0 {
260
- activeTab = view [0 ].ActiveTab
261
- if len (sheets ) > activeTab && sheets [activeTab ].SheetID != 0 {
262
- return sheets [activeTab ].SheetID
260
+ if wb .BookViews != nil && len (wb .BookViews .WorkBookView ) > 0 {
261
+ activeTab := wb .BookViews .WorkBookView [0 ].ActiveTab
262
+ if len (wb .Sheets .Sheet ) > activeTab && wb .Sheets .Sheet [activeTab ].SheetID != 0 {
263
+ return wb .Sheets .Sheet [activeTab ].SheetID
263
264
}
264
265
}
265
- if len (wb .Sheets .Sheet ) = = 1 {
266
+ if len (wb .Sheets .Sheet ) > = 1 {
266
267
return wb .Sheets .Sheet [0 ].SheetID
267
268
}
268
269
}
@@ -413,9 +414,11 @@ func (f *File) DeleteSheet(name string) {
413
414
f .SheetCount --
414
415
}
415
416
}
416
- for idx , bookView := range wb .BookViews .WorkBookView {
417
- if bookView .ActiveTab >= f .SheetCount {
418
- wb .BookViews .WorkBookView [idx ].ActiveTab --
417
+ if wb .BookViews != nil {
418
+ for idx , bookView := range wb .BookViews .WorkBookView {
419
+ if bookView .ActiveTab >= f .SheetCount {
420
+ wb .BookViews .WorkBookView [idx ].ActiveTab --
421
+ }
419
422
}
420
423
}
421
424
f .SetActiveSheet (len (f .GetSheetMap ()))
0 commit comments