Browse Source

Handle 31 character sheet names correctly.

Kaur Kuut 7 years ago
parent
commit
b2084a9c9f
3 changed files with 16 additions and 2 deletions
  1. 2 2
      file.go
  2. 14 0
      file_test.go
  3. BIN
      testdocs/max_sheet_name_length.xlsx

+ 2 - 2
file.go

@@ -155,8 +155,8 @@ func (f *File) AddSheet(sheetName string) (*Sheet, error) {
 	if _, exists := f.Sheet[sheetName]; exists {
 		return nil, fmt.Errorf("duplicate sheet name '%s'.", sheetName)
 	}
-	if utf8.RuneCountInString(sheetName) >= 31 {
-		return nil, fmt.Errorf("sheet name must be less than 31 characters long.  It is currently '%d' characters long", utf8.RuneCountInString(sheetName))
+	if utf8.RuneCountInString(sheetName) > 31 {
+		return nil, fmt.Errorf("sheet name must be 31 or fewer characters long.  It is currently '%d' characters long", utf8.RuneCountInString(sheetName))
 	}
 	sheet := &Sheet{
 		Name:     sheetName,

+ 14 - 0
file_test.go

@@ -376,6 +376,20 @@ func (l *FileSuite) TestAppendSheetWithDuplicateName(c *C) {
 	c.Assert(err, ErrorMatches, "duplicate sheet name 'MySheet'.")
 }
 
+// Test that we can read & create a 31 rune sheet name
+func (l *FileSuite) TestMaxSheetNameLength(c *C) {
+	// Open a genuine xlsx created by Microsoft Excel 2007
+	xlsxFile, err := OpenFile("./testdocs/max_sheet_name_length.xlsx")
+	c.Assert(err, IsNil)
+	c.Assert(xlsxFile, NotNil)
+	c.Assert(xlsxFile.Sheets[0].Name, Equals, "αααααβββββγγγγγδδδδδεεεεεζζζζζη")
+	// Create a new file with the same sheet name
+	f := NewFile()
+	s, err := f.AddSheet(xlsxFile.Sheets[0].Name)
+	c.Assert(err, IsNil)
+	c.Assert(s.Name, Equals, "αααααβββββγγγγγδδδδδεεεεεζζζζζη")
+}
+
 // Test that we can get the Nth sheet
 func (l *FileSuite) TestNthSheet(c *C) {
 	var f *File

BIN
testdocs/max_sheet_name_length.xlsx