Browse Source

Added basic support for filters.

Kaur Kuut 8 years ago
parent
commit
8dcbfb6521
4 changed files with 48 additions and 0 deletions
  1. 10 0
      sheet.go
  2. 30 0
      sheet_test.go
  3. 5 0
      xmlWorksheet.go
  4. 3 0
      xmlWorksheet_test.go

+ 10 - 0
sheet.go

@@ -18,6 +18,7 @@ type Sheet struct {
 	Selected    bool
 	SheetViews  []SheetView
 	SheetFormat SheetFormat
+	AutoFilter  *AutoFilter
 }
 
 type SheetView struct {
@@ -39,6 +40,11 @@ type SheetFormat struct {
 	OutlineLevelRow  uint8
 }
 
+type AutoFilter struct {
+	TopLeftCell     string
+	BottomRightCell string
+}
+
 // Add a new Row to a Sheet
 func (s *Sheet) AddRow() *Row {
 	row := &Row{Sheet: s}
@@ -342,6 +348,10 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 		worksheet.MergeCells.Count = len(worksheet.MergeCells.Cells)
 	}
 
+	if s.AutoFilter != nil {
+		worksheet.AutoFilter = &xlsxAutoFilter{Ref: fmt.Sprintf("%v:%v", s.AutoFilter.TopLeftCell, s.AutoFilter.BottomRightCell)}
+	}
+
 	worksheet.SheetData = xSheet
 	dimension := xlsxDimension{}
 	dimension.Ref = fmt.Sprintf("A1:%s%d",

+ 30 - 0
sheet_test.go

@@ -392,3 +392,33 @@ func (s *SheetSuite) TestOutlineLevels(c *C) {
 	c.Assert(worksheet.SheetData.Row[1].OutlineLevel, Equals, uint8(2))
 	c.Assert(worksheet.SheetData.Row[2].OutlineLevel, Equals, uint8(0))
 }
+
+func (s *SheetSuite) TestAutoFilter(c *C) {
+	file := NewFile()
+	sheet, _ := file.AddSheet("Sheet1")
+
+	r1 := sheet.AddRow()
+	r1.AddCell()
+	r1.AddCell()
+	r1.AddCell()
+
+	r2 := sheet.AddRow()
+	r2.AddCell()
+	r2.AddCell()
+	r2.AddCell()
+
+	r3 := sheet.AddRow()
+	r3.AddCell()
+	r3.AddCell()
+	r3.AddCell()
+
+	// Define a filter area
+	sheet.AutoFilter = &AutoFilter{TopLeftCell: "B2", BottomRightCell: "C3"}
+
+	refTable := NewSharedStringRefTable()
+	styles := newXlsxStyleSheet(nil)
+	worksheet := sheet.makeXLSXSheet(refTable, styles)
+
+	c.Assert(worksheet.AutoFilter, NotNil)
+	c.Assert(worksheet.AutoFilter.Ref, Equals, "B2:C3")
+}

+ 5 - 0
xmlWorksheet.go

@@ -17,6 +17,7 @@ type xlsxWorksheet struct {
 	SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"`
 	Cols          *xlsxCols         `xml:"cols,omitempty"`
 	SheetData     xlsxSheetData     `xml:"sheetData"`
+	AutoFilter    *xlsxAutoFilter   `xml:"autoFilter,omitempty"`
 	MergeCells    *xlsxMergeCells   `xml:"mergeCells,omitempty"`
 	PrintOptions  xlsxPrintOptions  `xml:"printOptions"`
 	PageMargins   xlsxPageMargins   `xml:"pageMargins"`
@@ -236,6 +237,10 @@ type xlsxRow struct {
 	OutlineLevel uint8   `xml:"outlineLevel,attr,omitempty"`
 }
 
+type xlsxAutoFilter struct {
+	Ref string `xml:"ref,attr"`
+}
+
 type xlsxMergeCell struct {
 	Ref string `xml:"ref,attr"` // ref: horiz "A1:C1", vert "B3:B6", both  "D3:G4"
 }

+ 3 - 0
xmlWorksheet_test.go

@@ -93,6 +93,7 @@ func (w *WorksheetSuite) TestUnmarshallWorksheet(c *C) {
               </c>
             </row>
           </sheetData>
+          <autoFilter ref="A1:Z4" />
           <printOptions headings="false"
                         gridLines="false"
                         gridLinesSet="true"
@@ -141,6 +142,8 @@ func (w *WorksheetSuite) TestUnmarshallWorksheet(c *C) {
 	c.Assert(cell.R, Equals, "A1")
 	c.Assert(cell.T, Equals, "s")
 	c.Assert(cell.V, Equals, "0")
+	c.Assert(worksheet.AutoFilter, NotNil)
+	c.Assert(worksheet.AutoFilter.Ref, Equals, "A1:Z4")
 }
 
 // MergeCells information is correctly read from the worksheet.