diff --git a/shared_strings.go b/shared_strings.go index 379ba0f..11ab66a 100644 --- a/shared_strings.go +++ b/shared_strings.go @@ -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. @@ -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. @@ -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 } diff --git a/shared_strings_test.go b/shared_strings_test.go index ef8bb69..7c4c163 100644 --- a/shared_strings_test.go +++ b/shared_strings_test.go @@ -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{ diff --git a/test/test-no-shared-strings.xlsx b/test/test-no-shared-strings.xlsx new file mode 100644 index 0000000..2d9a22f Binary files /dev/null and b/test/test-no-shared-strings.xlsx differ