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

Split out lib.go and lib_test.go to several smaller files, this helps make things a bit more readable.

Geoffrey J. Teale 11 лет назад
Родитель
Сommit
f06b375a2e
10 измененных файлов с 358 добавлено и 289 удалено
  1. 88 0
      cell.go
  2. 90 0
      cell_test.go
  3. 43 0
      file.go
  4. 63 0
      file_test.go
  5. 5 144
      lib.go
  6. 0 145
      lib_test.go
  7. 11 0
      row.go
  8. 20 0
      row_test.go
  9. 19 0
      sheet.go
  10. 19 0
      sheet_test.go

+ 88 - 0
cell.go

@@ -0,0 +1,88 @@
+package xlsx
+
+import (
+	"strconv"
+)
+
+// Style is a high level structure intended to provide user access to
+// the contents of Style within an XLSX file.
+type Style struct {
+	Border Border
+	Fill   Fill
+	Font   Font
+}
+
+// Border is a high level structure intended to provide user access to
+// the contents of Border Style within an Sheet.
+type Border struct {
+	Left   string
+	Right  string
+	Top    string
+	Bottom string
+}
+
+// Fill is a high level structure intended to provide user access to
+// the contents of background and foreground color index within an Sheet.
+type Fill struct {
+	PatternType string
+	BgColor     string
+	FgColor     string
+}
+
+type Font struct {
+	Size    int
+	Name    string
+	Family  int
+	Charset int
+}
+
+// Cell is a high level structure intended to provide user access to
+// the contents of Cell within an xlsx.Row.
+type Cell struct {
+	Value      string
+	styleIndex int
+	styles     *xlsxStyles
+}
+
+// CellInterface defines the public API of the Cell.
+type CellInterface interface {
+	String() string
+}
+
+// String returns the value of a Cell as a string.
+func (c *Cell) String() string {
+	return c.Value
+}
+
+// GetStyle returns the Style associated with a Cell
+func (c *Cell) GetStyle() *Style {
+	style := &Style{}
+
+	if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
+		xf := c.styles.CellXfs[c.styleIndex-1]
+		if xf.ApplyBorder {
+			var border Border
+			border.Left = c.styles.Borders[xf.BorderId].Left.Style
+			border.Right = c.styles.Borders[xf.BorderId].Right.Style
+			border.Top = c.styles.Borders[xf.BorderId].Top.Style
+			border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
+			style.Border = border
+		}
+		if xf.ApplyFill {
+			var fill Fill
+			fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
+			fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
+			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
+			style.Fill = fill
+		}
+		if xf.ApplyFont {
+			font := c.styles.Fonts[xf.FontId]
+			style.Font = Font{}
+			style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
+			style.Font.Name = font.Name.Val
+			style.Font.Family, _ = strconv.Atoi(font.Family.Val)
+			style.Font.Charset, _ = strconv.Atoi(font.Charset.Val)
+		}
+	}
+	return style
+}

+ 90 - 0
cell_test.go

@@ -0,0 +1,90 @@
+package xlsx
+
+import (
+	. "gopkg.in/check.v1"
+)
+
+type CellSuite struct {}
+
+var _ = Suite(&CellSuite{})
+
+// Test that GetStyle correctly converts the xlsxStyle.Fonts.
+func (s *CellSuite) TestGetStyleWithFonts(c *C) {
+	var cell *Cell
+	var style *Style
+	var xStyles *xlsxStyles
+	var fonts []xlsxFont
+	var cellXfs []xlsxXf
+
+	fonts = make([]xlsxFont, 1)
+	fonts[0] = xlsxFont{
+		Sz:   xlsxVal{Val: "10"},
+		Name: xlsxVal{Val: "Calibra"}}
+
+	cellXfs = make([]xlsxXf, 1)
+	cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
+
+	xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
+
+	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	style = cell.GetStyle()
+	c.Assert(style, NotNil)
+	c.Assert(style.Font.Size, Equals, 10)
+	c.Assert(style.Font.Name, Equals, "Calibra")
+}
+
+// Test that GetStyle correctly converts the xlsxStyle.Fills.
+func (s *CellSuite) TestGetStyleWithFills(c *C) {
+	var cell *Cell
+	var style *Style
+	var xStyles *xlsxStyles
+	var fills []xlsxFill
+	var cellXfs []xlsxXf
+
+	fills = make([]xlsxFill, 1)
+	fills[0] = xlsxFill{
+		PatternFill: xlsxPatternFill{
+			PatternType: "solid",
+			FgColor:     xlsxColor{RGB: "FF000000"},
+			BgColor:     xlsxColor{RGB: "00FF0000"}}}
+	cellXfs = make([]xlsxXf, 1)
+	cellXfs[0] = xlsxXf{ApplyFill: true, FillId: 0}
+
+	xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
+
+	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	style = cell.GetStyle()
+	fill := style.Fill
+	c.Assert(fill.PatternType, Equals, "solid")
+	c.Assert(fill.BgColor, Equals, "00FF0000")
+	c.Assert(fill.FgColor, Equals, "FF000000")
+}
+
+// Test that GetStyle correctly converts the xlsxStyle.Borders.
+func (s *CellSuite) TestGetStyleWithBorders(c *C) {
+	var cell *Cell
+	var style *Style
+	var xStyles *xlsxStyles
+	var borders []xlsxBorder
+	var cellXfs []xlsxXf
+
+	borders = make([]xlsxBorder, 1)
+	borders[0] = xlsxBorder{
+		Left:   xlsxLine{Style: "thin"},
+		Right:  xlsxLine{Style: "thin"},
+		Top:    xlsxLine{Style: "thin"},
+		Bottom: xlsxLine{Style: "thin"}}
+
+	cellXfs = make([]xlsxXf, 1)
+	cellXfs[0] = xlsxXf{ApplyBorder: true, BorderId: 0}
+
+	xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
+
+	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	style = cell.GetStyle()
+	border := style.Border
+	c.Assert(border.Left, Equals, "thin")
+	c.Assert(border.Right, Equals, "thin")
+	c.Assert(border.Top, Equals, "thin")
+	c.Assert(border.Bottom, Equals, "thin")
+}

+ 43 - 0
file.go

@@ -0,0 +1,43 @@
+package xlsx
+
+import (
+	"archive/zip"
+)
+
+// File is a high level structure providing a slice of Sheet structs
+// to the user.
+type File struct {
+	worksheets     map[string]*zip.File
+	referenceTable []string
+	styles         *xlsxStyles
+	Sheets         []*Sheet          // sheet access by index
+	Sheet          map[string]*Sheet // sheet access by name
+}
+
+
+// Create a new File
+func NewFile() (file *File) {
+	file = &File{};
+	file.Sheets = make([]*Sheet, 0, 100)
+	file.Sheet = make(map[string]*Sheet)
+	return
+}
+
+// OpenFile() take the name of an XLSX file and returns a populated
+// xlsx.File struct for it.
+func OpenFile(filename string) (*File, error) {
+	var f *zip.ReadCloser
+	f, err := zip.OpenReader(filename)
+	if err != nil {
+		return nil, err
+	}
+	return ReadZip(f)
+}
+
+// Add a new Sheet, with the provided name, to a File
+func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
+	sheet = &Sheet{}
+	f.Sheets = append(f.Sheets, sheet)
+	f.Sheet[sheetName] = sheet
+	return sheet
+}

+ 63 - 0
file_test.go

@@ -0,0 +1,63 @@
+package xlsx
+
+import (
+	. "gopkg.in/check.v1"
+)
+
+type FileSuite struct {}
+
+var _ = Suite(&FileSuite{})
+
+// Test we can correctly open a XSLX file and return a xlsx.File
+// struct.
+func (l *FileSuite) TestOpenFile(c *C) {
+	var xlsxFile *File
+	var error error
+
+	xlsxFile, error = OpenFile("testfile.xlsx")
+	c.Assert(error, IsNil)
+	c.Assert(xlsxFile, NotNil)
+}
+
+// Test we can create a File object from scratch
+func (l *FileSuite) TestCreateFile(c *C) {
+	var xlsxFile *File
+
+	xlsxFile = NewFile()
+	c.Assert(xlsxFile, NotNil)
+}
+
+// Test that when we open a real XLSX file we create xlsx.Sheet
+// objects for the sheets inside the file and that these sheets are
+// themselves correct.
+func (l *FileSuite) TestCreateSheet(c *C) {
+	var xlsxFile *File
+	var err error
+	var sheet *Sheet
+	var row *Row
+	xlsxFile, err = OpenFile("testfile.xlsx")
+	c.Assert(err, IsNil)
+	c.Assert(xlsxFile, NotNil)
+	sheetLen := len(xlsxFile.Sheets)
+	c.Assert(sheetLen, Equals, 3)
+	sheet = xlsxFile.Sheets[0]
+	rowLen := len(sheet.Rows)
+	c.Assert(rowLen, Equals, 2)
+	row = sheet.Rows[0]
+	c.Assert(len(row.Cells), Equals, 2)
+	cell := row.Cells[0]
+	cellstring := cell.String()
+	c.Assert(cellstring, Equals, "Foo")
+}
+
+// Test that we can add a sheet to a File
+func (l *FileSuite) TestAddSheet(c *C) {
+	var f *File
+	f = NewFile()
+	sheet := f.AddSheet("MySheet")
+	c.Assert(sheet, NotNil)
+	c.Assert(len(f.Sheets), Equals, 1)
+	c.Assert(f.Sheets[0], Equals, sheet)
+	c.Assert(len(f.Sheet), Equals, 1)
+	c.Assert(f.Sheet["MySheet"], Equals, sheet)
+}

+ 5 - 144
lib.go

@@ -22,112 +22,6 @@ func (e *XLSXReaderError) Error() string {
 	return e.Err
 }
 
-// Cell is a high level structure intended to provide user access to
-// the contents of Cell within an xlsx.Row.
-type Cell struct {
-	Value      string
-	styleIndex int
-	styles     *xlsxStyles
-}
-
-// CellInterface defines the public API of the Cell.
-type CellInterface interface {
-	String() string
-}
-
-// String returns the value of a Cell as a string.
-func (c *Cell) String() string {
-	return c.Value
-}
-
-// GetStyle returns the Style associated with a Cell
-func (c *Cell) GetStyle() *Style {
-	style := &Style{}
-
-	if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
-		xf := c.styles.CellXfs[c.styleIndex-1]
-		if xf.ApplyBorder {
-			var border Border
-			border.Left = c.styles.Borders[xf.BorderId].Left.Style
-			border.Right = c.styles.Borders[xf.BorderId].Right.Style
-			border.Top = c.styles.Borders[xf.BorderId].Top.Style
-			border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
-			style.Border = border
-		}
-		if xf.ApplyFill {
-			var fill Fill
-			fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
-			fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
-			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
-			style.Fill = fill
-		}
-		if xf.ApplyFont {
-			font := c.styles.Fonts[xf.FontId]
-			style.Font = Font{}
-			style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
-			style.Font.Name = font.Name.Val
-			style.Font.Family, _ = strconv.Atoi(font.Family.Val)
-			style.Font.Charset, _ = strconv.Atoi(font.Charset.Val)
-		}
-	}
-	return style
-}
-
-// Row is a high level structure indended to provide user access to a
-// row within a xlsx.Sheet.  An xlsx.Row contains a slice of xlsx.Cell.
-type Row struct {
-	Cells []*Cell
-}
-
-// Sheet is a high level structure intended to provide user access to
-// the contents of a particular sheet within an XLSX file.
-type Sheet struct {
-	Rows   []*Row
-	MaxRow int
-	MaxCol int
-}
-
-// Style is a high level structure intended to provide user access to
-// the contents of Style within an XLSX file.
-type Style struct {
-	Border Border
-	Fill   Fill
-	Font   Font
-}
-
-// Border is a high level structure intended to provide user access to
-// the contents of Border Style within an Sheet.
-type Border struct {
-	Left   string
-	Right  string
-	Top    string
-	Bottom string
-}
-
-// Fill is a high level structure intended to provide user access to
-// the contents of background and foreground color index within an Sheet.
-type Fill struct {
-	PatternType string
-	BgColor     string
-	FgColor     string
-}
-
-type Font struct {
-	Size    int
-	Name    string
-	Family  int
-	Charset int
-}
-
-// File is a high level structure providing a slice of Sheet structs
-// to the user.
-type File struct {
-	worksheets     map[string]*zip.File
-	referenceTable []string
-	styles         *xlsxStyles
-	Sheets         []*Sheet          // sheet access by index
-	Sheet          map[string]*Sheet // sheet access by name
-}
 
 // getRangeFromString is an internal helper function that converts
 // XLSX internal range syntax to a pair of integers.  For example,
@@ -280,8 +174,9 @@ func makeRowFromRaw(rawrow xlsxRow) *Row {
 	return row
 }
 
-// getValueFromCellData attempts to extract a valid value, usable in CSV form from the raw cell value.
-// Note - this is not actually general enough - we should support retaining tabs and newlines.
+// getValueFromCellData attempts to extract a valid value, usable in
+// CSV form from the raw cell value.  Note - this is not actually
+// general enough - we should support retaining tabs and newlines.
 func getValueFromCellData(rawcell xlsxC, reftable []string) string {
 	var value string = ""
 	var data string = rawcell.V
@@ -497,16 +392,6 @@ func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string
 	return sheetXMLMap, nil
 }
 
-// OpenFile() take the name of an XLSX file and returns a populated
-// xlsx.File struct for it.
-func OpenFile(filename string) (*File, error) {
-	var f *zip.ReadCloser
-	f, err := zip.OpenReader(filename)
-	if err != nil {
-		return nil, err
-	}
-	return ReadZip(f)
-}
 
 // ReadZip() takes a pointer to a zip.ReadCloser and returns a
 // xlsx.File struct populated with its contents.  In most cases
@@ -516,7 +401,8 @@ func ReadZip(f *zip.ReadCloser) (*File, error) {
 	return ReadZipReader(&f.Reader)
 }
 
-// ReadZipReader() can be used to read xlsx in memory without touch filesystem.
+// ReadZipReader() can be used to read an XLSX in memory without
+// touching thes filesystem.
 func ReadZipReader(r *zip.Reader) (*File, error) {
 	var err error
 	var file *File
@@ -592,28 +478,3 @@ func ReadZipReader(r *zip.Reader) (*File, error) {
 }
 
 
-// Create a new File
-func NewFile() (file *File) {
-	file = &File{};
-	file.Sheets = make([]*Sheet, 0, 100)
-	file.Sheet = make(map[string]*Sheet)
-	return
-}
-
-// Add a new Sheet, with the provided name, to a File
-func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
-	sheet = &Sheet{}
-	f.Sheets = append(f.Sheets, sheet)
-	f.Sheet[sheetName] = sheet
-	return sheet
-}
-
-// Add a new Row to a Sheet
-func (s *Sheet) AddRow() *Row {
-	row := &Row{}
-	s.Rows = append(s.Rows, row)
-	if len(s.Rows) > s.MaxRow {
-		s.MaxRow = len(s.Rows)
-	}
-	return row
-}

+ 0 - 145
lib_test.go

@@ -12,152 +12,7 @@ type LibSuite struct{}
 
 var _ = Suite(&LibSuite{})
 
-// Test we can correctly open a XSLX file and return a xlsx.File
-// struct.
-func (l *LibSuite) TestOpenFile(c *C) {
-	var xlsxFile *File
-	var error error
-
-	xlsxFile, error = OpenFile("testfile.xlsx")
-	c.Assert(error, IsNil)
-	c.Assert(xlsxFile, NotNil)
-
-}
-
-// Test we can create a File object from scratch
-func (l *LibSuite) TestCreateFile(c *C) {
-	var xlsxFile *File
-
-	xlsxFile = NewFile()
-	c.Assert(xlsxFile, NotNil)
-}
-
-// Test that when we open a real XLSX file we create xlsx.Sheet
-// objects for the sheets inside the file and that these sheets are
-// themselves correct.
-func (l *LibSuite) TestCreateSheet(c *C) {
-	var xlsxFile *File
-	var err error
-	var sheet *Sheet
-	var row *Row
-	xlsxFile, err = OpenFile("testfile.xlsx")
-	c.Assert(err, IsNil)
-	c.Assert(xlsxFile, NotNil)
-	sheetLen := len(xlsxFile.Sheets)
-	c.Assert(sheetLen, Equals, 3)
-	sheet = xlsxFile.Sheets[0]
-	rowLen := len(sheet.Rows)
-	c.Assert(rowLen, Equals, 2)
-	row = sheet.Rows[0]
-	c.Assert(len(row.Cells), Equals, 2)
-	cell := row.Cells[0]
-	cellstring := cell.String()
-	c.Assert(cellstring, Equals, "Foo")
-}
-
-// Test that we can add a sheet to a File
-func (l *LibSuite) TestAddSheet(c *C) {
-	var f *File
-	f = NewFile()
-	sheet := f.AddSheet("MySheet")
-	c.Assert(sheet, NotNil)
-	c.Assert(len(f.Sheets), Equals, 1)
-	c.Assert(f.Sheets[0], Equals, sheet)
-	c.Assert(len(f.Sheet), Equals, 1)
-	c.Assert(f.Sheet["MySheet"], Equals, sheet)
-}
-
-// Test we can add a Row to a Sheet
-func (l *LibSuite) TestAddRow(c *C) {
-	var f *File
-	f = NewFile()
-	sheet := f.AddSheet("MySheet")
-	row := sheet.AddRow()
-	c.Assert(row, NotNil)
-	c.Assert(len(sheet.Rows), Equals, 1)
-}
-
-
-// Test that GetStyle correctly converts the xlsxStyle.Fonts.
-func (l *LibSuite) TestGetStyleWithFonts(c *C) {
-	var cell *Cell
-	var style *Style
-	var xStyles *xlsxStyles
-	var fonts []xlsxFont
-	var cellXfs []xlsxXf
-
-	fonts = make([]xlsxFont, 1)
-	fonts[0] = xlsxFont{
-		Sz:   xlsxVal{Val: "10"},
-		Name: xlsxVal{Val: "Calibra"}}
 
-	cellXfs = make([]xlsxXf, 1)
-	cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
-
-	xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
-
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
-	style = cell.GetStyle()
-	c.Assert(style, NotNil)
-	c.Assert(style.Font.Size, Equals, 10)
-	c.Assert(style.Font.Name, Equals, "Calibra")
-}
-
-// Test that GetStyle correctly converts the xlsxStyle.Fills.
-func (l *LibSuite) TestGetStyleWithFills(c *C) {
-	var cell *Cell
-	var style *Style
-	var xStyles *xlsxStyles
-	var fills []xlsxFill
-	var cellXfs []xlsxXf
-
-	fills = make([]xlsxFill, 1)
-	fills[0] = xlsxFill{
-		PatternFill: xlsxPatternFill{
-			PatternType: "solid",
-			FgColor:     xlsxColor{RGB: "FF000000"},
-			BgColor:     xlsxColor{RGB: "00FF0000"}}}
-	cellXfs = make([]xlsxXf, 1)
-	cellXfs[0] = xlsxXf{ApplyFill: true, FillId: 0}
-
-	xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
-
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
-	style = cell.GetStyle()
-	fill := style.Fill
-	c.Assert(fill.PatternType, Equals, "solid")
-	c.Assert(fill.BgColor, Equals, "00FF0000")
-	c.Assert(fill.FgColor, Equals, "FF000000")
-}
-
-// Test that GetStyle correctly converts the xlsxStyle.Borders.
-func (l *LibSuite) TestGetStyleWithBorders(c *C) {
-	var cell *Cell
-	var style *Style
-	var xStyles *xlsxStyles
-	var borders []xlsxBorder
-	var cellXfs []xlsxXf
-
-	borders = make([]xlsxBorder, 1)
-	borders[0] = xlsxBorder{
-		Left:   xlsxLine{Style: "thin"},
-		Right:  xlsxLine{Style: "thin"},
-		Top:    xlsxLine{Style: "thin"},
-		Bottom: xlsxLine{Style: "thin"}}
-
-	cellXfs = make([]xlsxXf, 1)
-	cellXfs[0] = xlsxXf{ApplyBorder: true, BorderId: 0}
-
-	xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
-
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
-	style = cell.GetStyle()
-	border := style.Border
-	c.Assert(border.Left, Equals, "thin")
-	c.Assert(border.Right, Equals, "thin")
-	c.Assert(border.Top, Equals, "thin")
-	c.Assert(border.Bottom, Equals, "thin")
-}
 
 // Test that we can correctly extract a reference table from the
 // sharedStrings.xml file embedded in the XLSX file and return a

+ 11 - 0
row.go

@@ -0,0 +1,11 @@
+package xlsx
+
+type Row struct {
+	Cells []*Cell
+}
+
+func (r *Row) AddCell() *Cell {
+	cell := &Cell{}
+	r.Cells = append(r.Cells, cell)
+	return cell
+}

+ 20 - 0
row_test.go

@@ -0,0 +1,20 @@
+package xlsx
+
+import (
+	. "gopkg.in/check.v1"
+)
+
+type RowSuite struct {}
+
+var _ = Suite(&RowSuite{})
+
+// Test we can add a new Cell to a Row
+func (r *RowSuite) TestAddCell(c *C){
+	var f *File
+	f = NewFile()
+	sheet := f.AddSheet("MySheet")
+	row := sheet.AddRow()
+	cell := row.AddCell()
+	c.Assert(cell, NotNil)
+	c.Assert(len(row.Cells), Equals, 1)
+}

+ 19 - 0
sheet.go

@@ -0,0 +1,19 @@
+package xlsx
+
+// Sheet is a high level structure intended to provide user access to
+// the contents of a particular sheet within an XLSX file.
+type Sheet struct {
+	Rows   []*Row
+	MaxRow int
+	MaxCol int
+}
+
+// Add a new Row to a Sheet
+func (s *Sheet) AddRow() *Row {
+	row := &Row{}
+	s.Rows = append(s.Rows, row)
+	if len(s.Rows) > s.MaxRow {
+		s.MaxRow = len(s.Rows)
+	}
+	return row
+}

+ 19 - 0
sheet_test.go

@@ -0,0 +1,19 @@
+package xlsx
+
+import (
+	. "gopkg.in/check.v1"
+)
+
+type SheetSuite struct{}
+
+var _ = Suite(&SheetSuite{})
+
+// Test we can add a Row to a Sheet
+func (s *SheetSuite) TestAddRow(c *C) {
+	var f *File
+	f = NewFile()
+	sheet := f.AddSheet("MySheet")
+	row := sheet.AddRow()
+	c.Assert(row, NotNil)
+	c.Assert(len(sheet.Rows), Equals, 1)
+}