Browse Source

Merge branch 'llehouerou-absolutepath-workbookrel'

Geoffrey J. Teale 11 years ago
parent
commit
66ff1b356e
4 changed files with 25 additions and 13 deletions
  1. 14 13
      lib.go
  2. 10 0
      lib_test.go
  3. BIN
      noStylesAndSharedStringsTest.xlsx
  4. 1 0
      workbook.go

+ 14 - 13
lib.go

@@ -7,6 +7,7 @@ import (
 	"fmt"
 	"io"
 	"math"
+	"path"
 	"strconv"
 	"strings"
 )
@@ -630,11 +631,13 @@ func readSharedStringsFromZipFile(f *zip.File) ([]string, error) {
 	var decoder *xml.Decoder
 	var reftable []string
 
+
 	// In a file with no strings it's possible that
 	// sharedStrings.xml doesn't exist.  In this case the value
 	// passed as f will be nil.
 	if f == nil {
-		return make([]string,0), nil
+		reftable = make([]string, 0)
+		return reftable, nil
 	}
 	rc, error = f.Open()
 	if error != nil {
@@ -702,8 +705,9 @@ func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string
 	}
 	sheetXMLMap = make(map[string]string)
 	for _, rel := range wbRelationships.Relationships {
-		if strings.HasSuffix(rel.Target, ".xml") && strings.HasPrefix(rel.Target, "worksheets/") {
-			sheetXMLMap[rel.Id] = strings.Replace(rel.Target[len("worksheets/"):], ".xml", "", 1)
+		if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
+			_, filename := path.Split(rel.Target)
+			sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
 		}
 	}
 	return sheetXMLMap, nil
@@ -773,18 +777,15 @@ func ReadZipReader(r *zip.Reader) (*File, error) {
 	if err != nil {
 		return nil, err
 	}
-	if reftable == nil {
-		readerErr := new(XLSXReaderError)
-		readerErr.Err = "No valid sharedStrings.xml found in XLSX file"
-		return nil, readerErr
-	}
 	file.referenceTable = reftable
-	style, err = readStylesFromZipFile(styles)
-	if err != nil {
-		return nil, err
+	if styles != nil {
+		style, err = readStylesFromZipFile(styles)
+		if err != nil {
+			return nil, err
+		}
+
+		file.styles = style
 	}
-	file.styles = style
-	file.numFmtRefTable = buildNumFmtRefTable(style)
 	sheets, err = readSheetsFromZipFile(workbook, file, sheetXMLMap)
 	if err != nil {
 		return nil, err

+ 10 - 0
lib_test.go

@@ -24,6 +24,16 @@ func (l *LibSuite) TestOpenFile(c *C) {
 
 }
 
+func (l *LibSuite) TestOpenFileWithoutStyleAndSharedStrings(c *C) {
+	var xlsxFile *File
+	var error error
+
+	xlsxFile, error = OpenFile("noStylesAndSharedStringsTest.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

BIN
noStylesAndSharedStringsTest.xlsx


+ 1 - 0
workbook.go

@@ -17,6 +17,7 @@ type xlsxWorkbookRels struct {
 type xlsxWorkbookRelation struct {
 	Id     string `xml:",attr"`
 	Target string `xml:",attr"`
+	Type   string `xml:",attr"`
 }
 
 // xlsxWorkbook directly maps the workbook element from the namespace