Browse Source

Omit unneeded cellStyles, cellStyle, cellStyle.Xf.

Don't create named style elements when creating a new style for a cell.
Geoffrey J. Teale 9 years ago
parent
commit
5b867b4a4a
8 changed files with 39 additions and 66 deletions
  1. 4 4
      cell_test.go
  2. 1 1
      file_test.go
  3. 1 12
      sheet.go
  4. 2 10
      sheet_test.go
  5. 4 14
      style.go
  6. 1 6
      style_test.go
  7. 24 17
      xmlStyle.go
  8. 2 2
      xmlStyle_test.go

+ 4 - 4
cell_test.go

@@ -43,7 +43,7 @@ func (s *CellSuite) TestSetStyleWithFonts(c *C) {
 	style.Font = *font
 	cell.SetStyle(style)
 	style = cell.GetStyle()
-	xFont, _, _, _, _ := style.makeXLSXStyleElements()
+	xFont, _, _, _ := style.makeXLSXStyleElements()
 	c.Assert(xFont.Sz.Val, Equals, "12")
 	c.Assert(xFont.Name.Val, Equals, "Calibra")
 }
@@ -55,7 +55,7 @@ func (s *CellSuite) TestGetStyleWithFills(c *C) {
 	style.Fill = fill
 	cell := &Cell{Value: "123", style: style}
 	style = cell.GetStyle()
-	_, xFill, _, _, _ := style.makeXLSXStyleElements()
+	_, xFill, _, _ := style.makeXLSXStyleElements()
 	c.Assert(xFill.PatternFill.PatternType, Equals, "solid")
 	c.Assert(xFill.PatternFill.BgColor.RGB, Equals, "00FF0000")
 	c.Assert(xFill.PatternFill.FgColor.RGB, Equals, "FF000000")
@@ -72,7 +72,7 @@ func (s *CellSuite) TestSetStyleWithFills(c *C) {
 	style.Fill = *fill
 	cell.SetStyle(style)
 	style = cell.GetStyle()
-	_, xFill, _, _, _ := style.makeXLSXStyleElements()
+	_, xFill, _, _ := style.makeXLSXStyleElements()
 	xPatternFill := xFill.PatternFill
 	c.Assert(xPatternFill.PatternType, Equals, "solid")
 	c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
@@ -86,7 +86,7 @@ func (s *CellSuite) TestGetStyleWithBorders(c *C) {
 	style.Border = border
 	cell := Cell{Value: "123", style: style}
 	style = cell.GetStyle()
-	_, _, xBorder, _, _ := style.makeXLSXStyleElements()
+	_, _, xBorder, _ := style.makeXLSXStyleElements()
 	c.Assert(xBorder.Left.Style, Equals, "thin")
 	c.Assert(xBorder.Right.Style, Equals, "thin")
 	c.Assert(xBorder.Top.Style, Equals, "thin")

+ 1 - 1
file_test.go

@@ -695,7 +695,7 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	// For now we only allow simple string data in the
 	// spreadsheet.  Style support will follow.
 	expectedStyles := `<?xml version="1.0" encoding="UTF-8"?>
-<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><fills count="2"><fill><patternFill patternType="none"><fgColor rgb="FFFFFFFF"/><bgColor rgb="00000000"/></patternFill></fill><fill><patternFill patternType="lightGray"/></fill></fills><borders count="1"><border><left style=""></left><right style=""></right><top style=""></top><bottom style=""></bottom></border></borders><cellStyleXfs count="1"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellStyleXfs><cellXfs count="2"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellXfs></styleSheet>`
+<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><fills count="2"><fill><patternFill patternType="none"><fgColor rgb="FFFFFFFF"/><bgColor rgb="00000000"/></patternFill></fill><fill><patternFill patternType="lightGray"/></fill></fills><borders count="1"><border><left style=""></left><right style=""></right><top style=""></top><bottom style=""></bottom></border></borders><cellXfs count="2"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellXfs></styleSheet>`
 
 	c.Assert(parts["xl/styles.xml"], Equals, expectedStyles)
 }

+ 1 - 12
sheet.go

@@ -322,7 +322,7 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 }
 
 func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
-	xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
+	xFont, xFill, xBorder, xCellXf := style.makeXLSXStyleElements()
 	fontId := styles.addFont(xFont)
 	fillId := styles.addFill(xFill)
 
@@ -332,10 +332,6 @@ func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfI
 	styles.addFill(greyfill)
 
 	borderId := styles.addBorder(xBorder)
-	xCellStyleXf.FontId = fontId
-	xCellStyleXf.FillId = fillId
-	xCellStyleXf.BorderId = borderId
-	xCellStyleXf.NumFmtId = builtInNumFmtIndex_GENERAL
 	xCellXf.FontId = fontId
 	xCellXf.FillId = fillId
 	xCellXf.BorderId = borderId
@@ -345,12 +341,6 @@ func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfI
 		xCellXf.ApplyNumberFormat = true
 	}
 
-	xCellStyleXf.Alignment.Horizontal = style.Alignment.Horizontal
-	xCellStyleXf.Alignment.Indent = style.Alignment.Indent
-	xCellStyleXf.Alignment.ShrinkToFit = style.Alignment.ShrinkToFit
-	xCellStyleXf.Alignment.TextRotation = style.Alignment.TextRotation
-	xCellStyleXf.Alignment.Vertical = style.Alignment.Vertical
-	xCellStyleXf.Alignment.WrapText = style.Alignment.WrapText
 	xCellXf.Alignment.Horizontal = style.Alignment.Horizontal
 	xCellXf.Alignment.Indent = style.Alignment.Indent
 	xCellXf.Alignment.ShrinkToFit = style.Alignment.ShrinkToFit
@@ -358,7 +348,6 @@ func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfI
 	xCellXf.Alignment.Vertical = style.Alignment.Vertical
 	xCellXf.Alignment.WrapText = style.Alignment.WrapText
 
-	styles.addCellStyleXf(xCellStyleXf)
 	XfId = styles.addCellXf(xCellXf)
 	return
 }

File diff suppressed because it is too large
+ 2 - 10
sheet_test.go


+ 4 - 14
style.go

@@ -27,11 +27,10 @@ func NewStyle() *Style {
 }
 
 // Generate the underlying XLSX style elements that correspond to the Style.
-func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
+func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellXf xlsxXf) {
 	xFont = xlsxFont{}
 	xFill = xlsxFill{}
 	xBorder = xlsxBorder{}
-	xCellStyleXf = xlsxXf{}
 	xCellXf = xlsxXf{}
 	xFont.Sz.Val = strconv.Itoa(style.Font.Size)
 	xFont.Name.Val = style.Font.Name
@@ -79,18 +78,9 @@ func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBo
 	xCellXf.ApplyFill = style.ApplyFill
 	xCellXf.ApplyFont = style.ApplyFont
 	xCellXf.ApplyAlignment = style.ApplyAlignment
-	xCellStyleXf.ApplyBorder = style.ApplyBorder
-	xCellStyleXf.ApplyFill = style.ApplyFill
-	xCellStyleXf.ApplyFont = style.ApplyFont
-	xCellStyleXf.ApplyAlignment = style.ApplyAlignment
-	xCellStyleXf.NumFmtId = 0
-
-	xCellStyleXf.Alignment.Horizontal = style.Alignment.Horizontal
-	xCellStyleXf.Alignment.Indent = style.Alignment.Indent
-	xCellStyleXf.Alignment.ShrinkToFit = style.Alignment.ShrinkToFit
-	xCellStyleXf.Alignment.TextRotation = style.Alignment.TextRotation
-	xCellStyleXf.Alignment.Vertical = style.Alignment.Vertical
-	xCellStyleXf.Alignment.WrapText = style.Alignment.WrapText
+	if style.NamedStyleIndex != nil {
+		xCellXf.XfId = style.NamedStyleIndex
+	}
 	return
 }
 

+ 1 - 6
style_test.go

@@ -35,9 +35,7 @@ func (s *StyleSuite) TestMakeXLSXStyleElements(c *C) {
 	style.ApplyFill = true
 
 	style.ApplyFont = true
-	xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
-	// c.Assert(xNumFmt.NumFmtId, Equals, 164)
-	// c.Assert(xNumFmt.FormatCode, Equals, "GENERAL")
+	xFont, xFill, xBorder, xCellXf := style.makeXLSXStyleElements()
 	c.Assert(xFont.Sz.Val, Equals, "12")
 	c.Assert(xFont.Name.Val, Equals, "Verdana")
 	c.Assert(xFont.B, NotNil)
@@ -50,9 +48,6 @@ func (s *StyleSuite) TestMakeXLSXStyleElements(c *C) {
 	c.Assert(xBorder.Right.Style, Equals, "thin")
 	c.Assert(xBorder.Top.Style, Equals, "thin")
 	c.Assert(xBorder.Bottom.Style, Equals, "thin")
-	c.Assert(xCellStyleXf.ApplyBorder, Equals, true)
-	c.Assert(xCellStyleXf.ApplyFill, Equals, true)
-	c.Assert(xCellStyleXf.ApplyFont, Equals, true)
 	c.Assert(xCellXf.ApplyBorder, Equals, true)
 	c.Assert(xCellXf.ApplyFill, Equals, true)
 	c.Assert(xCellXf.ApplyFont, Equals, true)

+ 24 - 17
xmlStyle.go

@@ -72,13 +72,13 @@ const (
 type xlsxStyleSheet struct {
 	XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
 
-	Fonts        xlsxFonts        `xml:"fonts,omitempty"`
-	Fills        xlsxFills        `xml:"fills,omitempty"`
-	Borders      xlsxBorders      `xml:"borders,omitempty"`
-	CellStyles   xlsxCellStyles   `xml:"cellStyles,omitempty"`
-	CellStyleXfs xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
-	CellXfs      xlsxCellXfs      `xml:"cellXfs,omitempty"`
-	NumFmts      xlsxNumFmts      `xml:"numFmts,omitempty"`
+	Fonts        xlsxFonts         `xml:"fonts,omitempty"`
+	Fills        xlsxFills         `xml:"fills,omitempty"`
+	Borders      xlsxBorders       `xml:"borders,omitempty"`
+	CellStyles   *xlsxCellStyles   `xml:"cellStyles,omitempty"`
+	CellStyleXfs *xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
+	CellXfs      xlsxCellXfs       `xml:"cellXfs,omitempty"`
+	NumFmts      xlsxNumFmts       `xml:"numFmts,omitempty"`
 
 	theme          *theme
 	styleCache     map[int]*Style
@@ -102,7 +102,7 @@ func (styles *xlsxStyleSheet) reset() {
 	// Microsoft seems to want an emtpy border to start with
 	styles.addBorder(xlsxBorder{})
 
-	styles.CellStyleXfs = xlsxCellStyleXfs{}
+	styles.CellStyleXfs = &xlsxCellStyleXfs{}
 
 	// add default xf
 	styles.CellXfs = xlsxCellXfs{Count: 1, Xf: []xlsxXf{{}}}
@@ -126,7 +126,7 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 	if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
 		xf := styles.CellXfs.Xf[styleIndex]
 
-		if xf.XfId != nil {
+		if xf.XfId != nil && styles.CellStyleXfs != nil {
 			namedStyleXf = styles.CellStyleXfs.Xf[*xf.XfId]
 			style.NamedStyleIndex = xf.XfId
 		} else {
@@ -268,6 +268,9 @@ func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
 
 func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
 	var cellStyleXf xlsxXf
+	if styles.CellStyleXfs == nil {
+		styles.CellStyleXfs = &xlsxCellStyleXfs{Count: 0}
+	}
 	for index, cellStyleXf = range styles.CellStyleXfs.Xf {
 		if cellStyleXf.Equals(xCellStyleXf) {
 			return index
@@ -388,11 +391,13 @@ func (styles *xlsxStyleSheet) Marshal() (result string, err error) {
 	}
 	result += xborders
 
-	xcellStyleXfs, err = styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
-	if err != nil {
-		return
+	if styles.CellStyleXfs != nil {
+		xcellStyleXfs, err = styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
+		if err != nil {
+			return
+		}
+		result += xcellStyleXfs
 	}
-	result += xcellStyleXfs
 
 	xcellXfs, err = styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
 	if err != nil {
@@ -400,11 +405,13 @@ func (styles *xlsxStyleSheet) Marshal() (result string, err error) {
 	}
 	result += xcellXfs
 
-	xcellStyles, err = styles.CellStyles.Marshal()
-	if err != nil {
-		return
+	if styles.CellStyles != nil {
+		xcellStyles, err = styles.CellStyles.Marshal()
+		if err != nil {
+			return
+		}
+		result += xcellStyles
 	}
-	result += xcellStyles
 
 	result += `</styleSheet>`
 	return

+ 2 - 2
xmlStyle_test.go

@@ -81,7 +81,7 @@ func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithABorder(c *C) {
 // Test we produce valid output for a style file with one cellStyleXf definition.
 func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyleXf(c *C) {
 	styles := newXlsxStyleSheet(nil)
-	styles.CellStyleXfs = xlsxCellStyleXfs{}
+	styles.CellStyleXfs = &xlsxCellStyleXfs{}
 	styles.CellStyleXfs.Count = 1
 	styles.CellStyleXfs.Xf = make([]xlsxXf, 1)
 	xf := xlsxXf{}
@@ -114,7 +114,7 @@ func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyleXf(c *C) {
 func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyle(c *C) {
 	var builtInId int
 	styles := newXlsxStyleSheet(nil)
-	styles.CellStyles = xlsxCellStyles{Count: 1}
+	styles.CellStyles = &xlsxCellStyles{Count: 1}
 	styles.CellStyles.CellStyle = make([]xlsxCellStyle, 1)
 
 	builtInId = 31

Some files were not shown because too many files changed in this diff