Prechádzať zdrojové kódy

Move FileToSlice functionality and tests into file.go and file_test.go and kill the FileToSlice.go and FileToSlice_test.go files.

Geoffrey J. Teale 11 rokov pred
rodič
commit
bf492cf5d6
4 zmenil súbory, kde vykonal 85 pridanie a 69 odobranie
  1. 0 34
      FileToSlice.go
  2. 0 35
      FileToSlice_test.go
  3. 54 0
      file.go
  4. 31 0
      file_test.go

+ 0 - 34
FileToSlice.go

@@ -1,34 +0,0 @@
-package xlsx
-
-// get all raw data from excel
-// output index mean=> sheetIndex ,row ,cell ,value
-// not remove any cells
-func FileToSlice(path string) ([][][]string, error) {
-	f, err := OpenFile(path)
-	if err != nil {
-		return nil, err
-	}
-	return f.ToSlice()
-}
-
-// get all raw data from excel
-// output index mean=> sheetIndex ,row ,cell ,value
-// not remove any cells
-func (file *File) ToSlice() (output [][][]string, err error) {
-	output = [][][]string{}
-	for _, sheet := range file.Sheets {
-		s := [][]string{}
-		for _, row := range sheet.Rows {
-			if row == nil {
-				continue
-			}
-			r := []string{}
-			for _, cell := range row.Cells {
-				r = append(r, cell.String())
-			}
-			s = append(s, r)
-		}
-		output = append(output, s)
-	}
-	return output, nil
-}

+ 0 - 35
FileToSlice_test.go

@@ -1,35 +0,0 @@
-package xlsx
-
-import (
-	. "gopkg.in/check.v1"
-)
-
-type SliceReaderSuite struct{}
-
-var _ = Suite(&SliceReaderSuite{})
-
-func (s *SliceReaderSuite) TestFileToSlice(c *C) {
-	output, err := FileToSlice("testfile.xlsx")
-	c.Assert(err, IsNil)
-	fileToSliceCheckOutput(c, output)
-}
-
-func (s *SliceReaderSuite) TestFileObjToSlice(c *C) {
-	f, err := OpenFile("testfile.xlsx")
-	output, err := f.ToSlice()
-	c.Assert(err, IsNil)
-	fileToSliceCheckOutput(c, output)
-}
-
-func fileToSliceCheckOutput(c *C, output [][][]string) {
-	c.Assert(len(output), Equals, 3)
-	c.Assert(len(output[0]), Equals, 2)
-	c.Assert(len(output[0][0]), Equals, 2)
-	c.Assert(output[0][0][0], Equals, "Foo")
-	c.Assert(output[0][0][1], Equals, "Bar")
-	c.Assert(len(output[0][1]), Equals, 2)
-	c.Assert(output[0][1][0], Equals, "Baz")
-	c.Assert(output[0][1][1], Equals, "Quuk")
-	c.Assert(len(output[1]), Equals, 0)
-	c.Assert(len(output[2]), Equals, 0)
-}

+ 54 - 0
file.go

@@ -40,6 +40,28 @@ func OpenFile(filename string) (*File, error) {
 	return ReadZip(f)
 }
 
+// A convenient wrapper around File.ToSlice, FileToSlice will
+// return the raw data contained in an Excel XLSX file as three
+// dimensional slice.  The first index represents the sheet number,
+// the second the row number, and the third the cell number.
+//
+// For example:
+//
+//    var mySlice [][][]string
+//    var value string
+//    mySlice = xlsx.FileToSlice("myXLSX.xlsx")
+//    value = mySlice[0][0][0]
+//
+// Here, value would be set to the raw value of the cell A1 in the
+// first sheet in the XLSX file.
+func FileToSlice(path string) ([][][]string, error) {
+	f, err := OpenFile(path)
+	if err != nil {
+		return nil, err
+	}
+	return f.ToSlice()
+}
+
 // Save the File to an xlsx file at the provided path.
 func (f *File) Save(path string) (err error) {
 	var parts map[string]string
@@ -191,3 +213,35 @@ func (f *File) MarshallParts() (map[string]string, error) {
 
 	return parts, nil
 }
+
+// Return the raw data contained in the File as three
+// dimensional slice.  The first index represents the sheet number,
+// the second the row number, and the third the cell number.
+//
+// For example:
+//
+//    var mySlice [][][]string
+//    var value string
+//    mySlice = xlsx.FileToSlice("myXLSX.xlsx")
+//    value = mySlice[0][0][0]
+//
+// Here, value would be set to the raw value of the cell A1 in the
+// first sheet in the XLSX file.
+func (file *File) ToSlice() (output [][][]string, err error) {
+	output = [][][]string{}
+	for _, sheet := range file.Sheets {
+		s := [][]string{}
+		for _, row := range sheet.Rows {
+			if row == nil {
+				continue
+			}
+			r := []string{}
+			for _, cell := range row.Cells {
+				r = append(r, cell.String())
+			}
+			s = append(s, r)
+		}
+		output = append(output, s)
+	}
+	return output, nil
+}

+ 31 - 0
file_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"encoding/xml"
 	"path/filepath"
+
 	. "gopkg.in/check.v1"
 )
 
@@ -411,3 +412,33 @@ func (l *FileSuite) TestSaveFile(c *C) {
 	cell1 = row1.Cells[0]
 	c.Assert(cell1.Value, Equals, "A cell!")
 }
+
+type SliceReaderSuite struct{}
+
+var _ = Suite(&SliceReaderSuite{})
+
+func (s *SliceReaderSuite) TestFileToSlice(c *C) {
+	output, err := FileToSlice("testfile.xlsx")
+	c.Assert(err, IsNil)
+	fileToSliceCheckOutput(c, output)
+}
+
+func (s *SliceReaderSuite) TestFileObjToSlice(c *C) {
+	f, err := OpenFile("testfile.xlsx")
+	output, err := f.ToSlice()
+	c.Assert(err, IsNil)
+	fileToSliceCheckOutput(c, output)
+}
+
+func fileToSliceCheckOutput(c *C, output [][][]string) {
+	c.Assert(len(output), Equals, 3)
+	c.Assert(len(output[0]), Equals, 2)
+	c.Assert(len(output[0][0]), Equals, 2)
+	c.Assert(output[0][0][0], Equals, "Foo")
+	c.Assert(output[0][0][1], Equals, "Bar")
+	c.Assert(len(output[0][1]), Equals, 2)
+	c.Assert(output[0][1][0], Equals, "Baz")
+	c.Assert(output[0][1][1], Equals, "Quuk")
+	c.Assert(len(output[1]), Equals, 0)
+	c.Assert(len(output[2]), Equals, 0)
+}