Browse Source

Merge pull request #113 from sirwart/master

Fix a couple of bugs + default col width

Thanks for adding the tests. Looks like nice work to me, thanks.
Shawn Milochik 10 years ago
parent
commit
dacd090ce1
8 changed files with 53 additions and 40 deletions
  1. 5 1
      lib.go
  2. 2 2
      lib_test.go
  3. 16 10
      sheet.go
  4. 3 3
      style.go
  5. 6 6
      xmlStyle.go
  6. 12 12
      xmlStyle_test.go
  7. 6 5
      xmlWorksheet.go
  8. 3 1
      xmlWorksheet_test.go

+ 5 - 1
lib.go

@@ -494,7 +494,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, in
 			// from the data.
 			for x > insertColIndex {
 				// Put an empty Cell into the array
-				row.Cells[insertColIndex-minCol] = new(Cell)
+				row.Cells[insertColIndex] = new(Cell)
 				insertColIndex++
 			}
 			cellX := insertColIndex
@@ -561,6 +561,10 @@ func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *F
 	sheet.Rows, sheet.Cols, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi)
 	sheet.Hidden = rsheet.State == sheetStateHidden || rsheet.State == sheetStateVeryHidden
 	sheet.SheetViews = readSheetViews(worksheet.SheetViews)
+
+	sheet.SheetFormat.DefaultColWidth = worksheet.SheetFormatPr.DefaultColWidth
+	sheet.SheetFormat.DefaultRowHeight = worksheet.SheetFormatPr.DefaultRowHeight
+
 	result.Sheet = sheet
 	sc <- result
 }

+ 2 - 2
lib_test.go

@@ -317,8 +317,8 @@ func (l *LibSuite) TestReadRowsFromSheet(c *C) {
 	sheetView := worksheet.SheetViews.SheetView[0]
 	c.Assert(sheetView.Pane, NotNil)
 	pane := sheetView.Pane
-	c.Assert(pane.XSplit, Equals, 0)
-	c.Assert(pane.YSplit, Equals, 1)
+	c.Assert(pane.XSplit, Equals, 0.0)
+	c.Assert(pane.YSplit, Equals, 1.0)
 }
 
 func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {

+ 16 - 10
sheet.go

@@ -8,14 +8,15 @@ import (
 // Sheet is a high level structure intended to provide user access to
 // the contents of a particular sheet within an XLSX file.
 type Sheet struct {
-	Name       string
-	File       *File
-	Rows       []*Row
-	Cols       []*Col
-	MaxRow     int
-	MaxCol     int
-	Hidden     bool
-	SheetViews []SheetView
+	Name        string
+	File        *File
+	Rows        []*Row
+	Cols        []*Col
+	MaxRow      int
+	MaxCol      int
+	Hidden      bool
+	SheetViews  []SheetView
+	SheetFormat SheetFormat
 }
 
 type SheetView struct {
@@ -23,13 +24,18 @@ type SheetView struct {
 }
 
 type Pane struct {
-	XSplit      int
-	YSplit      int
+	XSplit      float64
+	YSplit      float64
 	TopLeftCell string
 	ActivePane  string
 	State       string // Either "split" or "frozen"
 }
 
+type SheetFormat struct {
+	DefaultColWidth  float64
+	DefaultRowHeight float64
+}
+
 // Add a new Row to a Sheet
 func (s *Sheet) AddRow() *Row {
 	row := &Row{Sheet: s}

+ 3 - 3
style.go

@@ -36,17 +36,17 @@ func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBo
 	xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
 	xFont.Color.RGB = style.Font.Color
 	if style.Font.Bold {
-		xFont.B = &struct{}{}
+		xFont.B = &xlsxVal{}
 	} else {
 		xFont.B = nil
 	}
 	if style.Font.Italic {
-		xFont.I = &struct{}{}
+		xFont.I = &xlsxVal{}
 	} else {
 		xFont.I = nil
 	}
 	if style.Font.Underline {
-		xFont.U = &struct{}{}
+		xFont.U = &xlsxVal{}
 	} else {
 		xFont.U = nil
 	}

+ 6 - 6
xmlStyle.go

@@ -105,13 +105,13 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 			style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
 			style.Font.Color = styles.argbValue(xfont.Color)
 
-			if xfont.B != nil {
+			if bold := xfont.B; bold != nil && bold.Val != "0" {
 				style.Font.Bold = true
 			}
-			if xfont.I != nil {
+			if italic := xfont.I; italic != nil && italic.Val != "0" {
 				style.Font.Italic = true
 			}
-			if xfont.U != nil {
+			if underline := xfont.U; underline != nil && underline.Val != "0"  {
 				style.Font.Underline = true
 			}
 		}
@@ -445,9 +445,9 @@ type xlsxFont struct {
 	Family  xlsxVal   `xml:"family,omitempty"`
 	Charset xlsxVal   `xml:"charset,omitempty"`
 	Color   xlsxColor `xml:"color,omitempty"`
-	B       *struct{} `xml:"b,omitempty"`
-	I       *struct{} `xml:"i,omitempty"`
-	U       *struct{} `xml:"u,omitempty"`
+	B       *xlsxVal  `xml:"b,omitempty"`
+	I       *xlsxVal  `xml:"i,omitempty"`
+	U       *xlsxVal  `xml:"u,omitempty"`
 }
 
 func (font *xlsxFont) Equals(other xlsxFont) bool {

+ 12 - 12
xmlStyle_test.go

@@ -26,9 +26,9 @@ func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFont(c *C) {
 	font := xlsxFont{}
 	font.Sz.Val = "10"
 	font.Name.Val = "Andale Mono"
-	font.B = &struct{}{}
-	font.I = &struct{}{}
-	font.U = &struct{}{}
+	font.B = &xlsxVal{}
+	font.I = &xlsxVal{}
+	font.U = &xlsxVal{}
 	styles.Fonts.Font[0] = font
 
 	expected := `<?xml version="1.0" encoding="UTF-8"?>
@@ -163,16 +163,16 @@ func (x *XMLStyleSuite) TestFontEquals(c *C) {
 		Color:  xlsxColor{RGB: "FFFF0000"},
 		Name:   xlsxVal{Val: "Calibri"},
 		Family: xlsxVal{Val: "2"},
-		B:      &struct{}{},
-		I:      &struct{}{},
-		U:      &struct{}{}}
+		B:      &xlsxVal{},
+		I:      &xlsxVal{},
+		U:      &xlsxVal{}}
 	fontB := xlsxFont{Sz: xlsxVal{Val: "11"},
 		Color:  xlsxColor{RGB: "FFFF0000"},
 		Name:   xlsxVal{Val: "Calibri"},
 		Family: xlsxVal{Val: "2"},
-		B:      &struct{}{},
-		I:      &struct{}{},
-		U:      &struct{}{}}
+		B:      &xlsxVal{},
+		I:      &xlsxVal{},
+		U:      &xlsxVal{}}
 
 	c.Assert(fontA.Equals(fontB), Equals, true)
 	fontB.Sz.Val = "12"
@@ -189,13 +189,13 @@ func (x *XMLStyleSuite) TestFontEquals(c *C) {
 	fontB.Family.Val = "2"
 	fontB.B = nil
 	c.Assert(fontA.Equals(fontB), Equals, false)
-	fontB.B = &struct{}{}
+	fontB.B = &xlsxVal{}
 	fontB.I = nil
 	c.Assert(fontA.Equals(fontB), Equals, false)
-	fontB.I = &struct{}{}
+	fontB.I = &xlsxVal{}
 	fontB.U = nil
 	c.Assert(fontA.Equals(fontB), Equals, false)
-	fontB.U = &struct{}{}
+	fontB.U = &xlsxVal{}
 	// For sanity
 	c.Assert(fontA.Equals(fontB), Equals, true)
 }

+ 6 - 5
xmlWorksheet.go

@@ -102,6 +102,7 @@ type xlsxPageMargins struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxSheetFormatPr struct {
+	DefaultColWidth  float64 `xml:"defaultColWidth,attr,omitempty"`
 	DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
 }
 
@@ -154,11 +155,11 @@ type xlsxSelection struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxPane struct {
-	XSplit      int    `xml:"xSplit,attr"`
-	YSplit      int    `xml:"ySplit,attr"`
-	TopLeftCell string `xml:"topLeftCell,attr"`
-	ActivePane  string `xml:"activePane,attr"`
-	State       string `xml:"state,attr"` // Either "split" or "frozen"
+	XSplit      float64 `xml:"xSplit,attr"`
+	YSplit      float64 `xml:"ySplit,attr"`
+	TopLeftCell string  `xml:"topLeftCell,attr"`
+	ActivePane  string  `xml:"activePane,attr"`
+	State       string  `xml:"state,attr"` // Either "split" or "frozen"
 }
 
 // xlsxSheetPr directly maps the sheetPr element in the namespace

+ 3 - 1
xmlWorksheet_test.go

@@ -44,7 +44,7 @@ func (w *WorksheetSuite) TestUnmarshallWorksheet(c *C) {
                          sqref="B2"/>
             </sheetView>
           </sheetViews>
-          <sheetFormatPr defaultRowHeight="15">
+          <sheetFormatPr defaultRowHeight="15" defaultColWidth="8">
           </sheetFormatPr>
           <cols>
             <col collapsed="false"
@@ -131,6 +131,8 @@ func (w *WorksheetSuite) TestUnmarshallWorksheet(c *C) {
 	c.Assert(err, IsNil)
 	c.Assert(worksheet.Dimension.Ref, Equals, "A1:B2")
 	c.Assert(worksheet.SheetData.Row, HasLen, 2)
+	c.Assert(worksheet.SheetFormatPr.DefaultRowHeight, Equals, 15.0)
+	c.Assert(worksheet.SheetFormatPr.DefaultColWidth, Equals, 8.0)
 	row := worksheet.SheetData.Row[0]
 	c.Assert(row.R, Equals, 1)
 	c.Assert(row.C, HasLen, 2)