Browse Source

Don't duplicate fills in style output.

Geoffrey J. Teale 11 years ago
parent
commit
77280d2342
2 changed files with 26 additions and 4 deletions
  1. 3 3
      sheet_test.go
  2. 23 1
      xmlStyle.go

+ 3 - 3
sheet_test.go

@@ -81,7 +81,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "10")
 	c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
 
-	c.Assert(styles.Fills.Count, Equals, 2)
+	c.Assert(styles.Fills.Count, Equals, 1)
 	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")
@@ -99,7 +99,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
 	// The 1st element cannot get initialised this way by accident.
 	c.Assert(styles.CellStyleXfs.Xf[1].FontId, Equals, 0)
-	c.Assert(styles.CellStyleXfs.Xf[1].FillId, Equals, 1)
+	c.Assert(styles.CellStyleXfs.Xf[1].FillId, Equals, 0)
 	c.Assert(styles.CellStyleXfs.Xf[1].BorderId, Equals, 1)
 
 	c.Assert(styles.CellXfs.Count, Equals, 2)
@@ -109,7 +109,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	// As above, we need the 1st element to make the test fail
 	// when it should.
 	c.Assert(styles.CellXfs.Xf[1].FontId, Equals, 0)
-	c.Assert(styles.CellXfs.Xf[1].FillId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].FillId, Equals, 0)
 	c.Assert(styles.CellXfs.Xf[1].BorderId, Equals, 1)
 
 	// Finally we check that the cell points to the right CellXf /

+ 23 - 1
xmlStyle.go

@@ -117,6 +117,12 @@ func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
 }
 
 func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
+	var fill xlsxFill
+	for index, fill = range styles.Fills.Fill {
+		if fill.Equals(xFill) {
+			return index
+		}
+	}
 	styles.Fills.Fill = append(styles.Fills.Fill, xFill)
 	index = styles.Fills.Count
 	styles.Fills.Count += 1
@@ -301,7 +307,7 @@ type xlsxFont struct {
 }
 
 func (font *xlsxFont) Equals(other xlsxFont) bool {
-	return font.Sz.Val == other.Sz.Val && font.Name.Val == other.Name.Val && font.Family.Val == other.Family.Val && font.Charset.Val == other.Charset.Val && font.Color.RGB == other.Color.RGB
+	return font.Sz.Equals(other.Sz) && font.Name.Equals(other.Name) && font.Family.Equals(other.Family) && font.Charset.Equals(other.Charset) && font.Color.Equals(other.Color)
 }
 
 func (font *xlsxFont) Marshal() (result string, err error) {
@@ -333,6 +339,10 @@ type xlsxVal struct {
 	Val string `xml:"val,attr,omitempty"`
 }
 
+func (val *xlsxVal) Equals(other xlsxVal) bool {
+	return val.Val == other.Val
+}
+
 // 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
@@ -373,6 +383,10 @@ type xlsxFill struct {
 	PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
 }
 
+func (fill *xlsxFill) Equals(other xlsxFill) bool {
+	return fill.PatternFill.Equals(other.PatternFill)
+}
+
 func (fill *xlsxFill) Marshal() (result string, err error) {
 	if fill.PatternFill.PatternType != "" {
 		var xpatternFill string
@@ -398,6 +412,10 @@ type xlsxPatternFill struct {
 	BgColor     xlsxColor `xml:"bgColor,omitempty"`
 }
 
+func (patternFill *xlsxPatternFill) Equals(other xlsxPatternFill) bool {
+	return patternFill.PatternType == other.PatternType && patternFill.FgColor.Equals(other.FgColor) && patternFill.BgColor.Equals(other.BgColor)
+}
+
 func (patternFill *xlsxPatternFill) Marshal() (result string, err error) {
 	result = fmt.Sprintf(`<patternFill patternType="%s"`, patternFill.PatternType)
 	ending := `/>`
@@ -428,6 +446,10 @@ type xlsxColor struct {
 	RGB string `xml:"rgb,attr,omitempty"`
 }
 
+func (color *xlsxColor) Equals(other xlsxColor) bool {
+	return color.RGB == other.RGB
+}
+
 // xlsxBorders directly maps the borders element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much