Browse Source

Stop trying to assign to a nil map. *doh*

Geoffrey J. Teale 11 years ago
parent
commit
53b289a753
8 changed files with 40 additions and 28 deletions
  1. 4 5
      file.go
  2. 1 1
      file_test.go
  3. 7 6
      lib.go
  4. 1 1
      sheet.go
  5. 2 3
      style.go
  6. 4 3
      style_test.go
  7. 19 6
      xmlStyle.go
  8. 2 3
      xmlStyle_test.go

+ 4 - 5
file.go

@@ -13,7 +13,6 @@ import (
 // to the user.
 type File struct {
 	worksheets     map[string]*zip.File
-	numFmtRefTable NumFmtRefTable
 	referenceTable *RefTable
 	Date1904       bool
 	styles         *xlsxStyleSheet
@@ -24,7 +23,6 @@ type File struct {
 // Create a new File
 func NewFile() (file *File) {
 	file = &File{}
-	file.numFmtRefTable = make(NumFmtRefTable)
 	file.Sheet = make(map[string]*Sheet)
 	file.Sheets = make([]*Sheet, 0)
 	return
@@ -32,13 +30,14 @@ func NewFile() (file *File) {
 
 // OpenFile() take the name of an XLSX file and returns a populated
 // xlsx.File struct for it.
-func OpenFile(filename string) (*File, error) {
+func OpenFile(filename string) (file *File, err error) {
 	var f *zip.ReadCloser
-	f, err := zip.OpenReader(filename)
+	f, err = zip.OpenReader(filename)
 	if err != nil {
 		return nil, err
 	}
-	return ReadZip(f)
+	file, err = ReadZip(f)
+	return
 }
 
 // A convenient wrapper around File.ToSlice, FileToSlice will

+ 1 - 1
file_test.go

@@ -650,7 +650,7 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	// For now we only allow simple string data in the
 	// spreadsheet.  Style support will follow.
 	expectedStyles := `<?xml version="1.0" encoding="UTF-8"?>
-<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><numFmts count="1"><numFmt numFmtId="164" formatCode="GENERAL"/></numFmts><fonts count="2"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><cellStyleXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="164"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="164"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellStyleXfs><cellXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="164"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="164"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellXfs></styleSheet>`
+<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="2"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><cellStyleXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellStyleXfs><cellXfs count="2"><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf><xf applyAlignment="false" applyBorder="false" applyFont="false" applyFill="false" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="false" textRotation="0" vertical="" wrapText="false"/></xf></cellXfs></styleSheet>`
 	c.Assert(parts["xl/styles.xml"], Equals, expectedStyles)
 }
 

+ 7 - 6
lib.go

@@ -407,7 +407,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, in
 			row.Cells[cellX].Value = getValueFromCellData(rawcell, reftable)
 			if file.styles != nil {
 				row.Cells[cellX].style = file.styles.getStyle(rawcell.S)
-				row.Cells[cellX].numFmt = file.styles.getNumberFormat(rawcell.S, file.numFmtRefTable)
+				row.Cells[cellX].numFmt = file.styles.getNumberFormat(rawcell.S)
 			}
 			row.Cells[cellX].date1904 = file.Date1904
 			row.Cells[cellX].Hidden = rawrow.Hidden || (len(cols) > cellX && cols[cellX].Hidden)
@@ -535,15 +535,15 @@ func readStylesFromZipFile(f *zip.File) (*xlsxStyleSheet, error) {
 	if error != nil {
 		return nil, error
 	}
+	buildNumFmtRefTable(style)
 	return style, nil
 }
 
-func buildNumFmtRefTable(style *xlsxStyleSheet) map[int]xlsxNumFmt {
-	refTable := make(map[int]xlsxNumFmt)
+func buildNumFmtRefTable(style *xlsxStyleSheet) {
 	for _, numFmt := range style.NumFmts.NumFmt {
-		refTable[numFmt.NumFmtId] = numFmt
+		// We do this for the side effect of populating the NumFmtRefTable.
+		style.addNumFmt(numFmt)
 	}
-	return refTable
 }
 
 type WorkBookRels map[string]string
@@ -643,7 +643,8 @@ func ReadZipReader(r *zip.Reader) (*File, error) {
 	var workbookRels *zip.File
 	var worksheets map[string]*zip.File
 
-	file = new(File)
+	file = NewFile()
+	// file.numFmtRefTable = make(map[int]xlsxNumFmt, 1)
 	worksheets = make(map[string]*zip.File, len(r.File))
 	for _, v = range r.File {
 		switch v.Name {

+ 1 - 1
sheet.go

@@ -77,7 +77,7 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 			fontId := styles.addFont(xFont)
 			fillId := styles.addFill(xFill)
 			borderId := styles.addBorder(xBorder)
-			styles.addNumFmt(xNumFmt, s.File.numFmtRefTable)
+			styles.addNumFmt(xNumFmt)
 			xCellStyleXf.FontId = fontId
 			xCellStyleXf.FillId = fillId
 			xCellStyleXf.BorderId = borderId

+ 2 - 3
style.go

@@ -20,9 +20,6 @@ func NewStyle() *Style {
 
 // Generate the underlying XLSX style elements that correspond to the Style.
 func (style *Style) makeXLSXStyleElements() (xNumFmt xlsxNumFmt, xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellStyleXf xlsxXf, xCellXf xlsxXf) {
-	// We always set the general numberformat for now.
-	xNumFmt = xlsxNumFmt{NumFmtId: 164, FormatCode: "GENERAL"}
-
 	xFont = xlsxFont{}
 	xFill = xlsxFill{}
 	xBorder = xlsxBorder{}
@@ -45,9 +42,11 @@ func (style *Style) makeXLSXStyleElements() (xNumFmt xlsxNumFmt, xFont xlsxFont,
 	xCellXf.ApplyBorder = style.ApplyBorder
 	xCellXf.ApplyFill = style.ApplyFill
 	xCellXf.ApplyFont = style.ApplyFont
+	xCellXf.NumFmtId = 0
 	xCellStyleXf.ApplyBorder = style.ApplyBorder
 	xCellStyleXf.ApplyFill = style.ApplyFill
 	xCellStyleXf.ApplyFont = style.ApplyFont
+	xCellStyleXf.NumFmtId = 0
 	return
 }
 

+ 4 - 3
style_test.go

@@ -23,10 +23,11 @@ func (s *StyleSuite) TestMakeXLSXStyleElements(c *C) {
 	style.Border = border
 	style.ApplyBorder = true
 	style.ApplyFill = true
+
 	style.ApplyFont = true
-	xNumFmt, xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
-	c.Assert(xNumFmt.NumFmtId, Equals, 164)
-	c.Assert(xNumFmt.FormatCode, Equals, "GENERAL")
+	_, xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
+	// c.Assert(xNumFmt.NumFmtId, Equals, 164)
+	// c.Assert(xNumFmt.FormatCode, Equals, "GENERAL")
 	c.Assert(xFont.Sz.Val, Equals, "12")
 	c.Assert(xFont.Name.Val, Equals, "Verdana")
 	c.Assert(xFill.PatternFill.PatternType, Equals, "solid")

+ 19 - 6
xmlStyle.go

@@ -14,7 +14,9 @@ import (
 	"strings"
 )
 
-type NumFmtRefTable map[int]xlsxNumFmt
+var (
+	NumFmtRefTable map[int]xlsxNumFmt
+)
 
 // xlsxStyle directly maps the styleSheet element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
@@ -31,6 +33,14 @@ type xlsxStyleSheet struct {
 	NumFmts      xlsxNumFmts      `xml:"numFmts,omitempty"`
 }
 
+func (styles *xlsxStyleSheet) buildNumFmtRefTable() (numFmtRefTable map[int]xlsxNumFmt) {
+	numFmtRefTable = make(map[int]xlsxNumFmt)
+	for _, numFmt := range styles.NumFmts.NumFmt {
+		numFmtRefTable[numFmt.NumFmtId] = numFmt
+	}
+	return numFmtRefTable
+}
+
 func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style Style) {
 	var styleXf xlsxXf
 	style = Style{}
@@ -80,14 +90,14 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style Style) {
 
 }
 
-func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int, numFmtRefTable map[int]xlsxNumFmt) string {
+func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
 	if styles.CellXfs.Xf == nil {
 		return ""
 	}
 	var numberFormat string = ""
 	if styleIndex > -1 && styleIndex <= styles.CellXfs.Count {
 		xf := styles.CellXfs.Xf[styleIndex]
-		numFmt := numFmtRefTable[xf.NumFmtId]
+		numFmt := NumFmtRefTable[xf.NumFmtId]
 		numberFormat = numFmt.FormatCode
 	}
 	return strings.ToLower(numberFormat)
@@ -128,11 +138,14 @@ func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
 	return
 }
 
-func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt, numFmtRefTable NumFmtRefTable) (index int) {
-	numFmt, ok := numFmtRefTable[xNumFmt.NumFmtId]
+func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) (index int) {
+	numFmt, ok := NumFmtRefTable[xNumFmt.NumFmtId]
 	if !ok {
+		if NumFmtRefTable == nil {
+			NumFmtRefTable = make(map[int]xlsxNumFmt)
+		}
 		styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
-		numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
+		NumFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
 		index = styles.NumFmts.Count
 		styles.NumFmts.Count += 1
 		return

+ 2 - 3
xmlStyle_test.go

@@ -144,10 +144,9 @@ func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellXf(c *C) {
 func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithANumFmt(c *C) {
 	styles := &xlsxStyleSheet{}
 	styles.NumFmts = xlsxNumFmts{}
-	styles.NumFmts.Count = 1
-	styles.NumFmts.NumFmt = make([]xlsxNumFmt, 1)
+	styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
 	numFmt := xlsxNumFmt{NumFmtId: 164, FormatCode: "GENERAL"}
-	styles.NumFmts.NumFmt[0] = numFmt
+	styles.addNumFmt(numFmt)
 
 	expected := `<?xml version="1.0" encoding="UTF-8"?>
 <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><numFmts count="1"><numFmt numFmtId="164" formatCode="GENERAL"/></numFmts></styleSheet>`