Geoffrey J. Teale 11 rokov pred
rodič
commit
28307f3607
8 zmenil súbory, kde vykonal 55 pridanie a 21 odobranie
  1. 2 0
      col.go
  2. 3 1
      file_test.go
  3. 10 11
      lib.go
  4. 14 5
      lib_test.go
  5. 2 0
      row.go
  6. 18 1
      sheet.go
  7. 4 1
      sheet_test.go
  8. 2 2
      xmlWorksheet.go

+ 2 - 0
col.go

@@ -1,5 +1,7 @@
 package xlsx
 
 type Col struct {
+	Min    int
+	Max    int
 	Hidden bool
 }

+ 3 - 1
file_test.go

@@ -284,7 +284,9 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	expectedSheet := `<?xml version="1.0" encoding="UTF-8"?>
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <dimension ref="A1:A1"></dimension>
-    <cols></cols>
+    <cols>
+      <col min="1" max="1"></col>
+    </cols>
     <sheetData>
       <row r="1">
         <c r="A1" t="s">

+ 10 - 11
lib.go

@@ -326,7 +326,7 @@ func getValueFromCellData(rawcell xlsxC, reftable *RefTable) string {
 // readRowsFromSheet is an internal helper function that extracts the
 // rows from a XSLXWorksheet, poulates them with Cells and resolves
 // the value references from the reference table and stores them in
-func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int) {
+func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, int, int) {
 	var rows []*Row
 	var cols []*Col
 	var row *Row
@@ -336,7 +336,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 	var insertRowIndex, insertColIndex int
 
 	if len(Worksheet.SheetData.Row) == 0 {
-		return nil, 0, 0
+		return nil, nil, 0, 0
 	}
 	reftable = file.referenceTable
 	if len(Worksheet.Dimension.Ref) > 0 {
@@ -357,14 +357,13 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 			Hidden: false,
 		}
 	}
-	for colIndex := 0; colIndex < len(Worksheet.Cols.Col); colIndex++ {
-		rawcol := Worksheet.Cols.Col[colIndex]
-		for c := rawcol.Min - 1; c < colCount && c < rawcol.Max; c++ {
-			cols[c] = &Col{
-				Hidden: rawcol.Hidden,
-			}
-		}
+	for _, rawcol := range Worksheet.Cols.Col {
+		cols = append(cols, &Col{
+			Min:    rawcol.Min,
+			Max:    rawcol.Max,
+			Hidden: rawcol.Hidden})
 	}
+
 	for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
 		rawrow := Worksheet.SheetData.Row[rowIndex]
 		// Some spreadsheets will omit blank rows from the
@@ -408,7 +407,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 		}
 		insertRowIndex++
 	}
-	return rows, colCount, rowCount
+	return rows, cols, colCount, rowCount
 }
 
 type indexedSheet struct {
@@ -430,7 +429,7 @@ func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *F
 		return
 	}
 	sheet := new(Sheet)
-	sheet.Rows, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi)
+	sheet.Rows, sheet.Cols, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi)
 	result.Sheet = sheet
 	sc <- result
 }

+ 14 - 5
lib_test.go

@@ -303,7 +303,7 @@ func (l *LibSuite) TestReadRowsFromSheet(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 2)
 	row := rows[0]
@@ -312,6 +312,10 @@ func (l *LibSuite) TestReadRowsFromSheet(c *C) {
 	c.Assert(cell1.Value, Equals, "Foo")
 	cell2 := row.Cells[1]
 	c.Assert(cell2.Value, Equals, "Bar")
+	col := cols[0]
+	c.Assert(col.Min, Equals, 0)
+	c.Assert(col.Max, Equals, 0)
+	c.Assert(col.Hidden, Equals, false)
 }
 
 func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
@@ -356,7 +360,7 @@ func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
 
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	_, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	_, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 1)
 }
@@ -445,7 +449,7 @@ func (l *LibSuite) TestReadRowsFromSheetWithEmptyCells(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file)
 	c.Assert(maxRows, Equals, 3)
 	c.Assert(maxCols, Equals, 3)
 
@@ -460,6 +464,11 @@ func (l *LibSuite) TestReadRowsFromSheetWithEmptyCells(c *C) {
 
 	cell3 := row.Cells[2]
 	c.Assert(cell3.Value, Equals, "Yes")
+
+	col := cols[0]
+	c.Assert(col.Min, Equals, 0)
+	c.Assert(col.Max, Equals, 0)
+	c.Assert(col.Hidden, Equals, false)
 }
 
 func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
@@ -482,7 +491,7 @@ func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
 
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
+	rows, _, maxCol, maxRow := readRowsFromSheet(worksheet, file)
 	c.Assert(maxCol, Equals, 4)
 	c.Assert(maxRow, Equals, 8)
 
@@ -589,7 +598,7 @@ func (l *LibSuite) TestReadRowsFromSheetWithMultipleSpans(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 4)
 	row := rows[0]

+ 2 - 0
row.go

@@ -3,10 +3,12 @@ package xlsx
 type Row struct {
 	Cells  []*Cell
 	Hidden bool
+	sheet  *Sheet
 }
 
 func (r *Row) AddCell() *Cell {
 	cell := &Cell{}
 	r.Cells = append(r.Cells, cell)
+	r.sheet.maybeAddCol(len(r.Cells))
 	return cell
 }

+ 18 - 1
sheet.go

@@ -10,13 +10,14 @@ import (
 type Sheet struct {
 	Name   string
 	Rows   []*Row
+	Cols   []*Col
 	MaxRow int
 	MaxCol int
 }
 
 // Add a new Row to a Sheet
 func (s *Sheet) AddRow() *Row {
-	row := &Row{}
+	row := &Row{sheet: s}
 	s.Rows = append(s.Rows, row)
 	if len(s.Rows) > s.MaxRow {
 		s.MaxRow = len(s.Rows)
@@ -24,6 +25,14 @@ func (s *Sheet) AddRow() *Row {
 	return row
 }
 
+// Make sure we always have as many Cols as we do cells.
+func (s *Sheet) maybeAddCol(cellCount int) {
+	if cellCount > s.MaxCol {
+		s.Cols = append(s.Cols, &Col{Min: cellCount, Max: cellCount, Hidden: false})
+		s.MaxCol = cellCount
+	}
+}
+
 // Get a Cell by passing it's cartesian coordinates (zero based) as
 // row and column integer indexes.
 //
@@ -65,6 +74,14 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
 		}
 		xSheet.Row = append(xSheet.Row, xRow)
 	}
+
+	worksheet.Cols = xlsxCols{Col: make([]xlsxCol, maxCell)}
+	for _, col := range s.Cols {
+		worksheet.Cols.Col = append(worksheet.Cols.Col,
+			xlsxCol{Min: col.Min,
+				Max:    col.Max,
+				Hidden: col.Hidden})
+	}
 	worksheet.SheetData = xSheet
 	dimension := xlsxDimension{}
 	dimension.Ref = fmt.Sprintf("A1:%s%d",

+ 4 - 1
sheet_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
+
 	. "gopkg.in/check.v1"
 )
 
@@ -65,7 +66,9 @@ func (s *SheetSuite) TestMarshalSheet(c *C) {
 	expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <dimension ref="A1:A1"></dimension>
-    <cols></cols>
+    <cols>
+      <col min="1" max="1"></col>
+    </cols>
     <sheetData>
       <row r="1">
         <c r="A1" t="s">

+ 2 - 2
xmlWorksheet.go

@@ -11,7 +11,7 @@ import (
 type xlsxWorksheet struct {
 	XMLName   xml.Name      `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	Dimension xlsxDimension `xml:"dimension"`
-	Cols      xslxCols      `xml:"cols,omitempty"`
+	Cols      xlsxCols      `xml:"cols,omitempty"`
 	SheetData xlsxSheetData `xml:"sheetData"`
 }
 
@@ -19,7 +19,7 @@ type xlsxWorksheet struct {
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
 // as I need.
-type xslxCols struct {
+type xlsxCols struct {
 	Col []xlsxCol `xml:"col"`
 }