Browse Source

Make workboook file match the output of libreoffice.

Geoffrey J. Teale 11 years ago
parent
commit
aee85d2a83
4 changed files with 55 additions and 22 deletions
  1. 20 3
      file.go
  2. 16 12
      file_test.go
  3. 17 7
      xmlWorkbook.go
  4. 2 0
      xmlWorkbook_test.go

+ 20 - 3
file.go

@@ -112,12 +112,28 @@ func (f *File) makeWorkbook() xlsxWorkbook {
 	workbook = xlsxWorkbook{}
 	workbook.FileVersion = xlsxFileVersion{}
 	workbook.FileVersion.AppName = "Go XLSX"
-	workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false}
+	workbook.WorkbookPr = xlsxWorkbookPr{
+		BackupFile:  false,
+		ShowObjects: "all"}
 	workbook.BookViews = xlsxBookViews{}
 	workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
-	workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{}
+	workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{
+		ActiveTab:            0,
+		FirstSheet:           0,
+		ShowHorizontalScroll: true,
+		ShowSheetTabs:        true,
+		ShowVerticalScroll:   true,
+		TabRatio:             204,
+		WindowHeight:         8192,
+		WindowWidth:          16384,
+		XWindow:              "0",
+		YWindow:              "0"}
 	workbook.Sheets = xlsxSheets{}
 	workbook.Sheets.Sheet = make([]xlsxSheet, len(f.Sheets))
+	workbook.CalcPr.IterateCount = 100
+	workbook.CalcPr.RefMode = "A1"
+	workbook.CalcPr.Iterate = false
+	workbook.CalcPr.IterateDelta = 0.001
 	return workbook
 }
 
@@ -160,7 +176,8 @@ func (f *File) MarshallParts() (map[string]string, error) {
 		workbook.Sheets.Sheet[sheetIndex-1] = xlsxSheet{
 			Name:    sheet.Name,
 			SheetId: sheetId,
-			Id:      rId}
+			Id:      rId,
+			State:   "visible"}
 		parts[partName], err = marshal(xSheet)
 		if err != nil {
 			return parts, err

+ 16 - 12
file_test.go

@@ -240,26 +240,29 @@ func (l *FileSuite) TestMarshalWorkbook(c *C) {
 	workbook.Sheets.Sheet[0] = xlsxSheet{
 		Name:    "MyFirstSheet",
 		SheetId: "1",
-		Id:      "rId1"}
+		Id:      "rId1",
+		State:   "visible"}
 
 	workbook.Sheets.Sheet[1] = xlsxSheet{
 		Name:    "MySecondSheet",
 		SheetId: "2",
-		Id:      "rId2"}
+		Id:      "rId2",
+		State:   "visible"}
 
 	expectedWorkbook := `<?xml version="1.0" encoding="UTF-8"?>
    <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
       <fileVersion appName="Go XLSX"></fileVersion>
-      <workbookPr date1904="false"></workbookPr>
+      <workbookPr showObjects="all" date1904="false"></workbookPr>
+      <workbookProtection></workbookProtection>
       <bookViews>
-         <workbookView></workbookView>
+         <workbookView showHorizontalScroll="true" showVerticalScroll="true" showSheetTabs="true" tabRatio="204" windowHeight="8192" windowWidth="16384" xWindow="0" yWindow="0"></workbookView>
       </bookViews>
       <sheets>
-         <sheet name="MyFirstSheet" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId1"></sheet>
-         <sheet name="MySecondSheet" sheetId="2" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2"></sheet>
+         <sheet name="MyFirstSheet" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId1" state="visible"></sheet>
+         <sheet name="MySecondSheet" sheetId="2" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2" state="visible"></sheet>
       </sheets>
       <definedNames></definedNames>
-      <calcPr></calcPr>
+      <calcPr iterateCount="100" refMode="A1" iterateDelta="0.001"></calcPr>
    </workbook>`
 	output, err := xml.MarshalIndent(workbook, "   ", "   ")
 	c.Assert(err, IsNil)
@@ -715,16 +718,17 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	expectedWorkbook := `<?xml version="1.0" encoding="UTF-8"?>
   <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <fileVersion appName="Go XLSX"></fileVersion>
-    <workbookPr date1904="false"></workbookPr>
+    <workbookPr showObjects="all" date1904="false"></workbookPr>
+    <workbookProtection></workbookProtection>
     <bookViews>
-      <workbookView></workbookView>
+      <workbookView showHorizontalScroll="true" showVerticalScroll="true" showSheetTabs="true" tabRatio="204" windowHeight="8192" windowWidth="16384" xWindow="0" yWindow="0"></workbookView>
     </bookViews>
     <sheets>
-      <sheet name="MySheet" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId1"></sheet>
-      <sheet name="AnotherSheet" sheetId="2" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2"></sheet>
+      <sheet name="MySheet" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId1" state="visible"></sheet>
+      <sheet name="AnotherSheet" sheetId="2" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2" state="visible"></sheet>
     </sheets>
     <definedNames></definedNames>
-    <calcPr></calcPr>
+    <calcPr iterateCount="100" refMode="A1" iterateDelta="0.001"></calcPr>
   </workbook>`
 	c.Assert(parts["xl/workbook.xml"], Equals, expectedWorkbook)
 

+ 17 - 7
xmlWorkbook.go

@@ -26,13 +26,22 @@ 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"`
-	Sheets       xlsxSheets       `xml:"sheets"`
-	DefinedNames xlsxDefinedNames `xml:"definedNames"`
-	CalcPr       xlsxCalcPr       `xml:"calcPr"`
+	XMLName            xml.Name               `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
+	FileVersion        xlsxFileVersion        `xml:"fileVersion"`
+	WorkbookPr         xlsxWorkbookPr         `xml:"workbookPr"`
+	WorkbookProtection xlsxWorkbookProtection `xml:"workbookProtection"`
+	BookViews          xlsxBookViews          `xml:"bookViews"`
+	Sheets             xlsxSheets             `xml:"sheets"`
+	DefinedNames       xlsxDefinedNames       `xml:"definedNames"`
+	CalcPr             xlsxCalcPr             `xml:"calcPr"`
+}
+
+// xlsxWorkbookProtection directly maps the workbookProtection element from the
+// namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
+// - currently I have not checked it for completeness - it does as
+// much as I need.
+type xlsxWorkbookProtection struct {
+	// We don't need this, yet.
 }
 
 // xlsxFileVersion directly maps the fileVersion element from the
@@ -98,6 +107,7 @@ type xlsxSheet struct {
 	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"`
+	State   string `xml:"state,attr,omitempty"`
 }
 
 // xlsxDefinedNames directly maps the definedNames element from the

+ 2 - 0
xmlWorkbook_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
+
 	. "gopkg.in/check.v1"
 )
 
@@ -96,6 +97,7 @@ func (w *WorkbookSuite) TestMarshallWorkbook(c *C) {
 	expectedWorkbook := `  <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <fileVersion appName="xlsx"></fileVersion>
     <workbookPr date1904="false"></workbookPr>
+    <workbookProtection></workbookProtection>
     <bookViews>
       <workbookView></workbookView>
     </bookViews>