Browse Source

Add count to style/fills element.

Geoffrey J. Teale 11 years ago
parent
commit
016971f0e0
3 changed files with 33 additions and 17 deletions
  1. 3 3
      file_test.go
  2. 5 5
      sheet_test.go
  3. 25 9
      xmlStyle.go

+ 3 - 3
file_test.go

@@ -78,10 +78,10 @@ func (l *FileSuite) TestReadStylesFromZipFile(c *C) {
 	c.Assert(font.Sz.Val, Equals, "11")
 	c.Assert(font.Name.Val, Equals, "Calibri")
 
-	fillCount = len(xlsxFile.styles.Fills)
+	fillCount = xlsxFile.styles.Fills.Count
 	c.Assert(fillCount, Equals, 3)
 
-	fill = xlsxFile.styles.Fills[2]
+	fill = xlsxFile.styles.Fills.Fill[2]
 	c.Assert(fill.PatternFill.PatternType, Equals, "solid")
 
 	borderCount = len(xlsxFile.styles.Borders)
@@ -407,7 +407,7 @@ func (l *FileSuite) TestMarshalFile(c *C) {
         <charset val="0"></charset>
       </font>
     </fonts>
-    <fills>
+    <fills count="2">
       <fill>
         <patternFill>
           <fgColor></fgColor>

+ 5 - 5
sheet_test.go

@@ -77,14 +77,14 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	styles := &xlsxStyles{}
 	worksheet := sheet.makeXLSXSheet(refTable, styles)
 
-	c.Assert(len(styles.Fonts.Font), Equals, 2)
+	c.Assert(styles.Fonts.Count, Equals, 2)
 	c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "10")
 	c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
 
-	c.Assert(len(styles.Fills), Equals, 2)
-	c.Assert(styles.Fills[0].PatternFill.PatternType, Equals, "solid")
-	c.Assert(styles.Fills[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
-	c.Assert(styles.Fills[0].PatternFill.BgColor.RGB, Equals, "00000000")
+	c.Assert(styles.Fills.Count, Equals, 2)
+	c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "solid")
+	c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
+	c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "00000000")
 
 	c.Assert(len(styles.Borders), Equals, 2)
 	c.Assert(styles.Borders[0].Left.Style, Equals, "none")

+ 25 - 9
xmlStyle.go

@@ -18,7 +18,7 @@ import (
 // as I need.
 type xlsxStyles struct {
 	Fonts        xlsxFonts    `xml:"fonts,omitempty"`
-	Fills        []xlsxFill   `xml:"fills>fill,omitempty"`
+	Fills        xlsxFills    `xml:"fills,omitempty"`
 	Borders      []xlsxBorder `xml:"borders>border,omitempty"`
 	CellStyleXfs []xlsxXf     `xml:"cellStyleXfs>xf,omitempty"`
 	CellXfs      []xlsxXf     `xml:"cellXfs>xf,omitempty"`
@@ -34,6 +34,10 @@ type xlsxNumFmt struct {
 	FormatCode string `xml:"formatCode,omitempty"`
 }
 
+// xlsxFonts directly maps the fonts element in the namespace
+// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
+// currently I have not checked it for completeness - it does as much
+// as I need.
 type xlsxFonts struct {
 	Count int        `xml:"count,attr"`
 	Font  []xlsxFont `xml:"font,omitempty"`
@@ -58,6 +62,15 @@ type xlsxVal struct {
 	Val string `xml:"val,attr,omitempty"`
 }
 
+// xlsxFills directly maps the fills element in the namespace
+// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
+// currently I have not checked it for completeness - it does as much
+// as I need.
+type xlsxFills struct {
+	Count int        `xml:"count,attr"`
+	Fill  []xlsxFill `xml:"fill,omitempty"`
+}
+
 // xlsxFill directly maps the fill element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
@@ -160,14 +173,14 @@ func (styles *xlsxStyles) getStyle(styleIndex int) (style Style) {
 			style.Border.Bottom = styles.Borders[xf.BorderId].Bottom.Style
 		}
 
-		if xf.FillId > -1 && xf.FillId < len(styles.Fills) {
-			xFill := styles.Fills[xf.FillId]
+		if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
+			xFill := styles.Fills.Fill[xf.FillId]
 			style.Fill.PatternType = xFill.PatternFill.PatternType
 			style.Fill.FgColor = xFill.PatternFill.FgColor.RGB
 			style.Fill.BgColor = xFill.PatternFill.BgColor.RGB
 		}
 
-		if xf.FontId > -1 && xf.FontId < len(styles.Fonts.Font) {
+		if xf.FontId > -1 && xf.FontId < styles.Fonts.Count {
 			xfont := styles.Fonts.Font[xf.FontId]
 			style.Font.Size, _ = strconv.Atoi(xfont.Sz.Val)
 			style.Font.Name = xfont.Name.Val
@@ -192,15 +205,18 @@ func (styles *xlsxStyles) getNumberFormat(styleIndex int, numFmtRefTable map[int
 	return strings.ToLower(numberFormat)
 }
 
-func (styles *xlsxStyles) addFont(xFont xlsxFont) int {
+func (styles *xlsxStyles) addFont(xFont xlsxFont) (index int) {
 	styles.Fonts.Font = append(styles.Fonts.Font, xFont)
+	index = styles.Fonts.Count
 	styles.Fonts.Count += 1
-	return len(styles.Fonts.Font) - 1
+	return
 }
 
-func (styles *xlsxStyles) addFill(xFill xlsxFill) int {
-	styles.Fills = append(styles.Fills, xFill)
-	return len(styles.Fills) - 1
+func (styles *xlsxStyles) addFill(xFill xlsxFill) (index int) {
+	styles.Fills.Fill = append(styles.Fills.Fill, xFill)
+	index = styles.Fills.Count
+	styles.Fills.Count += 1
+	return
 }
 
 func (styles *xlsxStyles) addBorder(xBorder xlsxBorder) int {