Browse Source

Output styles information in XLSX file.

Geoffrey J. Teale 11 years ago
parent
commit
4e5c01b15a
6 changed files with 87 additions and 32 deletions
  1. 4 0
      cell.go
  2. 4 3
      file.go
  3. 53 3
      file_test.go
  4. 1 1
      row.go
  5. 1 1
      style.go
  6. 24 24
      xmlStyle.go

+ 4 - 0
cell.go

@@ -22,6 +22,10 @@ type CellInterface interface {
 	FormattedValue() string
 }
 
+func NewCell() *Cell {
+	return &Cell{style: *NewStyle()}
+}
+
 // String returns the value of a Cell as a string.
 func (c *Cell) String() string {
 	return c.FormattedValue()

+ 4 - 3
file.go

@@ -208,9 +208,10 @@ func (f *File) MarshallParts() (map[string]string, error) {
 		return parts, err
 	}
 
-	parts["xl/styles.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
-</styleSheet>`
+	parts["xl/styles.xml"], err = marshal(styles)
+	if err != nil {
+		return parts, err
+	}
 
 	return parts, nil
 }

+ 53 - 3
file_test.go

@@ -391,9 +391,59 @@ 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" standalone="yes"?>
-<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
-</styleSheet>`
+	expectedStyles := `<?xml version="1.0" encoding="UTF-8"?>
+  <xlsxStyles>
+    <fonts>
+      <font>
+        <sz val="12"></sz>
+        <name val="Verdana"></name>
+        <family val="0"></family>
+        <charset val="0"></charset>
+      </font>
+      <font>
+        <sz val="12"></sz>
+        <name val="Verdana"></name>
+        <family val="0"></family>
+        <charset val="0"></charset>
+      </font>
+    </fonts>
+    <fills>
+      <fill>
+        <patternFill>
+          <fgColor></fgColor>
+          <bgColor></bgColor>
+        </patternFill>
+      </fill>
+      <fill>
+        <patternFill>
+          <fgColor></fgColor>
+          <bgColor></bgColor>
+        </patternFill>
+      </fill>
+    </fills>
+    <borders>
+      <border>
+        <left></left>
+        <right></right>
+        <top></top>
+        <bottom></bottom>
+      </border>
+      <border>
+        <left></left>
+        <right></right>
+        <top></top>
+        <bottom></bottom>
+      </border>
+    </borders>
+    <cellStyleXfs>
+      <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>
+      <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>
+  </xlsxStyles>`
 	c.Assert(parts["xl/styles.xml"], Equals, expectedStyles)
 }
 

+ 1 - 1
row.go

@@ -6,7 +6,7 @@ type Row struct {
 }
 
 func (r *Row) AddCell() *Cell {
-	cell := &Cell{}
+	cell := NewCell()
 	r.Cells = append(r.Cells, cell)
 	return cell
 }

+ 1 - 1
style.go

@@ -15,7 +15,7 @@ type Style struct {
 
 // Return a new Style structure initialised with the default values.
 func NewStyle() *Style {
-	return &Style{}
+	return &Style{Font: *NewFont(12, "Verdana")}
 }
 
 // Generate the underlying XLSX style elements that correspond to the Style.

+ 24 - 24
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        []xlsxFont   `xml:"fonts>font,"`
-	Fills        []xlsxFill   `xml:"fills>fill"`
-	Borders      []xlsxBorder `xml:"borders>border"`
-	CellStyleXfs []xlsxXf     `xml:"cellStyleXfs>xf"`
-	CellXfs      []xlsxXf     `xml:"cellXfs>xf"`
-	NumFmts      []xlsxNumFmt `xml:numFmts>numFmt"`
+	Fonts        []xlsxFont   `xml:"fonts>font,omitempty"`
+	Fills        []xlsxFill   `xml:"fills>fill,omitempty"`
+	Borders      []xlsxBorder `xml:"borders>border,omitempty"`
+	CellStyleXfs []xlsxXf     `xml:"cellStyleXfs>xf,omitempty"`
+	CellXfs      []xlsxXf     `xml:"cellXfs>xf,omitempty"`
+	NumFmts      []xlsxNumFmt `xml:numFmts>numFmt,omitempty"`
 }
 
 // xlsxNumFmt directly maps the numFmt element in the namespace
@@ -30,8 +30,8 @@ type xlsxStyles struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxNumFmt struct {
-	NumFmtId   int    `xml:"numFmtId"`
-	FormatCode string `xml:"formatCode"`
+	NumFmtId   int    `xml:"numFmtId,omitempty"`
+	FormatCode string `xml:"formatCode,omitempty"`
 }
 
 // xlsxFont directly maps the font element in the namespace
@@ -39,10 +39,10 @@ type xlsxNumFmt struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxFont struct {
-	Sz      xlsxVal `xml:"sz"`
-	Name    xlsxVal `xml:"name"`
-	Family  xlsxVal `xml:"family"`
-	Charset xlsxVal `xml:"charset"`
+	Sz      xlsxVal `xml:"sz,omitempty"`
+	Name    xlsxVal `xml:"name,omitempty"`
+	Family  xlsxVal `xml:"family,omitempty"`
+	Charset xlsxVal `xml:"charset,omitempty"`
 }
 
 // xlsxVal directly maps the val element in the namespace
@@ -50,7 +50,7 @@ type xlsxFont struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxVal struct {
-	Val string `xml:"val,attr"`
+	Val string `xml:"val,attr,omitempty"`
 }
 
 // xlsxFill directly maps the fill element in the namespace
@@ -58,7 +58,7 @@ type xlsxVal struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxFill struct {
-	PatternFill xlsxPatternFill `xml:"patternFill"`
+	PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
 }
 
 // xlsxPatternFill directly maps the patternFill element in the namespace
@@ -66,9 +66,9 @@ type xlsxFill struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxPatternFill struct {
-	PatternType string    `xml:"patternType,attr"`
-	FgColor     xlsxColor `xml:"fgColor"`
-	BgColor     xlsxColor `xml:"bgColor"`
+	PatternType string    `xml:"patternType,attr,omitempty"`
+	FgColor     xlsxColor `xml:"fgColor,omitempty"`
+	BgColor     xlsxColor `xml:"bgColor,omitempty"`
 }
 
 // xlsxColor is a common mapping used for both the fgColor and bgColor
@@ -77,7 +77,7 @@ type xlsxPatternFill struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxColor struct {
-	RGB string `xml:"rgb,attr"`
+	RGB string `xml:"rgb,attr,omitempty"`
 }
 
 // xlsxBorder directly maps the border element in the namespace
@@ -85,10 +85,10 @@ type xlsxColor struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxBorder struct {
-	Left   xlsxLine `xml:"left"`
-	Right  xlsxLine `xml:"right"`
-	Top    xlsxLine `xml:"top"`
-	Bottom xlsxLine `xml:"bottom"`
+	Left   xlsxLine `xml:"left,omitempty"`
+	Right  xlsxLine `xml:"right,omitempty"`
+	Top    xlsxLine `xml:"top,omitempty"`
+	Bottom xlsxLine `xml:"bottom,omitempty"`
 }
 
 // xlsxLine directly maps the line style element in the namespace
@@ -96,7 +96,7 @@ type xlsxBorder struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxLine struct {
-	Style string `xml:"style,attr"`
+	Style string `xml:"style,attr,omitempty"`
 }
 
 // xlsxXf directly maps the xf element in the namespace
@@ -113,7 +113,7 @@ type xlsxXf struct {
 	FillId          int           `xml:"fillId,attr"`
 	FontId          int           `xml:"fontId,attr"`
 	NumFmtId        int           `xml:"numFmtId,attr"`
-	alignment       xlsxAlignment `xml:"alignement"`
+	alignment       xlsxAlignment `xml:"alignment"`
 }
 
 type xlsxAlignment struct {