瀏覽代碼

Fix #426, handle empty workbook view

xuri 6 年之前
父節點
當前提交
a526e90404
共有 4 個文件被更改,包括 25 次插入23 次删除
  1. 19 16
      sheet.go
  2. 1 2
      stream.go
  3. 1 1
      xmlWorkbook.go
  4. 4 4
      xmlWorksheet.go

+ 19 - 16
sheet.go

@@ -149,11 +149,12 @@ func (f *File) setContentTypes(index int) {
 
 
 // setSheet provides a function to update sheet property by given index.
 // setSheet provides a function to update sheet property by given index.
 func (f *File) setSheet(index int, name string) {
 func (f *File) setSheet(index int, name string) {
-	var xlsx xlsxWorksheet
-	xlsx.Dimension.Ref = "A1"
-	xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
-		WorkbookViewID: 0,
-	})
+	xlsx := xlsxWorksheet{
+		Dimension: &xlsxDimension{Ref: "A1"},
+		SheetViews: xlsxSheetViews{
+			SheetView: []xlsxSheetView{{WorkbookViewID: 0}},
+		},
+	}
 	path := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
 	path := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
 	f.sheetMap[trimSheetName(name)] = path
 	f.sheetMap[trimSheetName(name)] = path
 	f.Sheet[path] = &xlsx
 	f.Sheet[path] = &xlsx
@@ -222,6 +223,9 @@ func (f *File) SetActiveSheet(index int) {
 	wb := f.workbookReader()
 	wb := f.workbookReader()
 	for activeTab, sheet := range wb.Sheets.Sheet {
 	for activeTab, sheet := range wb.Sheets.Sheet {
 		if sheet.SheetID == index {
 		if sheet.SheetID == index {
+			if wb.BookViews == nil {
+				wb.BookViews = &xlsxBookViews{}
+			}
 			if len(wb.BookViews.WorkBookView) > 0 {
 			if len(wb.BookViews.WorkBookView) > 0 {
 				wb.BookViews.WorkBookView[0].ActiveTab = activeTab
 				wb.BookViews.WorkBookView[0].ActiveTab = activeTab
 			} else {
 			} else {
@@ -253,16 +257,13 @@ func (f *File) SetActiveSheet(index int) {
 func (f *File) GetActiveSheetIndex() int {
 func (f *File) GetActiveSheetIndex() int {
 	wb := f.workbookReader()
 	wb := f.workbookReader()
 	if wb != nil {
 	if wb != nil {
-		view := wb.BookViews.WorkBookView
-		sheets := wb.Sheets.Sheet
-		var activeTab int
-		if len(view) > 0 {
-			activeTab = view[0].ActiveTab
-			if len(sheets) > activeTab && sheets[activeTab].SheetID != 0 {
-				return sheets[activeTab].SheetID
+		if wb.BookViews != nil && len(wb.BookViews.WorkBookView) > 0 {
+			activeTab := wb.BookViews.WorkBookView[0].ActiveTab
+			if len(wb.Sheets.Sheet) > activeTab && wb.Sheets.Sheet[activeTab].SheetID != 0 {
+				return wb.Sheets.Sheet[activeTab].SheetID
 			}
 			}
 		}
 		}
-		if len(wb.Sheets.Sheet) == 1 {
+		if len(wb.Sheets.Sheet) >= 1 {
 			return wb.Sheets.Sheet[0].SheetID
 			return wb.Sheets.Sheet[0].SheetID
 		}
 		}
 	}
 	}
@@ -413,9 +414,11 @@ func (f *File) DeleteSheet(name string) {
 			f.SheetCount--
 			f.SheetCount--
 		}
 		}
 	}
 	}
-	for idx, bookView := range wb.BookViews.WorkBookView {
-		if bookView.ActiveTab >= f.SheetCount {
-			wb.BookViews.WorkBookView[idx].ActiveTab--
+	if wb.BookViews != nil {
+		for idx, bookView := range wb.BookViews.WorkBookView {
+			if bookView.ActiveTab >= f.SheetCount {
+				wb.BookViews.WorkBookView[idx].ActiveTab--
+			}
 		}
 		}
 	}
 	}
 	f.SetActiveSheet(len(f.GetSheetMap()))
 	f.SetActiveSheet(len(f.GetSheetMap()))

+ 1 - 2
stream.go

@@ -191,13 +191,12 @@ func StreamMarshalSheet(ws *xlsxWorksheet, replaceMap map[string][]byte) []byte
 	var marshalResult []byte
 	var marshalResult []byte
 	marshalResult = append(marshalResult, []byte(XMLHeader+`<worksheet`+templateNamespaceIDMap)...)
 	marshalResult = append(marshalResult, []byte(XMLHeader+`<worksheet`+templateNamespaceIDMap)...)
 	for i := 0; i < s.NumField(); i++ {
 	for i := 0; i < s.NumField(); i++ {
-		f := s.Field(i)
 		content, ok := replaceMap[typeOfT.Field(i).Name]
 		content, ok := replaceMap[typeOfT.Field(i).Name]
 		if ok {
 		if ok {
 			marshalResult = append(marshalResult, content...)
 			marshalResult = append(marshalResult, content...)
 			continue
 			continue
 		}
 		}
-		out, _ := xml.Marshal(f.Interface())
+		out, _ := xml.Marshal(s.Field(i).Interface())
 		marshalResult = append(marshalResult, out...)
 		marshalResult = append(marshalResult, out...)
 	}
 	}
 	marshalResult = append(marshalResult, []byte(`</worksheet>`)...)
 	marshalResult = append(marshalResult, []byte(`</worksheet>`)...)

+ 1 - 1
xmlWorkbook.go

@@ -33,7 +33,7 @@ type xlsxWorkbook struct {
 	FileVersion         *xlsxFileVersion         `xml:"fileVersion"`
 	FileVersion         *xlsxFileVersion         `xml:"fileVersion"`
 	WorkbookPr          *xlsxWorkbookPr          `xml:"workbookPr"`
 	WorkbookPr          *xlsxWorkbookPr          `xml:"workbookPr"`
 	WorkbookProtection  *xlsxWorkbookProtection  `xml:"workbookProtection"`
 	WorkbookProtection  *xlsxWorkbookProtection  `xml:"workbookProtection"`
-	BookViews           xlsxBookViews            `xml:"bookViews"`
+	BookViews           *xlsxBookViews           `xml:"bookViews"`
 	Sheets              xlsxSheets               `xml:"sheets"`
 	Sheets              xlsxSheets               `xml:"sheets"`
 	ExternalReferences  *xlsxExternalReferences  `xml:"externalReferences"`
 	ExternalReferences  *xlsxExternalReferences  `xml:"externalReferences"`
 	DefinedNames        *xlsxDefinedNames        `xml:"definedNames"`
 	DefinedNames        *xlsxDefinedNames        `xml:"definedNames"`

+ 4 - 4
xmlWorksheet.go

@@ -17,10 +17,10 @@ import "encoding/xml"
 type xlsxWorksheet struct {
 type xlsxWorksheet struct {
 	XMLName               xml.Name                     `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	XMLName               xml.Name                     `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	SheetPr               *xlsxSheetPr                 `xml:"sheetPr"`
 	SheetPr               *xlsxSheetPr                 `xml:"sheetPr"`
-	Dimension             xlsxDimension                `xml:"dimension"`
-	SheetViews            xlsxSheetViews               `xml:"sheetViews,omitempty"`
+	Dimension             *xlsxDimension               `xml:"dimension"`
+	SheetViews            xlsxSheetViews               `xml:"sheetViews"`
 	SheetFormatPr         *xlsxSheetFormatPr           `xml:"sheetFormatPr"`
 	SheetFormatPr         *xlsxSheetFormatPr           `xml:"sheetFormatPr"`
-	Cols                  *xlsxCols                    `xml:"cols,omitempty"`
+	Cols                  *xlsxCols                    `xml:"cols"`
 	SheetData             xlsxSheetData                `xml:"sheetData"`
 	SheetData             xlsxSheetData                `xml:"sheetData"`
 	SheetCalcPr           *xlsxInnerXML                `xml:"sheetCalcPr"`
 	SheetCalcPr           *xlsxInnerXML                `xml:"sheetCalcPr"`
 	SheetProtection       *xlsxSheetProtection         `xml:"sheetProtection"`
 	SheetProtection       *xlsxSheetProtection         `xml:"sheetProtection"`
@@ -33,7 +33,7 @@ type xlsxWorksheet struct {
 	MergeCells            *xlsxMergeCells              `xml:"mergeCells"`
 	MergeCells            *xlsxMergeCells              `xml:"mergeCells"`
 	PhoneticPr            *xlsxPhoneticPr              `xml:"phoneticPr"`
 	PhoneticPr            *xlsxPhoneticPr              `xml:"phoneticPr"`
 	ConditionalFormatting []*xlsxConditionalFormatting `xml:"conditionalFormatting"`
 	ConditionalFormatting []*xlsxConditionalFormatting `xml:"conditionalFormatting"`
-	DataValidations       *xlsxDataValidations         `xml:"dataValidations,omitempty"`
+	DataValidations       *xlsxDataValidations         `xml:"dataValidations"`
 	Hyperlinks            *xlsxHyperlinks              `xml:"hyperlinks"`
 	Hyperlinks            *xlsxHyperlinks              `xml:"hyperlinks"`
 	PrintOptions          *xlsxPrintOptions            `xml:"printOptions"`
 	PrintOptions          *xlsxPrintOptions            `xml:"printOptions"`
 	PageMargins           *xlsxPageMargins             `xml:"pageMargins"`
 	PageMargins           *xlsxPageMargins             `xml:"pageMargins"`