Browse Source

Merge pull request #265 from dizk/master

Recover from error in file.ToSlice().
Geoffrey J. Teale 8 years ago
parent
commit
ccec065d7d
4 changed files with 14 additions and 1 deletions
  1. 1 0
      .gitignore
  2. 7 1
      file.go
  3. 6 0
      file_test.go
  4. BIN
      testdocs/testFileToSlice.xlsx

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+.vscode
 .DS_Store
 xlsx.test
 *.swp

+ 7 - 1
file.go

@@ -301,7 +301,13 @@ func (file *File) ToSlice() (output [][][]string, err error) {
 			for _, cell := range row.Cells {
 				str, err := cell.String()
 				if err != nil {
-					return output, err
+					// Recover from strconv.NumError if the value is an empty string,
+					// and insert an empty string in the output.
+					if numErr, ok := err.(*strconv.NumError); ok && numErr.Num == "" {
+						str = ""
+					} else {
+						return output, err
+					}
 				}
 				r = append(r, str)
 			}

+ 6 - 0
file_test.go

@@ -740,6 +740,12 @@ func (s *SliceReaderSuite) TestFileToSlice(c *C) {
 	fileToSliceCheckOutput(c, output)
 }
 
+func (s *SliceReaderSuite) TestFileToSliceMissingCol(c *C) {
+	// Test xlsx file with the A column removed
+	_, err := FileToSlice("./testdocs/testFileToSlice.xlsx")
+	c.Assert(err, IsNil)
+}
+
 func (s *SliceReaderSuite) TestFileObjToSlice(c *C) {
 	f, err := OpenFile("./testdocs/testfile.xlsx")
 	output, err := f.ToSlice()

BIN
testdocs/testFileToSlice.xlsx