Browse Source

Don't duplicate border definitions in styles.

Geoffrey J. Teale 11 years ago
parent
commit
8c9a61c07e
2 changed files with 17 additions and 3 deletions
  1. 3 3
      sheet_test.go
  2. 14 0
      xmlStyle.go

+ 3 - 3
sheet_test.go

@@ -86,7 +86,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
 	c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "00000000")
 
-	c.Assert(styles.Borders.Count, Equals, 2)
+	c.Assert(styles.Borders.Count, Equals, 1)
 	c.Assert(styles.Borders.Border[0].Left.Style, Equals, "none")
 	c.Assert(styles.Borders.Border[0].Right.Style, Equals, "thin")
 	c.Assert(styles.Borders.Border[0].Top.Style, Equals, "none")
@@ -100,7 +100,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	// 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, 0)
-	c.Assert(styles.CellStyleXfs.Xf[1].BorderId, Equals, 1)
+	c.Assert(styles.CellStyleXfs.Xf[1].BorderId, Equals, 0)
 
 	c.Assert(styles.CellXfs.Count, Equals, 2)
 	c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
@@ -110,7 +110,7 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	// when it should.
 	c.Assert(styles.CellXfs.Xf[1].FontId, Equals, 0)
 	c.Assert(styles.CellXfs.Xf[1].FillId, Equals, 0)
-	c.Assert(styles.CellXfs.Xf[1].BorderId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].BorderId, Equals, 0)
 
 	// Finally we check that the cell points to the right CellXf /
 	// CellStyleXf.

+ 14 - 0
xmlStyle.go

@@ -130,6 +130,12 @@ func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
 }
 
 func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
+	var border xlsxBorder
+	for index, border = range styles.Borders.Border {
+		if border.Equals(xBorder) {
+			return index
+		}
+	}
 	styles.Borders.Border = append(styles.Borders.Border, xBorder)
 	index = styles.Borders.Count
 	styles.Borders.Count += 1
@@ -494,6 +500,10 @@ type xlsxBorder struct {
 	Bottom xlsxLine `xml:"bottom,omitempty"`
 }
 
+func (border *xlsxBorder) Equals(other xlsxBorder) bool {
+	return border.Left.Equals(other.Left) && border.Right.Equals(other.Right) && border.Top.Equals(other.Top) && border.Bottom.Equals(other.Bottom)
+}
+
 func (border *xlsxBorder) Marshal() (result string, err error) {
 	emit := false
 	subparts := ""
@@ -529,6 +539,10 @@ type xlsxLine struct {
 	Style string `xml:"style,attr,omitempty"`
 }
 
+func (line *xlsxLine) Equals(other xlsxLine) bool {
+	return line.Style == other.Style
+}
+
 // xlsxCellStyleXfs directly maps the cellStyleXfs element in the
 // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
 // - currently I have not checked it for completeness - it does as