Browse Source

Merge in EricLagergren-master branch and resolve conflicts.

Geoffrey J. Teale 9 years ago
parent
commit
6e9c8e467d
6 changed files with 136 additions and 191 deletions
  1. 6 13
      cell.go
  2. 52 70
      file.go
  3. 2 4
      file_test.go
  4. 1 1
      lib.go
  5. 13 23
      style.go
  6. 62 80
      xmlStyle.go

+ 6 - 13
cell.go

@@ -103,11 +103,7 @@ func (c *Cell) SetFloatWithFormat(n float64, format string) {
 	c.cellType = CellTypeNumeric
 	c.cellType = CellTypeNumeric
 }
 }
 
 
-var timeLocationUTC *time.Location
-
-func init() {
-	timeLocationUTC, _ = time.LoadLocation("UTC")
-}
+var timeLocationUTC, _ = time.LoadLocation("UTC")
 
 
 func timeToUTCTime(t time.Time) time.Time {
 func timeToUTCTime(t time.Time) time.Time {
 	return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), timeLocationUTC)
 	return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), timeLocationUTC)
@@ -163,24 +159,21 @@ func (c *Cell) SetInt(n int) {
 
 
 // SetInt sets a cell's value to an integer.
 // SetInt sets a cell's value to an integer.
 func (c *Cell) SetValue(n interface{}) {
 func (c *Cell) SetValue(n interface{}) {
-	var s string
-	switch n.(type) {
+	switch t := n.(type) {
 	case time.Time:
 	case time.Time:
 		c.SetDateTime(n.(time.Time))
 		c.SetDateTime(n.(time.Time))
 		return
 		return
 	case int, int8, int16, int32, int64, float32, float64:
 	case int, int8, int16, int32, int64, float32, float64:
 		c.setGeneral(fmt.Sprintf("%v", n))
 		c.setGeneral(fmt.Sprintf("%v", n))
-		return
 	case string:
 	case string:
-		s = n.(string)
+		c.SetString(t)
 	case []byte:
 	case []byte:
-		s = string(n.([]byte))
+		c.SetString(string(t))
 	case nil:
 	case nil:
-		s = ""
+		c.SetString("")
 	default:
 	default:
-		s = fmt.Sprintf("%v", n)
+		c.SetString(fmt.Sprintf("%v", n))
 	}
 	}
-	c.SetString(s)
 }
 }
 
 
 // SetInt sets a cell's value to an integer.
 // SetInt sets a cell's value to an integer.

+ 52 - 70
file.go

@@ -25,12 +25,12 @@ type File struct {
 }
 }
 
 
 // Create a new File
 // Create a new File
-func NewFile() (file *File) {
-	file = &File{}
-	file.Sheet = make(map[string]*Sheet)
-	file.Sheets = make([]*Sheet, 0)
-	file.DefinedNames = make([]*xlsxDefinedName, 0)
-	return
+func NewFile() *File {
+	return &File{
+		Sheet:        make(map[string]*Sheet),
+		Sheets:       make([]*Sheet, 0),
+		DefinedNames: make([]*xlsxDefinedName, 0),
+	}
 }
 }
 
 
 // OpenFile() take the name of an XLSX file and returns a populated
 // OpenFile() take the name of an XLSX file and returns a populated
@@ -47,22 +47,19 @@ func OpenFile(filename string) (file *File, err error) {
 
 
 // OpenBinary() take bytes of an XLSX file and returns a populated
 // OpenBinary() take bytes of an XLSX file and returns a populated
 // xlsx.File struct for it.
 // xlsx.File struct for it.
-func OpenBinary(bs []byte) (file *File, err error) {
+func OpenBinary(bs []byte) (*File, error) {
 	r := bytes.NewReader(bs)
 	r := bytes.NewReader(bs)
-	file, err = OpenReaderAt(r, int64(r.Len()))
-	return
+	return OpenReaderAt(r, int64(r.Len()))
 }
 }
 
 
 // OpenReaderAt() take io.ReaderAt of an XLSX file and returns a populated
 // OpenReaderAt() take io.ReaderAt of an XLSX file and returns a populated
 // xlsx.File struct for it.
 // xlsx.File struct for it.
-func OpenReaderAt(r io.ReaderAt, size int64) (file *File, err error) {
-	var f *zip.Reader
-	f, err = zip.NewReader(r, size)
+func OpenReaderAt(r io.ReaderAt, size int64) (*File, error) {
+	file, err := zip.NewReader(r, size)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	file, err = ReadZipReader(f)
-	return
+	return ReadZipReader(file)
 }
 }
 
 
 // A convenient wrapper around File.ToSlice, FileToSlice will
 // A convenient wrapper around File.ToSlice, FileToSlice will
@@ -89,58 +86,46 @@ func FileToSlice(path string) ([][][]string, error) {
 
 
 // Save the File to an xlsx file at the provided path.
 // Save the File to an xlsx file at the provided path.
 func (f *File) Save(path string) (err error) {
 func (f *File) Save(path string) (err error) {
-	var target *os.File
-
-	target, err = os.Create(path)
+	target, err := os.Create(path)
 	if err != nil {
 	if err != nil {
-		return
+		return err
 	}
 	}
-
 	err = f.Write(target)
 	err = f.Write(target)
 	if err != nil {
 	if err != nil {
-		return
+		return err
 	}
 	}
-
 	return target.Close()
 	return target.Close()
 }
 }
 
 
 // Write the File to io.Writer as xlsx
 // Write the File to io.Writer as xlsx
 func (f *File) Write(writer io.Writer) (err error) {
 func (f *File) Write(writer io.Writer) (err error) {
-	var parts map[string]string
-	var zipWriter *zip.Writer
-
-	parts, err = f.MarshallParts()
+	parts, err := f.MarshallParts()
 	if err != nil {
 	if err != nil {
 		return
 		return
 	}
 	}
-
-	zipWriter = zip.NewWriter(writer)
-
+	zipWriter := zip.NewWriter(writer)
 	for partName, part := range parts {
 	for partName, part := range parts {
-		var writer io.Writer
-		writer, err = zipWriter.Create(partName)
+		w, err := zipWriter.Create(partName)
 		if err != nil {
 		if err != nil {
-			return
+			return err
 		}
 		}
-		_, err = writer.Write([]byte(part))
+		_, err = w.Write([]byte(part))
 		if err != nil {
 		if err != nil {
-			return
+			return err
 		}
 		}
 	}
 	}
-
-	err = zipWriter.Close()
-
-	return
+	return zipWriter.Close()
 }
 }
 
 
 // Add a new Sheet, with the provided name, to a File
 // Add a new Sheet, with the provided name, to a File
-func (f *File) AddSheet(sheetName string) (sheet *Sheet, err error) {
+func (f *File) AddSheet(sheetName string) (*Sheet, error) {
 	if _, exists := f.Sheet[sheetName]; exists {
 	if _, exists := f.Sheet[sheetName]; exists {
-		return nil, fmt.Errorf("Duplicate sheet name '%s'.", sheetName)
+		return nil, fmt.Errorf("duplicate sheet name '%s'.", sheetName)
 	}
 	}
-	sheet = &Sheet{Name: sheetName, File: f}
-	if len(f.Sheets) == 0 {
-		sheet.Selected = true
+	sheet := &Sheet{
+		Name:     sheetName,
+		File:     f,
+		Selected: len(f.Sheets) == 0,
 	}
 	}
 	f.Sheet[sheetName] = sheet
 	f.Sheet[sheetName] = sheet
 	f.Sheets = append(f.Sheets, sheet)
 	f.Sheets = append(f.Sheets, sheet)
@@ -148,34 +133,31 @@ func (f *File) AddSheet(sheetName string) (sheet *Sheet, err error) {
 }
 }
 
 
 func (f *File) makeWorkbook() xlsxWorkbook {
 func (f *File) makeWorkbook() xlsxWorkbook {
-	var workbook xlsxWorkbook
-	workbook = xlsxWorkbook{}
-	workbook.FileVersion = xlsxFileVersion{}
-	workbook.FileVersion.AppName = "Go XLSX"
-	workbook.WorkbookPr = xlsxWorkbookPr{
-		BackupFile:  false,
-		ShowObjects: "all"}
-	workbook.BookViews = xlsxBookViews{}
-	workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
-	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
-	workbook.DefinedNames = xlsxDefinedNames{}
-	return workbook
+	return xlsxWorkbook{
+		FileVersion: xlsxFileVersion{AppName: "Go XLSX"},
+		WorkbookPr:  xlsxWorkbookPr{ShowObjects: "all"},
+		BookViews: xlsxBookViews{
+			WorkBookView: []xlsxWorkBookView{
+				{
+					ShowHorizontalScroll: true,
+					ShowSheetTabs:        true,
+					ShowVerticalScroll:   true,
+					TabRatio:             204,
+					WindowHeight:         8192,
+					WindowWidth:          16384,
+					XWindow:              "0",
+					YWindow:              "0",
+				},
+			},
+		},
+		Sheets: xlsxSheets{Sheet: make([]xlsxSheet, len(f.Sheets))},
+		CalcPr: xlsxCalcPr{
+			IterateCount: 100,
+			RefMode:      "A1",
+			Iterate:      false,
+			IterateDelta: 0.001,
+		},
+	}
 }
 }
 
 
 // Some tools that read XLSX files have very strict requirements about
 // Some tools that read XLSX files have very strict requirements about

+ 2 - 4
file_test.go

@@ -245,13 +245,11 @@ func (l *FileSuite) TestAddSheet(c *C) {
 
 
 // Test that AddSheet returns an error if you try to add two sheets with the same name
 // Test that AddSheet returns an error if you try to add two sheets with the same name
 func (l *FileSuite) TestAddSheetWithDuplicateName(c *C) {
 func (l *FileSuite) TestAddSheetWithDuplicateName(c *C) {
-	var f *File
-
-	f = NewFile()
+	f := NewFile()
 	_, err := f.AddSheet("MySheet")
 	_, err := f.AddSheet("MySheet")
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 	_, err = f.AddSheet("MySheet")
 	_, err = f.AddSheet("MySheet")
-	c.Assert(err, ErrorMatches, "Duplicate sheet name 'MySheet'.")
+	c.Assert(err, ErrorMatches, "duplicate sheet name 'MySheet'.")
 }
 }
 
 
 // Test that we can get the Nth sheet
 // Test that we can get the Nth sheet

+ 1 - 1
lib.go

@@ -695,7 +695,7 @@ func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]strin
 	}
 	}
 	file.Date1904 = workbook.WorkbookPr.Date1904
 	file.Date1904 = workbook.WorkbookPr.Date1904
 
 
-	for entryNum, _ := range workbook.DefinedNames.DefinedName {
+	for entryNum := range workbook.DefinedNames.DefinedName {
 		file.DefinedNames = append(file.DefinedNames, &workbook.DefinedNames.DefinedName[entryNum])
 		file.DefinedNames = append(file.DefinedNames, &workbook.DefinedNames.DefinedName[entryNum])
 	}
 	}
 
 

+ 13 - 23
style.go

@@ -104,14 +104,10 @@ type Border struct {
 
 
 func NewBorder(left, right, top, bottom string) *Border {
 func NewBorder(left, right, top, bottom string) *Border {
 	return &Border{
 	return &Border{
-		Left:        left,
-		LeftColor:   "",
-		Right:       right,
-		RightColor:  "",
-		Top:         top,
-		TopColor:    "",
-		Bottom:      bottom,
-		BottomColor: "",
+		Left:   left,
+		Right:  right,
+		Top:    top,
+		Bottom: bottom,
 	}
 	}
 }
 }
 
 
@@ -124,7 +120,11 @@ type Fill struct {
 }
 }
 
 
 func NewFill(patternType, fgColor, bgColor string) *Fill {
 func NewFill(patternType, fgColor, bgColor string) *Fill {
-	return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
+	return &Fill{
+		PatternType: patternType,
+		FgColor:     fgColor,
+		BgColor:     bgColor,
+	}
 }
 }
 
 
 type Font struct {
 type Font struct {
@@ -151,13 +151,8 @@ type Alignment struct {
 	WrapText     bool
 	WrapText     bool
 }
 }
 
 
-var defaultFontSize int
-var defaultFontName string
-
-func init() {
-	defaultFontSize = 12
-	defaultFontName = "Verdana"
-}
+var defaultFontSize = 12
+var defaultFontName = "Verdana"
 
 
 func SetDefaultFont(size int, name string) {
 func SetDefaultFont(size int, name string) {
 	defaultFontSize = size
 	defaultFontSize = size
@@ -179,12 +174,7 @@ func DefaultBorder() *Border {
 
 
 func DefaultAlignment() *Alignment {
 func DefaultAlignment() *Alignment {
 	return &Alignment{
 	return &Alignment{
-		Horizontal:   "general",
-		Indent:       0,
-		ShrinkToFit:  false,
-		TextRotation: 0,
-		Vertical:     "bottom",
-		WrapText:     false,
+		Horizontal: "general",
+		Vertical:   "bottom",
 	}
 	}
-
 }
 }

+ 62 - 80
xmlStyle.go

@@ -80,18 +80,18 @@ type xlsxStyleSheet struct {
 	CellXfs      xlsxCellXfs       `xml:"cellXfs,omitempty"`
 	CellXfs      xlsxCellXfs       `xml:"cellXfs,omitempty"`
 	NumFmts      xlsxNumFmts       `xml:"numFmts,omitempty"`
 	NumFmts      xlsxNumFmts       `xml:"numFmts,omitempty"`
 
 
-	theme          *theme
+	theme *theme
+
+	sync.RWMutex   // protects the following
 	styleCache     map[int]*Style
 	styleCache     map[int]*Style
 	numFmtRefTable map[int]xlsxNumFmt
 	numFmtRefTable map[int]xlsxNumFmt
-	lock           *sync.RWMutex
 }
 }
 
 
 func newXlsxStyleSheet(t *theme) *xlsxStyleSheet {
 func newXlsxStyleSheet(t *theme) *xlsxStyleSheet {
-	stylesheet := new(xlsxStyleSheet)
-	stylesheet.theme = t
-	stylesheet.styleCache = make(map[int]*Style)
-	stylesheet.lock = new(sync.RWMutex)
-	return stylesheet
+	return &xlsxStyleSheet{
+		theme:      t,
+		styleCache: make(map[int]*Style),
+	}
 }
 }
 
 
 func (styles *xlsxStyleSheet) reset() {
 func (styles *xlsxStyleSheet) reset() {
@@ -115,18 +115,17 @@ func (styles *xlsxStyleSheet) reset() {
 	styles.NumFmts = xlsxNumFmts{}
 	styles.NumFmts = xlsxNumFmts{}
 }
 }
 
 
-func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
-	styles.lock.RLock()
+func (styles *xlsxStyleSheet) getStyle(styleIndex int) *Style {
+	styles.RLock()
 	style, ok := styles.styleCache[styleIndex]
 	style, ok := styles.styleCache[styleIndex]
-	styles.lock.RUnlock()
+	styles.RUnlock()
 	if ok {
 	if ok {
-		return
+		return style
 	}
 	}
+
+	style = new(Style)
+
 	var namedStyleXf xlsxXf
 	var namedStyleXf xlsxXf
-	style = &Style{}
-	style.Border = Border{}
-	style.Fill = Fill{}
-	style.Font = Font{}
 
 
 	xfCount := styles.CellXfs.Count
 	xfCount := styles.CellXfs.Count
 	if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
 	if styleIndex > -1 && xfCount > 0 && styleIndex <= xfCount {
@@ -189,9 +188,9 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 		if xf.Alignment.Vertical != "" {
 		if xf.Alignment.Vertical != "" {
 			style.Alignment.Vertical = xf.Alignment.Vertical
 			style.Alignment.Vertical = xf.Alignment.Vertical
 		}
 		}
-		styles.lock.Lock()
+		styles.Lock()
 		styles.styleCache[styleIndex] = style
 		styles.styleCache[styleIndex] = style
-		styles.lock.Unlock()
+		styles.Unlock()
 	}
 	}
 	return style
 	return style
 }
 }
@@ -199,9 +198,8 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 func (styles *xlsxStyleSheet) argbValue(color xlsxColor) string {
 func (styles *xlsxStyleSheet) argbValue(color xlsxColor) string {
 	if color.Theme != nil && styles.theme != nil {
 	if color.Theme != nil && styles.theme != nil {
 		return styles.theme.themeColor(int64(*color.Theme), color.Tint)
 		return styles.theme.themeColor(int64(*color.Theme), color.Tint)
-	} else {
-		return color.RGB
 	}
 	}
+	return color.RGB
 }
 }
 
 
 // Excel styles can reference number formats that are built-in, all of which
 // Excel styles can reference number formats that are built-in, all of which
@@ -241,7 +239,7 @@ func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
 	}
 	}
 	styles.Fonts.Font = append(styles.Fonts.Font, xFont)
 	styles.Fonts.Font = append(styles.Fonts.Font, xFont)
 	index = styles.Fonts.Count
 	index = styles.Fonts.Count
-	styles.Fonts.Count += 1
+	styles.Fonts.Count++
 	return
 	return
 }
 }
 
 
@@ -254,7 +252,7 @@ func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
 	}
 	}
 	styles.Fills.Fill = append(styles.Fills.Fill, xFill)
 	styles.Fills.Fill = append(styles.Fills.Fill, xFill)
 	index = styles.Fills.Count
 	index = styles.Fills.Count
-	styles.Fills.Count += 1
+	styles.Fills.Count++
 	return
 	return
 }
 }
 
 
@@ -268,7 +266,7 @@ func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
 	styles.Borders.Border = append(styles.Borders.Border, xBorder)
 	styles.Borders.Border = append(styles.Borders.Border, xBorder)
 	index = styles.Borders.Count
 	index = styles.Borders.Count
 
 
-	styles.Borders.Count += 1
+	styles.Borders.Count++
 	return
 	return
 }
 }
 
 
@@ -284,7 +282,7 @@ func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) {
 	}
 	}
 	styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
 	styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf)
 	index = styles.CellStyleXfs.Count
 	index = styles.CellStyleXfs.Count
-	styles.CellStyleXfs.Count += 1
+	styles.CellStyleXfs.Count++
 	return
 	return
 }
 }
 
 
@@ -298,7 +296,7 @@ func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
 
 
 	styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
 	styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
 	index = styles.CellXfs.Count
 	index = styles.CellXfs.Count
-	styles.CellXfs.Count += 1
+	styles.CellXfs.Count++
 	return
 	return
 }
 }
 
 
@@ -326,12 +324,12 @@ func (styles *xlsxStyleSheet) newNumFmt(formatCode string) xlsxNumFmt {
 
 
 	// The user define NumFmtId. The one less than 164 in built in.
 	// The user define NumFmtId. The one less than 164 in built in.
 	numFmtId = builtinNumFmtsCount + 1
 	numFmtId = builtinNumFmtsCount + 1
-	styles.lock.Lock()
-	defer styles.lock.Unlock()
+	styles.Lock()
+	defer styles.Unlock()
 	for {
 	for {
 		// get a unused NumFmtId
 		// get a unused NumFmtId
 		if _, ok = styles.numFmtRefTable[numFmtId]; ok {
 		if _, ok = styles.numFmtRefTable[numFmtId]; ok {
-			numFmtId += 1
+			numFmtId++
 		} else {
 		} else {
 			styles.addNumFmt(xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode})
 			styles.addNumFmt(xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode})
 			break
 			break
@@ -353,74 +351,63 @@ func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) {
 		}
 		}
 		styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
 		styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
 		styles.numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
 		styles.numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
-		styles.NumFmts.Count += 1
+		styles.NumFmts.Count++
 	}
 	}
 }
 }
 
 
-func (styles *xlsxStyleSheet) Marshal() (result string, err error) {
-	var xNumFmts string
-	var xfonts string
-	var xfills string
-	var xborders string
-	var xcellStyleXfs string
-	var xcellXfs string
-	var xcellStyles string
-
-	var outputFontMap map[int]int = make(map[int]int)
-	var outputFillMap map[int]int = make(map[int]int)
-	var outputBorderMap map[int]int = make(map[int]int)
+func (styles *xlsxStyleSheet) Marshal() (string, error) {
+	result := xml.Header + `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
 
 
-	result = xml.Header
-	result += `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
-
-	xNumFmts, err = styles.NumFmts.Marshal()
+	xNumFmts, err := styles.NumFmts.Marshal()
 	if err != nil {
 	if err != nil {
-		return
+		return "", err
 	}
 	}
 	result += xNumFmts
 	result += xNumFmts
 
 
-	xfonts, err = styles.Fonts.Marshal(outputFontMap)
+	outputFontMap := make(map[int]int)
+	xfonts, err := styles.Fonts.Marshal(outputFontMap)
 	if err != nil {
 	if err != nil {
-		return
+		return "", err
 	}
 	}
 	result += xfonts
 	result += xfonts
 
 
-	xfills, err = styles.Fills.Marshal(outputFillMap)
+	outputFillMap := make(map[int]int)
+	xfills, err := styles.Fills.Marshal(outputFillMap)
 	if err != nil {
 	if err != nil {
-		return
+		return "", err
 	}
 	}
 	result += xfills
 	result += xfills
 
 
-	xborders, err = styles.Borders.Marshal(outputBorderMap)
+	outputBorderMap := make(map[int]int)
+	xborders, err := styles.Borders.Marshal(outputBorderMap)
 	if err != nil {
 	if err != nil {
-		return
+		return "", err
 	}
 	}
 	result += xborders
 	result += xborders
 
 
 	if styles.CellStyleXfs != nil {
 	if styles.CellStyleXfs != nil {
-		xcellStyleXfs, err = styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
+		xcellStyleXfs, err := styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
 		if err != nil {
 		if err != nil {
-			return
+			return "", err
 		}
 		}
 		result += xcellStyleXfs
 		result += xcellStyleXfs
 	}
 	}
 
 
-	xcellXfs, err = styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
+	xcellXfs, err := styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
 	if err != nil {
 	if err != nil {
-		return
+		return "", err
 	}
 	}
 	result += xcellXfs
 	result += xcellXfs
 
 
 	if styles.CellStyles != nil {
 	if styles.CellStyles != nil {
-		xcellStyles, err = styles.CellStyles.Marshal()
+		xcellStyles, err := styles.CellStyles.Marshal()
 		if err != nil {
 		if err != nil {
-			return
+			return "", err
 		}
 		}
 		result += xcellStyles
 		result += xcellStyles
 	}
 	}
 
 
-	result += `</styleSheet>`
-	return
+	return result + "</styleSheet>", nil
 }
 }
 
 
 // xlsxNumFmts directly maps the numFmts element in the namespace
 // xlsxNumFmts directly maps the numFmts element in the namespace
@@ -484,7 +471,7 @@ func (fonts *xlsxFonts) Marshal(outputFontMap map[int]int) (result string, err e
 		}
 		}
 		if xfont != "" {
 		if xfont != "" {
 			outputFontMap[i] = emittedCount
 			outputFontMap[i] = emittedCount
-			emittedCount += 1
+			emittedCount++
 			subparts += xfont
 			subparts += xfont
 		}
 		}
 	}
 	}
@@ -525,7 +512,7 @@ func (font *xlsxFont) Equals(other xlsxFont) bool {
 }
 }
 
 
 func (font *xlsxFont) Marshal() (result string, err error) {
 func (font *xlsxFont) Marshal() (result string, err error) {
-	result = `<font>`
+	result = "<font>"
 	if font.Sz.Val != "" {
 	if font.Sz.Val != "" {
 		result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
 		result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
 	}
 	}
@@ -550,8 +537,7 @@ func (font *xlsxFont) Marshal() (result string, err error) {
 	if font.U != nil {
 	if font.U != nil {
 		result += "<u/>"
 		result += "<u/>"
 	}
 	}
-	result += `</font>`
-	return
+	return result + "</font>", nil
 }
 }
 
 
 // xlsxVal directly maps the val element in the namespace
 // xlsxVal directly maps the val element in the namespace
@@ -575,27 +561,27 @@ type xlsxFills struct {
 	Fill  []xlsxFill `xml:"fill,omitempty"`
 	Fill  []xlsxFill `xml:"fill,omitempty"`
 }
 }
 
 
-func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (result string, err error) {
-	emittedCount := 0
-	subparts := ""
+func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (string, error) {
+	var subparts string
+	var emittedCount int
 	for i, fill := range fills.Fill {
 	for i, fill := range fills.Fill {
-		var xfill string
-		xfill, err = fill.Marshal()
+		xfill, err := fill.Marshal()
 		if err != nil {
 		if err != nil {
-			return
+			return "", err
 		}
 		}
 		if xfill != "" {
 		if xfill != "" {
 			outputFillMap[i] = emittedCount
 			outputFillMap[i] = emittedCount
-			emittedCount += 1
+			emittedCount++
 			subparts += xfill
 			subparts += xfill
 		}
 		}
 	}
 	}
+	var result string
 	if emittedCount > 0 {
 	if emittedCount > 0 {
 		result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
 		result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
 		result += subparts
 		result += subparts
 		result += `</fills>`
 		result += `</fills>`
 	}
 	}
-	return
+	return result, nil
 }
 }
 
 
 // xlsxFill directly maps the fill element in the namespace
 // xlsxFill directly maps the fill element in the namespace
@@ -696,7 +682,7 @@ func (borders *xlsxBorders) Marshal(outputBorderMap map[int]int) (result string,
 		}
 		}
 		if xborder != "" {
 		if xborder != "" {
 			outputBorderMap[i] = emittedCount
 			outputBorderMap[i] = emittedCount
-			emittedCount += 1
+			emittedCount++
 			subparts += xborder
 			subparts += xborder
 		}
 		}
 	}
 	}
@@ -891,19 +877,16 @@ func (xf *xlsxXf) Equals(other xlsxXf) bool {
 }
 }
 
 
 func (xf *xlsxXf) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
 func (xf *xlsxXf) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
-	var xAlignment string
 	result = fmt.Sprintf(`<xf applyAlignment="%b" applyBorder="%b" applyFont="%b" applyFill="%b" applyNumberFormat="%b" applyProtection="%b" borderId="%d" fillId="%d" fontId="%d" numFmtId="%d"`, bool2Int(xf.ApplyAlignment), bool2Int(xf.ApplyBorder), bool2Int(xf.ApplyFont), bool2Int(xf.ApplyFill), bool2Int(xf.ApplyNumberFormat), bool2Int(xf.ApplyProtection), outputBorderMap[xf.BorderId], outputFillMap[xf.FillId], outputFontMap[xf.FontId], xf.NumFmtId)
 	result = fmt.Sprintf(`<xf applyAlignment="%b" applyBorder="%b" applyFont="%b" applyFill="%b" applyNumberFormat="%b" applyProtection="%b" borderId="%d" fillId="%d" fontId="%d" numFmtId="%d"`, bool2Int(xf.ApplyAlignment), bool2Int(xf.ApplyBorder), bool2Int(xf.ApplyFont), bool2Int(xf.ApplyFill), bool2Int(xf.ApplyNumberFormat), bool2Int(xf.ApplyProtection), outputBorderMap[xf.BorderId], outputFillMap[xf.FillId], outputFontMap[xf.FontId], xf.NumFmtId)
 	if xf.XfId != nil {
 	if xf.XfId != nil {
 		result += fmt.Sprintf(` xfId="%d"`, *xf.XfId)
 		result += fmt.Sprintf(` xfId="%d"`, *xf.XfId)
 	}
 	}
 	result += ">"
 	result += ">"
-	xAlignment, err = xf.Alignment.Marshal()
+	xAlignment, err := xf.Alignment.Marshal()
 	if err != nil {
 	if err != nil {
-		return
+		return result, err
 	}
 	}
-	result += xAlignment
-	result += `</xf>`
-	return
+	return result + xAlignment + "</xf>", nil
 }
 }
 
 
 type xlsxAlignment struct {
 type xlsxAlignment struct {
@@ -931,8 +914,7 @@ func (alignment *xlsxAlignment) Marshal() (result string, err error) {
 	if alignment.Vertical == "" {
 	if alignment.Vertical == "" {
 		alignment.Vertical = "bottom"
 		alignment.Vertical = "bottom"
 	}
 	}
-	result = fmt.Sprintf(`<alignment horizontal="%s" indent="%d" shrinkToFit="%b" textRotation="%d" vertical="%s" wrapText="%b"/>`, alignment.Horizontal, alignment.Indent, bool2Int(alignment.ShrinkToFit), alignment.TextRotation, alignment.Vertical, bool2Int(alignment.WrapText))
-	return
+	return fmt.Sprintf(`<alignment horizontal="%s" indent="%d" shrinkToFit="%b" textRotation="%d" vertical="%s" wrapText="%b"/>`, alignment.Horizontal, alignment.Indent, bool2Int(alignment.ShrinkToFit), alignment.TextRotation, alignment.Vertical, bool2Int(alignment.WrapText)), nil
 }
 }
 
 
 func bool2Int(b bool) int {
 func bool2Int(b bool) int {