Browse Source

Add count attribute to styles/cellStyleXfs and styles/cellXfs elements.

Geoffrey J. Teale 11 years ago
parent
commit
c07706c1ce
3 changed files with 62 additions and 40 deletions
  1. 6 6
      file_test.go
  2. 15 15
      sheet_test.go
  3. 41 19
      xmlStyle.go

+ 6 - 6
file_test.go

@@ -93,10 +93,10 @@ func (l *FileSuite) TestReadStylesFromZipFile(c *C) {
 	c.Assert(border.Top.Style, Equals, "thin")
 	c.Assert(border.Bottom.Style, Equals, "thin")
 
-	cellStyleXfCount = len(xlsxFile.styles.CellStyleXfs)
+	cellStyleXfCount = xlsxFile.styles.CellStyleXfs.Count
 	c.Assert(cellStyleXfCount, Equals, 20)
 
-	xf = xlsxFile.styles.CellStyleXfs[0]
+	xf = xlsxFile.styles.CellStyleXfs.Xf[0]
 	expectedXf := &xlsxXf{
 		ApplyAlignment:  true,
 		ApplyBorder:     true,
@@ -109,10 +109,10 @@ func (l *FileSuite) TestReadStylesFromZipFile(c *C) {
 		NumFmtId:        164}
 	testXf(c, &xf, expectedXf)
 
-	cellXfCount = len(xlsxFile.styles.CellXfs)
+	cellXfCount = xlsxFile.styles.CellXfs.Count
 	c.Assert(cellXfCount, Equals, 3)
 
-	xf = xlsxFile.styles.CellXfs[0]
+	xf = xlsxFile.styles.CellXfs.Xf[0]
 	expectedXf = &xlsxXf{
 		ApplyAlignment:  false,
 		ApplyBorder:     false,
@@ -435,11 +435,11 @@ func (l *FileSuite) TestMarshalFile(c *C) {
         <bottom></bottom>
       </border>
     </borders>
-    <cellStyleXfs>
+    <cellStyleXfs count="2">
       <xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
       <xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="1" fillId="1" fontId="1" numFmtId="0"></xf>
     </cellStyleXfs>
-    <cellXfs>
+    <cellXfs count="2">
       <xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
       <xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="1" fillId="1" fontId="1" numFmtId="0"></xf>
     </cellXfs>

+ 15 - 15
sheet_test.go

@@ -92,25 +92,25 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(styles.Borders.Border[0].Top.Style, Equals, "none")
 	c.Assert(styles.Borders.Border[0].Bottom.Style, Equals, "thin")
 
-	c.Assert(len(styles.CellStyleXfs), Equals, 2)
+	c.Assert(styles.CellStyleXfs.Count, Equals, 2)
 	// The 0th CellStyleXf could just be getting the zero values by default
-	c.Assert(styles.CellStyleXfs[0].FontId, Equals, 0)
-	c.Assert(styles.CellStyleXfs[0].FillId, Equals, 0)
-	c.Assert(styles.CellStyleXfs[0].BorderId, Equals, 0)
+	c.Assert(styles.CellStyleXfs.Xf[0].FontId, Equals, 0)
+	c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
+	c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
 	// The 1st element cannot get initialised this way by accident.
-	c.Assert(styles.CellStyleXfs[1].FontId, Equals, 1)
-	c.Assert(styles.CellStyleXfs[1].FillId, Equals, 1)
-	c.Assert(styles.CellStyleXfs[1].BorderId, Equals, 1)
-
-	c.Assert(len(styles.CellXfs), Equals, 2)
-	c.Assert(styles.CellXfs[0].FontId, Equals, 0)
-	c.Assert(styles.CellXfs[0].FillId, Equals, 0)
-	c.Assert(styles.CellXfs[0].BorderId, Equals, 0)
+	c.Assert(styles.CellStyleXfs.Xf[1].FontId, Equals, 1)
+	c.Assert(styles.CellStyleXfs.Xf[1].FillId, Equals, 1)
+	c.Assert(styles.CellStyleXfs.Xf[1].BorderId, Equals, 1)
+
+	c.Assert(styles.CellXfs.Count, Equals, 2)
+	c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
+	c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
+	c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
 	// As above, we need the 1st element to make the test fail
 	// when it should.
-	c.Assert(styles.CellXfs[1].FontId, Equals, 1)
-	c.Assert(styles.CellXfs[1].FillId, Equals, 1)
-	c.Assert(styles.CellXfs[1].BorderId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].FontId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].FillId, Equals, 1)
+	c.Assert(styles.CellXfs.Xf[1].BorderId, Equals, 1)
 
 	// Finally we check that the cell points to the right CellXf /
 	// CellStyleXf.

+ 41 - 19
xmlStyle.go

@@ -17,12 +17,12 @@ import (
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxStyles struct {
-	Fonts        xlsxFonts    `xml:"fonts,omitempty"`
-	Fills        xlsxFills    `xml:"fills,omitempty"`
-	Borders      xlsxBorders  `xml:"borders,omitempty"`
-	CellStyleXfs []xlsxXf     `xml:"cellStyleXfs>xf,omitempty"`
-	CellXfs      []xlsxXf     `xml:"cellXfs>xf,omitempty"`
-	NumFmts      []xlsxNumFmt `xml:numFmts>numFmt,omitempty"`
+	Fonts        xlsxFonts        `xml:"fonts,omitempty"`
+	Fills        xlsxFills        `xml:"fills,omitempty"`
+	Borders      xlsxBorders      `xml:"borders,omitempty"`
+	CellStyleXfs xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
+	CellXfs      xlsxCellXfs      `xml:"cellXfs,omitempty"`
+	NumFmts      []xlsxNumFmt     `xml:numFmts>numFmt,omitempty"`
 }
 
 // xlsxNumFmt directly maps the numFmt element in the namespace
@@ -126,6 +126,24 @@ type xlsxLine struct {
 	Style string `xml:"style,attr,omitempty"`
 }
 
+// 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
+// much as I need.
+type xlsxCellStyleXfs struct {
+	Count int      `xml:"count,attr"`
+	Xf    []xlsxXf `xml:"xf,omitempty"`
+}
+
+// xlsxCellXfs directly maps the cellXfs 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 xlsxCellXfs struct {
+	Count int      `xml:"count,attr"`
+	Xf    []xlsxXf `xml:"xf,omitempty"`
+}
+
 // xlsxXf directly maps the xf element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
@@ -159,14 +177,14 @@ func (styles *xlsxStyles) getStyle(styleIndex int) (style Style) {
 	style.Fill = Fill{}
 	style.Font = Font{}
 
-	xfCount := len(styles.CellXfs)
+	xfCount := styles.CellXfs.Count
 	if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
-		xf := styles.CellXfs[styleIndex]
+		xf := styles.CellXfs.Xf[styleIndex]
 
 		// Google docs can produce output that has fewer
 		// CellStyleXfs than CellXfs - this copes with that.
-		if styleIndex < len(styles.CellStyleXfs) {
-			styleXf = styles.CellStyleXfs[styleIndex]
+		if styleIndex < styles.CellStyleXfs.Count {
+			styleXf = styles.CellStyleXfs.Xf[styleIndex]
 		} else {
 			styleXf = xlsxXf{}
 		}
@@ -202,12 +220,12 @@ func (styles *xlsxStyles) getStyle(styleIndex int) (style Style) {
 }
 
 func (styles *xlsxStyles) getNumberFormat(styleIndex int, numFmtRefTable map[int]xlsxNumFmt) string {
-	if styles.CellXfs == nil {
+	if styles.CellXfs.Xf == nil {
 		return ""
 	}
 	var numberFormat string = ""
-	if styleIndex > -1 && styleIndex <= len(styles.CellXfs) {
-		xf := styles.CellXfs[styleIndex]
+	if styleIndex > -1 && styleIndex <= styles.CellXfs.Count {
+		xf := styles.CellXfs.Xf[styleIndex]
 		numFmt := numFmtRefTable[xf.NumFmtId]
 		numberFormat = numFmt.FormatCode
 	}
@@ -235,12 +253,16 @@ func (styles *xlsxStyles) addBorder(xBorder xlsxBorder) (index int) {
 	return
 }
 
-func (styles *xlsxStyles) addCellStyleXf(xCellStyleXf xlsxXf) int {
-	styles.CellStyleXfs = append(styles.CellStyleXfs, xCellStyleXf)
-	return len(styles.CellStyleXfs) - 1
+func (styles *xlsxStyles) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
+	styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
+	index = styles.CellStyleXfs.Count
+	styles.CellStyleXfs.Count += 1
+	return
 }
 
-func (styles *xlsxStyles) addCellXf(xCellXf xlsxXf) int {
-	styles.CellXfs = append(styles.CellXfs, xCellXf)
-	return len(styles.CellXfs) - 1
+func (styles *xlsxStyles) addCellXf(xCellXf xlsxXf) (index int) {
+	styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
+	index = styles.CellXfs.Count
+	styles.CellXfs.Count += 1
+	return
 }