Ver Fonte

Initialise empty Col structs to have a Min and Max value of 1. Fixes #170

Geoffrey J. Teale há 10 anos atrás
pai
commit
c8bbe4d758
5 ficheiros alterados com 39 adições e 21 exclusões
  1. 19 17
      lib.go
  2. 12 1
      lib_test.go
  3. 7 2
      sheet.go
  4. BIN
      testdocs/original.xlsx
  5. 1 1
      xmlWorksheet.go

+ 19 - 17
lib.go

@@ -454,23 +454,25 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File, sheet *Sheet) ([]*R
 		}
 	}
 
-	// Columns can apply to a range, for convenience we expand the
-	// ranges out into individual column definitions.
-	for _, rawcol := range Worksheet.Cols.Col {
-		// Note, below, that sometimes column definitions can
-		// exist outside the defined dimensions of the
-		// spreadsheet - we deliberately exclude these
-		// columns.
-		for i := rawcol.Min; i <= rawcol.Max && i <= colCount; i++ {
-			col := &Col{
-				Min:    rawcol.Min,
-				Max:    rawcol.Max,
-				Hidden: rawcol.Hidden,
-				Width:  rawcol.Width}
-			cols[i-1] = col
-			if file.styles != nil {
-				col.style = file.styles.getStyle(rawcol.Style)
-				col.numFmt = file.styles.getNumberFormat(rawcol.Style)
+	if Worksheet.Cols != nil {
+		// Columns can apply to a range, for convenience we expand the
+		// ranges out into individual column definitions.
+		for _, rawcol := range Worksheet.Cols.Col {
+			// Note, below, that sometimes column definitions can
+			// exist outside the defined dimensions of the
+			// spreadsheet - we deliberately exclude these
+			// columns.
+			for i := rawcol.Min; i <= rawcol.Max && i <= colCount; i++ {
+				col := &Col{
+					Min:    rawcol.Min,
+					Max:    rawcol.Max,
+					Hidden: rawcol.Hidden,
+					Width:  rawcol.Width}
+				cols[i-1] = col
+				if file.styles != nil {
+					col.style = file.styles.getStyle(rawcol.Style)
+					col.numFmt = file.styles.getNumberFormat(rawcol.Style)
+				}
 			}
 		}
 	}

+ 12 - 1
lib_test.go

@@ -3,7 +3,8 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
-	// "strconv"
+	"os"
+
 	"strings"
 
 	. "gopkg.in/check.v1"
@@ -1186,3 +1187,13 @@ func (l *LibSuite) TestRowNotOverwrittenWhenFollowedByEmptyRow(c *C) {
 	c.Assert(cells, HasLen, 1)
 	c.Assert(cells[0].Value, Equals, "75")
 }
+
+// This was a specific issue raised by a user.
+func (l *LibSuite) TestRoundTripFileWithNoSheetCols(c *C) {
+	originalXlFile, err := OpenFile("testdocs/original.xlsx")
+	c.Assert(err, IsNil)
+	originalXlFile.Save("testdocs/after_write.xlsx")
+	_, err = OpenFile("testdocs/after_write.xlsx")
+	c.Assert(err, IsNil)
+	os.Remove("testdocs/after_write.xlsx")
+}

+ 7 - 2
sheet.go

@@ -119,10 +119,15 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 	worksheet.SheetFormatPr.DefaultColWidth = s.SheetFormat.DefaultColWidth
 
 	colsXfIdList := make([]int, len(s.Cols))
-	worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
+	worksheet.Cols = &xlsxCols{Col: []xlsxCol{}}
 	for c, col := range s.Cols {
 		XfId := 0
-
+		if col.Min == 0 {
+			col.Min = 1
+		}
+		if col.Max == 0 {
+			col.Max = 1
+		}
 		style := col.GetStyle()
 		//col's style always not nil
 		if style != nil {

BIN
testdocs/original.xlsx


+ 1 - 1
xmlWorksheet.go

@@ -15,7 +15,7 @@ type xlsxWorksheet struct {
 	Dimension     xlsxDimension     `xml:"dimension"`
 	SheetViews    xlsxSheetViews    `xml:"sheetViews"`
 	SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"`
-	Cols          xlsxCols          `xml:"cols"`
+	Cols          *xlsxCols         `xml:"cols,omitempty"`
 	SheetData     xlsxSheetData     `xml:"sheetData"`
 	MergeCells    *xlsxMergeCells   `xml:"mergeCells,omitempty"`
 	PrintOptions  xlsxPrintOptions  `xml:"printOptions"`