Browse Source

Test passing on first version of sheet marshalling.

Geoffrey J. Teale 11 years ago
parent
commit
3065a60cf8
3 changed files with 40 additions and 17 deletions
  1. 26 1
      sheet.go
  2. 11 14
      sheet_test.go
  3. 3 2
      worksheet.go

+ 26 - 1
sheet.go

@@ -1,6 +1,7 @@
 package xlsx
 
 import (
+	"bytes"
 	"encoding/xml"
 	"fmt"
 )
@@ -25,11 +26,20 @@ func (s *Sheet) AddRow() *Row {
 
 // Dump sheet to it's XML representation
 func (s *Sheet) makeXLSXSheet() ([]byte, error) {
+	worksheet := &xlsxWorksheet{}
 	xSheet := xlsxSheetData{}
+	maxRow := 0
+	maxCell := 0
 	for r, row := range s.Rows {
+		if r > maxRow {
+			maxRow = r
+		}
 		xRow := xlsxRow{}
 		xRow.R = r + 1
 		for c, cell := range row.Cells {
+			if c > maxCell {
+				maxCell = c
+			}
 			xC := xlsxC{}
 			xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r + 1)
 			xC.V = cell.Value
@@ -38,5 +48,20 @@ func (s *Sheet) makeXLSXSheet() ([]byte, error) {
 		}
 		xSheet.Row = append(xSheet.Row, xRow)
 	}
-	return xml.MarshalIndent(xSheet, "  ", "  ")
+	worksheet.SheetData = xSheet
+	dimension := xlsxDimension{}
+	dimension.Ref = fmt.Sprintf("A1:%s%d",
+		numericToLetters(maxCell), maxRow + 1)
+	worksheet.Dimension = dimension
+	output := bytes.NewBufferString(xml.Header)
+	body, err := xml.MarshalIndent(worksheet, "  ", "  ")
+	if err != nil {
+		return nil, err
+	}
+	_, err = output.Write(body)
+	if err != nil {
+		return nil, err
+	}
+	return output.Bytes(), nil
+
 }

+ 11 - 14
sheet_test.go

@@ -26,20 +26,17 @@ func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
 	cell.Value = "A cell!"
 	xSheet, err := sheet.makeXLSXSheet()
 	c.Assert(err, IsNil)
-	expectedXLSXSheet := `
-<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
-           xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
-  <dimension ref="A1:A1"/>
-  <sheetData>
-    <row r="1">
-      <c r="A1" t="s">
-        <v>A cell!</v>
-      </c>
-    </row>
-  </sheetData>
-</worksheet>
-`
+	expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
+  <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+    <dimension ref="A1:A1"></dimension>
+    <sheetData>
+      <row r="1">
+        <c r="A1" t="s">
+          <v>A cell!</v>
+        </c>
+      </row>
+    </sheetData>
+  </worksheet>`
 	c.Assert(string(xSheet), Equals, expectedXLSXSheet)
 }
 

+ 3 - 2
worksheet.go

@@ -9,6 +9,7 @@ import (
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxWorksheet struct {
+	XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	Dimension xlsxDimension `xml:"dimension"`
 	SheetData xlsxSheetData `xml:"sheetData"`
 }
@@ -36,7 +37,7 @@ type xlsxSheetData struct {
 // as I need.
 type xlsxRow struct {
 	R     int     `xml:"r,attr"`
-	Spans string  `xml:"spans,attr"`
+	Spans string  `xml:"spans,attr,omitempty"`
 	C     []xlsxC `xml:"c"`
 }
 
@@ -46,7 +47,7 @@ type xlsxRow struct {
 // as I need.
 type xlsxC struct {
 	R string `xml:"r,attr"`  // Cell ID, e.g. A1
-	S int    `xml:"s,attr"`  // Style reference.
+	S int    `xml:"s,attr,omitempty"`  // Style reference.
 	T string `xml:"t,attr"`  // Type.
 	V string `xml:"v"`       // Value
 }