Browse Source

Marshal multiple sheets.

Geoffrey J. Teale 11 years ago
parent
commit
844d054c36
2 changed files with 48 additions and 0 deletions
  1. 18 0
      file.go
  2. 30 0
      file_test.go

+ 18 - 0
file.go

@@ -2,6 +2,7 @@ package xlsx
 
 
 import (
 import (
 	"archive/zip"
 	"archive/zip"
+	"encoding/xml"
 )
 )
 
 
 // File is a high level structure providing a slice of Sheet structs
 // File is a high level structure providing a slice of Sheet structs
@@ -41,3 +42,20 @@ func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
 	f.Sheet[sheetName] = sheet
 	f.Sheet[sheetName] = sheet
 	return sheet
 	return sheet
 }
 }
+
+
+func (f *File) MarshallParts() ([]string, error) {
+	var parts []string
+	var refTable *RefTable = NewSharedStringRefTable()
+
+	parts = make([]string, len(f.Sheets) + 5)
+	for i, sheet := range f.Sheets {
+		xSheet := sheet.makeXLSXSheet(refTable)
+		body, err := xml.MarshalIndent(xSheet, "  ", "  ")
+		if err != nil {
+			return parts, err
+		}
+		parts[i] = xml.Header + string(body)
+	}
+	return parts, nil
+}

+ 30 - 0
file_test.go

@@ -61,3 +61,33 @@ func (l *FileSuite) TestAddSheet(c *C) {
 	c.Assert(len(f.Sheet), Equals, 1)
 	c.Assert(len(f.Sheet), Equals, 1)
 	c.Assert(f.Sheet["MySheet"], Equals, sheet)
 	c.Assert(f.Sheet["MySheet"], Equals, sheet)
 }
 }
+
+// Test that we can marshall a File to a collection of xml files
+func (l *FileSuite) TestMarshalFile(c *C) {
+	var f *File
+	f = NewFile()
+	sheet1 := f.AddSheet("MySheet")
+	row1 := sheet1.AddRow()
+	cell1 := row1.AddCell()
+	cell1.Value = "A cell!"
+	sheet2 := f.AddSheet("AnotherSheet")
+	row2 := sheet2.AddRow()
+	cell2 := row2.AddCell()
+	cell2.Value = "A cell!"
+	parts, err := f.MarshallParts()
+	c.Assert(err, IsNil)
+	c.Assert(len(parts), Equals, 7)
+	expectedSheet := `<?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>0</v>
+        </c>
+      </row>
+    </sheetData>
+  </worksheet>`
+	c.Assert(parts[0], Equals, expectedSheet)
+	c.Assert(parts[1], Equals, expectedSheet)
+}