Browse Source

Commit current state for safety

Geoffrey J. Teale 11 years ago
parent
commit
25123a7790
7 changed files with 106 additions and 20 deletions
  1. 17 3
      file.go
  2. 8 0
      file_test.go
  3. 10 3
      lib.go
  4. 13 0
      lib_test.go
  5. BIN
      testfile.xlsx
  6. 22 12
      workbook.go
  7. 36 2
      workbook_test.go

+ 17 - 3
file.go

@@ -47,15 +47,29 @@ func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
 func (f *File) MarshallParts() ([]string, error) {
 	var parts []string
 	var refTable *RefTable = NewSharedStringRefTable()
+	var err error
+	var sheetCount int = len(f.Sheets)
 
-	parts = make([]string, len(f.Sheets) + 5)
+	marshal := func(thing interface{}) (string, error) {
+		body, err := xml.MarshalIndent(thing, "  ", "  ")
+		if err != nil {
+			return "", err
+		}
+		return xml.Header + string(body), nil
+	}
+
+	parts = make([]string, sheetCount + 5)
 	for i, sheet := range f.Sheets {
 		xSheet := sheet.makeXLSXSheet(refTable)
-		body, err := xml.MarshalIndent(xSheet, "  ", "  ")
+		parts[i], err = marshal(xSheet)
 		if err != nil {
 			return parts, err
 		}
-		parts[i] = xml.Header + string(body)
+	}
+	xSST := refTable.makeXLSXSST()
+	parts[sheetCount], err = marshal(xSST)
+	if err != nil {
+		return parts, err
 	}
 	return parts, nil
 }

+ 8 - 0
file_test.go

@@ -90,4 +90,12 @@ func (l *FileSuite) TestMarshalFile(c *C) {
   </worksheet>`
 	c.Assert(parts[0], Equals, expectedSheet)
 	c.Assert(parts[1], Equals, expectedSheet)
+	expectedXLSXSST := `<?xml version="1.0" encoding="UTF-8"?>
+  <sst count="1" uniqueCount="1">
+    <si>
+      <t>A cell!</t>
+    </si>
+  </sst>`
+	c.Assert(parts[2], Equals, expectedXLSXSST)
+
 }

+ 10 - 3
lib.go

@@ -449,12 +449,19 @@ func readStylesFromZipFile(f *zip.File) (*xlsxStyles, error) {
 	return style, nil
 }
 
+
+type WorkBookRels map[string]string
+
+func (w *WorkBookRels) Marshal() string {
+	return ""
+}
+
 // readWorkbookRelationsFromZipFile is an internal helper function to
 // extract a map of relationship ID strings to the name of the
 // worksheet.xml file they refer to.  The resulting map can be used to
 // reliably derefence the worksheets in the XLSX file.
-func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string, error) {
-	var sheetXMLMap map[string]string
+func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
+	var sheetXMLMap WorkBookRels
 	var wbRelationships *xlsxWorkbookRels
 	var rc io.ReadCloser
 	var decoder *xml.Decoder
@@ -470,7 +477,7 @@ func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string
 	if err != nil {
 		return nil, err
 	}
-	sheetXMLMap = make(map[string]string)
+	sheetXMLMap = make(WorkBookRels)
 	for _, rel := range wbRelationships.Relationships {
 		if strings.HasSuffix(rel.Target, ".xml") && strings.HasPrefix(rel.Target, "worksheets/") {
 			sheetXMLMap[rel.Id] = strings.Replace(rel.Target[len("worksheets/"):], ".xml", "", 1)

+ 13 - 0
lib_test.go

@@ -138,6 +138,19 @@ func (l *LibSuite) TestReadWorkbookRelationsFromZipFileWithFunnyNames(c *C) {
 }
 
 
+// We can marshal WorkBookRels to an xml file
+func (l *LibSuite) TestWorkBookRelsMarshal(c *C) {
+	var rels WorkBookRels = make(WorkBookRels)
+	rels["rId1"] = "worksheets/sheet.xml"
+	expectedXML := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="worksheets/sheet.xml"/>
+</Relationships>
+`
+	c.Assert(rels.Marshal(), Equals, expectedXML)
+}
+
+
 // Excel column codes are a special form of base26 that doesn't allow
 // zeros, except in the least significant part of the code.  Test we
 // can smoosh the numbers in a normal base26 representation (presented

BIN
testfile.xlsx


+ 22 - 12
workbook.go

@@ -24,6 +24,7 @@ type xlsxWorkbookRelation struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxWorkbook struct {
+	XMLName		xml.Name	`xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
 	FileVersion  xlsxFileVersion  `xml:"fileVersion"`
 	WorkbookPr   xlsxWorkbookPr   `xml:"workbookPr"`
 	BookViews    xlsxBookViews    `xml:"bookViews"`
@@ -37,10 +38,10 @@ type xlsxWorkbook struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type xlsxFileVersion struct {
-	AppName      string `xml:"appName,attr"`
-	LastEdited   string `xml:"lastEdited,attr"`
-	LowestEdited string `xml:"lowestEdited,attr"`
-	RupBuild     string `xml:"rupBuild,attr"`
+	AppName      string `xml:"appName,attr,omitempty"`
+	LastEdited   string `xml:"lastEdited,attr,omitempty"`
+	LowestEdited string `xml:"lowestEdited,attr,omitempty"`
+	RupBuild     string `xml:"rupBuild,attr,omitempty"`
 }
 
 // xlsxWorkbookPr directly maps the workbookPr element from the
@@ -48,7 +49,10 @@ type xlsxFileVersion struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type xlsxWorkbookPr struct {
-	DefaultThemeVersion string `xml:"defaultThemeVersion,attr"`
+	DefaultThemeVersion string `xml:"defaultThemeVersion,attr,omitempty"`
+	BackupFile	bool `xml:"backupFile,attr,omitempty"`
+	ShowObjects	string `xml:"showObjects,attr,omitempty"`
+	Date1904	bool `xml:"date1904,attr"`
 }
 
 // xlsxBookViews directly maps the bookViews element from the
@@ -64,10 +68,16 @@ type xlsxBookViews struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type xlsxWorkBookView struct {
-	XWindow      string `xml:"xWindow,attr"`
-	YWindow      string `xml:"yWindow,attr"`
-	WindowWidth  string `xml:"windowWidth,attr"`
-	WindowHeight string `xml:"windowHeight,attr"`
+	ActiveTab	int `xml:"activeTab,attr,omitempty"`
+	FirstSheet	int `xml:"firstSheet,attr,omitempty"`
+	ShowHorizontalScroll bool `xml:"showHorizontalScroll,attr,omitempty"`
+	ShowVerticalScroll bool `xml:"showVerticalScroll,attr,omitempty"`
+	ShowSheetTabs bool `xml:"showSheetTabs,attr,omitempty"`
+	TabRatio	int `xml:"tabRatio,attr,omitempty"`
+	WindowHeight	int `xml:"windowHeight,attr,omitempty"`
+	WindowWidth	int `xml:"windowWidth,attr,omitempty"`
+	XWindow      string `xml:"xWindow,attr,omitempty"`
+	YWindow      string `xml:"yWindow,attr,omitempty"`
 }
 
 // xlsxSheets directly maps the sheets element from the namespace
@@ -83,9 +93,9 @@ type xlsxSheets struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxSheet struct {
-	Name    string `xml:"name,attr"`
-	SheetId string `xml:"sheetId,attr"`
-	Id      string `xml:"id,attr"`
+	Name    string `xml:"name,attr,omitempty"`
+	SheetId string `xml:"sheetId,attr,omitempty"`
+	Id      string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
 }
 
 // xlsxDefinedNames directly maps the definedNames element from the

+ 36 - 2
workbook_test.go

@@ -60,8 +60,8 @@ func (w *WorkbookSuite) TestUnmarshallWorkbookXML(c *C) {
 	workBookView := workbook.BookViews.WorkBookView[0]
 	c.Assert(workBookView.XWindow, Equals, "120")
 	c.Assert(workBookView.YWindow, Equals, "75")
-	c.Assert(workBookView.WindowWidth, Equals, "15135")
-	c.Assert(workBookView.WindowHeight, Equals, "7620")
+	c.Assert(workBookView.WindowWidth, Equals, 15135)
+	c.Assert(workBookView.WindowHeight, Equals, 7620)
 	c.Assert(workbook.Sheets.Sheet, HasLen,  3)
 	sheet := workbook.Sheets.Sheet[0]
 	c.Assert(sheet.Id, Equals, "rId1")
@@ -74,3 +74,37 @@ func (w *WorkbookSuite) TestUnmarshallWorkbookXML(c *C) {
 	c.Assert(dname.Name, Equals, "monitors")
 	c.Assert(workbook.CalcPr.CalcId, Equals, "125725")
 }
+
+
+// Test we can marshall a Workbook to xml
+func (w *WorkbookSuite) TestMarshallWorkbook(c *C) {
+	var workbook *xlsxWorkbook
+	workbook = new(xlsxWorkbook)
+	workbook.FileVersion = xlsxFileVersion{}
+	workbook.FileVersion.AppName = "xlsx"
+	workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false}
+	workbook.BookViews = xlsxBookViews{}
+	workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
+	workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{}
+	workbook.Sheets = xlsxSheets{}
+	workbook.Sheets.Sheet = make([]xlsxSheet, 1)
+	workbook.Sheets.Sheet[0] = xlsxSheet{Name: "sheet1", SheetId: "1", Id: "rId2"}
+	
+	body, err := xml.MarshalIndent(workbook, "  ", "  ")
+	c.Assert(err, IsNil)
+	expectedWorkbook := `  <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+    <fileVersion appName="xlsx"></fileVersion>
+    <workbookPr date1904="false"></workbookPr>
+    <bookViews>
+      <workbookView></workbookView>
+    </bookViews>
+    <sheets>
+      <sheet name="sheet1" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2"></sheet>
+    </sheets>
+    <definedNames></definedNames>
+    <calcPr calcId=""></calcPr>
+  </workbook>`
+	c.Assert(string(body), Equals, expectedWorkbook)
+	
+
+}