Browse Source

added DefinedNames to the file struct by looping over defined names in the function readSheetsFromZipFile()

DerLinkshaender 10 years ago
parent
commit
9bc5936b00
4 changed files with 30 additions and 5 deletions
  1. 3 0
      file.go
  2. 4 0
      lib.go
  3. 18 3
      xmlWorkbook.go
  4. 5 2
      xmlWorkbook_test.go

+ 3 - 0
file.go

@@ -21,6 +21,7 @@ type File struct {
 	Sheets         []*Sheet
 	Sheet          map[string]*Sheet
 	theme          *theme
+	DefinedNames   []*xlsxDefinedName
 }
 
 // Create a new File
@@ -28,6 +29,7 @@ func NewFile() (file *File) {
 	file = &File{}
 	file.Sheet = make(map[string]*Sheet)
 	file.Sheets = make([]*Sheet, 0)
+	file.DefinedNames = make([]*xlsxDefinedName, 0)
 	return
 }
 
@@ -172,6 +174,7 @@ func (f *File) makeWorkbook() xlsxWorkbook {
 	workbook.CalcPr.RefMode = "A1"
 	workbook.CalcPr.Iterate = false
 	workbook.CalcPr.IterateDelta = 0.001
+	workbook.DefinedNames = xlsxDefinedNames{}
 	return workbook
 }
 

+ 4 - 0
lib.go

@@ -627,6 +627,10 @@ func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]strin
 	}
 	file.Date1904 = workbook.WorkbookPr.Date1904
 
+	for _, entry := range workbook.DefinedNames.DefinedName {
+		file.DefinedNames = append(file.DefinedNames, &entry)
+	}
+
 	// Only try and read sheets that have corresponding files.
 	// Notably this excludes chartsheets don't right now
 	var workbookSheets []xlsxSheet

+ 18 - 3
xmlWorkbook.go

@@ -130,10 +130,25 @@ type xlsxDefinedNames struct {
 // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
 // - currently I have not checked it for completeness - it does as
 // much as I need.
+// for a descriptions of the attributes see
+// https://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.definedname.aspx
 type xlsxDefinedName struct {
-	Data         string `xml:",chardata"`
-	Name         string `xml:"name,attr"`
-	LocalSheetID string `xml:"localSheetId,attr"`
+	Data              string `xml:",chardata"`
+	Name              string `xml:"name,attr"`
+	Comment           string `xml:"comment,attr,omitempty"`
+	CustomMenu        string `xml:"customMenu,attr,omitempty"`
+	Description       string `xml:"description,attr,omitempty"`
+	Help              string `xml:"help,attr,omitempty"`
+	ShortcutKey       string `xml:"shortcutKey,attr,omitempty"`
+	StatusBar         string `xml:"statusBar,attr,omitempty"`
+	LocalSheetID      int    `xml:"localSheetId,attr,omitempty"`
+	FunctionGroupID   int    `xml:"functionGroupId,attr,omitempty"`
+	Function          bool   `xml:"function,attr,omitempty"`
+	Hidden            bool   `xml:"hidden,attr,omitempty"`
+	VbProcedure       bool   `xml:"vbProcedure,attr,omitempty"`
+	PublishToServer   bool   `xml:"publishToServer,attr,omitempty"`
+	WorkbookParameter bool   `xml:"workbookParameter,attr,omitempty"`
+	Xlm               bool   `xml:"xml,attr,omitempty"`
 }
 
 // xlsxCalcPr directly maps the calcPr element from the namespace

+ 5 - 2
xmlWorkbook_test.go

@@ -47,7 +47,8 @@ func (w *WorkbookSuite) TestUnmarshallWorkbookXML(c *C) {
                    state="veryHidden"/>
           </sheets>
           <definedNames>
-            <definedName name="monitors"
+            <definedName name="monitors" comment="this is the comment"
+                         description="give cells a name"
                          localSheetId="0">Sheet1!$A$1533</definedName>
           </definedNames>
           <calcPr calcId="125725"/>
@@ -77,8 +78,10 @@ func (w *WorkbookSuite) TestUnmarshallWorkbookXML(c *C) {
 	c.Assert(workbook.DefinedNames.DefinedName, HasLen, 1)
 	dname := workbook.DefinedNames.DefinedName[0]
 	c.Assert(dname.Data, Equals, "Sheet1!$A$1533")
-	c.Assert(dname.LocalSheetID, Equals, "0")
+	c.Assert(dname.LocalSheetID, Equals, 0)
 	c.Assert(dname.Name, Equals, "monitors")
+	c.Assert(dname.Comment, Equals, "this is the comment")
+	c.Assert(dname.Description, Equals, "give cells a name")
 	c.Assert(workbook.CalcPr.CalcId, Equals, "125725")
 }