Skip to content

Commit

Permalink
Handle absence of shared strings file (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
dglsparsons committed Aug 21, 2020
1 parent c5a5506 commit f13424b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion shared_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type sharedStrings struct {
} `xml:"si"`
}

// Sentinel error to indicate that no shared strings file can be found
var errNoSharedStrings = errors.New("No shared strings file exists")

// getSharedStringsFile attempts to find and return the zip.File struct associated with the
// shared strings section of an xlsx file. An error is returned if the sharedStrings file
// does not exist, or cannot be found.
Expand All @@ -25,7 +28,7 @@ func getSharedStringsFile(files []*zip.File) (*zip.File, error) {
}
}

return nil, errors.New("Unable to locate shared strings file")
return nil, errNoSharedStrings
}

// getPopulatedValues gets a list of string values from the raw sharedStrings struct.
Expand Down Expand Up @@ -54,6 +57,10 @@ func getPopulatedValues(ss sharedStrings) []string {
// This serves as a large lookup table of values, so we can efficiently parse rows.
func getSharedStrings(files []*zip.File) ([]string, error) {
ssFile, err := getSharedStringsFile(files)
if err != nil && errors.Is(err, errNoSharedStrings) {
// Valid to contain no shared strings
return []string{}, nil
}
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions shared_strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ func TestGetSharedStringsFile(t *testing.T) {
require.Equal(t, zipFiles[1], file)
}

func TestErrorReturnedIfNoSharedStringsFile(t *testing.T) {
_, err := getSharedStringsFile([]*zip.File{})
func TestNoErrorReturnedIfNoSharedStringsFile(t *testing.T) {
actual, err := getSharedStrings([]*zip.File{})

require.EqualError(t, err, "Unable to locate shared strings file")
require.NoError(t, err)
require.Equal(t, actual, []string{})
}

var sharedStringsTests = map[string]string{
Expand Down
Binary file added test/test-no-shared-strings.xlsx
Binary file not shown.

0 comments on commit f13424b

Please sign in to comment.