Skip to content

Commit a526e90

Browse files
committed
Fix qax-os#426, handle empty workbook view
1 parent 3763228 commit a526e90

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

Diff for: sheet.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ func (f *File) setContentTypes(index int) {
149149

150150
// setSheet provides a function to update sheet property by given index.
151151
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+
}
157158
path := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
158159
f.sheetMap[trimSheetName(name)] = path
159160
f.Sheet[path] = &xlsx
@@ -222,6 +223,9 @@ func (f *File) SetActiveSheet(index int) {
222223
wb := f.workbookReader()
223224
for activeTab, sheet := range wb.Sheets.Sheet {
224225
if sheet.SheetID == index {
226+
if wb.BookViews == nil {
227+
wb.BookViews = &xlsxBookViews{}
228+
}
225229
if len(wb.BookViews.WorkBookView) > 0 {
226230
wb.BookViews.WorkBookView[0].ActiveTab = activeTab
227231
} else {
@@ -253,16 +257,13 @@ func (f *File) SetActiveSheet(index int) {
253257
func (f *File) GetActiveSheetIndex() int {
254258
wb := f.workbookReader()
255259
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
263264
}
264265
}
265-
if len(wb.Sheets.Sheet) == 1 {
266+
if len(wb.Sheets.Sheet) >= 1 {
266267
return wb.Sheets.Sheet[0].SheetID
267268
}
268269
}
@@ -413,9 +414,11 @@ func (f *File) DeleteSheet(name string) {
413414
f.SheetCount--
414415
}
415416
}
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+
}
419422
}
420423
}
421424
f.SetActiveSheet(len(f.GetSheetMap()))

Diff for: stream.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,12 @@ func StreamMarshalSheet(ws *xlsxWorksheet, replaceMap map[string][]byte) []byte
191191
var marshalResult []byte
192192
marshalResult = append(marshalResult, []byte(XMLHeader+`<worksheet`+templateNamespaceIDMap)...)
193193
for i := 0; i < s.NumField(); i++ {
194-
f := s.Field(i)
195194
content, ok := replaceMap[typeOfT.Field(i).Name]
196195
if ok {
197196
marshalResult = append(marshalResult, content...)
198197
continue
199198
}
200-
out, _ := xml.Marshal(f.Interface())
199+
out, _ := xml.Marshal(s.Field(i).Interface())
201200
marshalResult = append(marshalResult, out...)
202201
}
203202
marshalResult = append(marshalResult, []byte(`</worksheet>`)...)

Diff for: xmlWorkbook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type xlsxWorkbook struct {
3333
FileVersion *xlsxFileVersion `xml:"fileVersion"`
3434
WorkbookPr *xlsxWorkbookPr `xml:"workbookPr"`
3535
WorkbookProtection *xlsxWorkbookProtection `xml:"workbookProtection"`
36-
BookViews xlsxBookViews `xml:"bookViews"`
36+
BookViews *xlsxBookViews `xml:"bookViews"`
3737
Sheets xlsxSheets `xml:"sheets"`
3838
ExternalReferences *xlsxExternalReferences `xml:"externalReferences"`
3939
DefinedNames *xlsxDefinedNames `xml:"definedNames"`

Diff for: xmlWorksheet.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import "encoding/xml"
1717
type xlsxWorksheet struct {
1818
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
1919
SheetPr *xlsxSheetPr `xml:"sheetPr"`
20-
Dimension xlsxDimension `xml:"dimension"`
21-
SheetViews xlsxSheetViews `xml:"sheetViews,omitempty"`
20+
Dimension *xlsxDimension `xml:"dimension"`
21+
SheetViews xlsxSheetViews `xml:"sheetViews"`
2222
SheetFormatPr *xlsxSheetFormatPr `xml:"sheetFormatPr"`
23-
Cols *xlsxCols `xml:"cols,omitempty"`
23+
Cols *xlsxCols `xml:"cols"`
2424
SheetData xlsxSheetData `xml:"sheetData"`
2525
SheetCalcPr *xlsxInnerXML `xml:"sheetCalcPr"`
2626
SheetProtection *xlsxSheetProtection `xml:"sheetProtection"`
@@ -33,7 +33,7 @@ type xlsxWorksheet struct {
3333
MergeCells *xlsxMergeCells `xml:"mergeCells"`
3434
PhoneticPr *xlsxPhoneticPr `xml:"phoneticPr"`
3535
ConditionalFormatting []*xlsxConditionalFormatting `xml:"conditionalFormatting"`
36-
DataValidations *xlsxDataValidations `xml:"dataValidations,omitempty"`
36+
DataValidations *xlsxDataValidations `xml:"dataValidations"`
3737
Hyperlinks *xlsxHyperlinks `xml:"hyperlinks"`
3838
PrintOptions *xlsxPrintOptions `xml:"printOptions"`
3939
PageMargins *xlsxPageMargins `xml:"pageMargins"`

0 commit comments

Comments
 (0)