Просмотр исходного кода

Merge pull request #52 from trinchan/hidden_cells

Add hidden cell property and rudimentary column support
Geoffrey J. Teale 11 лет назад
Родитель
Сommit
a5e1efbb67
8 измененных файлов с 43 добавлено и 5 удалено
  1. 1 0
      cell.go
  2. 5 0
      col.go
  3. 1 0
      col_test.go
  4. 2 1
      file_test.go
  5. 16 0
      lib.go
  6. 2 1
      row.go
  7. 1 0
      sheet_test.go
  8. 15 3
      worksheet.go

+ 1 - 0
cell.go

@@ -59,6 +59,7 @@ type Cell struct {
 	styles         *xlsxStyles
 	styles         *xlsxStyles
 	numFmtRefTable map[int]xlsxNumFmt
 	numFmtRefTable map[int]xlsxNumFmt
 	date1904       bool
 	date1904       bool
+	Hidden         bool
 }
 }
 
 
 // CellInterface defines the public API of the Cell.
 // CellInterface defines the public API of the Cell.

+ 5 - 0
col.go

@@ -0,0 +1,5 @@
+package xlsx
+
+type Col struct {
+	Hidden bool
+}

+ 1 - 0
col_test.go

@@ -0,0 +1 @@
+package xlsx

+ 2 - 1
file_test.go

@@ -2,8 +2,8 @@ package xlsx
 
 
 import (
 import (
 	"encoding/xml"
 	"encoding/xml"
-	. "gopkg.in/check.v1"
 	"path/filepath"
 	"path/filepath"
+	. "gopkg.in/check.v1"
 )
 )
 
 
 type FileSuite struct{}
 type FileSuite struct{}
@@ -283,6 +283,7 @@ func (l *FileSuite) TestMarshalFile(c *C) {
 	expectedSheet := `<?xml version="1.0" encoding="UTF-8"?>
 	expectedSheet := `<?xml version="1.0" encoding="UTF-8"?>
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <dimension ref="A1:A1"></dimension>
     <dimension ref="A1:A1"></dimension>
+    <cols></cols>
     <sheetData>
     <sheetData>
       <row r="1">
       <row r="1">
         <c r="A1" t="s">
         <c r="A1" t="s">

+ 16 - 0
lib.go

@@ -334,6 +334,7 @@ func getValueFromCellData(rawcell xlsxC, reftable *RefTable) string {
 // the value references from the reference table and stores them in
 // 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, int, int) {
 	var rows []*Row
 	var rows []*Row
+	var cols []*Col
 	var row *Row
 	var row *Row
 	var minCol, maxCol, minRow, maxRow, colCount, rowCount int
 	var minCol, maxCol, minRow, maxRow, colCount, rowCount int
 	var reftable *RefTable
 	var reftable *RefTable
@@ -355,7 +356,21 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 	rowCount = (maxRow - minRow) + 1
 	rowCount = (maxRow - minRow) + 1
 	colCount = (maxCol - minCol) + 1
 	colCount = (maxCol - minCol) + 1
 	rows = make([]*Row, rowCount)
 	rows = make([]*Row, rowCount)
+	cols = make([]*Col, colCount)
 	insertRowIndex = minRow
 	insertRowIndex = minRow
+	for i := range cols {
+		cols[i] = &Col{
+			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 rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
 	for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
 		rawrow := Worksheet.SheetData.Row[rowIndex]
 		rawrow := Worksheet.SheetData.Row[rowIndex]
 		// Some spreadsheets will omit blank rows from the
 		// Some spreadsheets will omit blank rows from the
@@ -389,6 +404,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 			row.Cells[cellX].styles = file.styles
 			row.Cells[cellX].styles = file.styles
 			row.Cells[cellX].numFmtRefTable = file.numFmtRefTable
 			row.Cells[cellX].numFmtRefTable = file.numFmtRefTable
 			row.Cells[cellX].date1904 = file.Date1904
 			row.Cells[cellX].date1904 = file.Date1904
+			row.Cells[cellX].Hidden = rawrow.Hidden || cols[cellX].Hidden
 			insertColIndex++
 			insertColIndex++
 		}
 		}
 		if len(rows) > insertRowIndex-minRow {
 		if len(rows) > insertRowIndex-minRow {

+ 2 - 1
row.go

@@ -1,7 +1,8 @@
 package xlsx
 package xlsx
 
 
 type Row struct {
 type Row struct {
-	Cells []*Cell
+	Cells  []*Cell
+	Hidden bool
 }
 }
 
 
 func (r *Row) AddCell() *Cell {
 func (r *Row) AddCell() *Cell {

+ 1 - 0
sheet_test.go

@@ -65,6 +65,7 @@ func (s *SheetSuite) TestMarshalSheet(c *C) {
 	expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
 	expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
   <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <dimension ref="A1:A1"></dimension>
     <dimension ref="A1:A1"></dimension>
+    <cols></cols>
     <sheetData>
     <sheetData>
       <row r="1">
       <row r="1">
         <c r="A1" t="s">
         <c r="A1" t="s">

+ 15 - 3
worksheet.go

@@ -11,9 +11,20 @@ import (
 type xlsxWorksheet struct {
 type xlsxWorksheet struct {
 	XMLName   xml.Name      `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	XMLName   xml.Name      `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
 	Dimension xlsxDimension `xml:"dimension"`
 	Dimension xlsxDimension `xml:"dimension"`
+	Cols      xslxCols      `xml:"cols,omitempty"`
 	SheetData xlsxSheetData `xml:"sheetData"`
 	SheetData xlsxSheetData `xml:"sheetData"`
 }
 }
 
 
+type xslxCols struct {
+	Col []xlsxCol `xml:"col"`
+}
+
+type xlsxCol struct {
+	Min    int  `xml:"min,attr"`
+	Max    int  `xml:"max,attr"`
+	Hidden bool `xml:"hidden,attr,omitempty"`
+}
+
 // xlsxDimension directly maps the dimension element in the namespace
 // xlsxDimension directly maps the dimension element in the namespace
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
 // currently I have not checked it for completeness - it does as much
@@ -36,9 +47,10 @@ type xlsxSheetData struct {
 // currently I have not checked it for completeness - it does as much
 // currently I have not checked it for completeness - it does as much
 // as I need.
 // as I need.
 type xlsxRow struct {
 type xlsxRow struct {
-	R     int     `xml:"r,attr"`
-	Spans string  `xml:"spans,attr,omitempty"`
-	C     []xlsxC `xml:"c"`
+	R      int     `xml:"r,attr"`
+	Spans  string  `xml:"spans,attr,omitempty"`
+	Hidden bool    `xml:"hidden,attr,omitempty"`
+	C      []xlsxC `xml:"c"`
 }
 }
 
 
 // xlsxC directly maps the c element in the namespace
 // xlsxC directly maps the c element in the namespace