Explorar el Código

Merge pull request #409 from xStrom/sheetname

Test for invalid characters in sheet name.
Ryan Hollis hace 7 años
padre
commit
075792b1f0
Se han modificado 2 ficheros con 16 adiciones y 0 borrados
  1. 7 0
      file.go
  2. 9 0
      file_test.go

+ 7 - 0
file.go

@@ -158,6 +158,13 @@ func (f *File) AddSheet(sheetName string) (*Sheet, error) {
 	if utf8.RuneCountInString(sheetName) >= 31 {
 	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))
 		return nil, fmt.Errorf("sheet name must be less than 31 characters long.  It is currently '%d' characters long", utf8.RuneCountInString(sheetName))
 	}
 	}
+	// Iterate over the runes
+	for _, r := range sheetName {
+		// Excel forbids : \ / ? * [ ]
+		if r == ':' || r == '\\' || r == '/' || r == '?' || r == '*' || r == '[' || r == ']' {
+			return nil, fmt.Errorf("sheet name must not contain any restricted characters : \\ / ? * [ ] but contains '%s'", string(r))
+		}
+	}
 	sheet := &Sheet{
 	sheet := &Sheet{
 		Name:     sheetName,
 		Name:     sheetName,
 		File:     f,
 		File:     f,

+ 9 - 0
file_test.go

@@ -389,6 +389,15 @@ func (l *FileSuite) TestNthSheet(c *C) {
 	c.Assert(sheetByIndex, Equals, sheetByName)
 	c.Assert(sheetByIndex, Equals, sheetByName)
 }
 }
 
 
+// Test invalid sheet name characters
+func (l *FileSuite) TestInvalidSheetNameCharacters(c *C) {
+	f := NewFile()
+	for _, invalidChar := range []string{":", "\\", "/", "?", "*", "[", "]"} {
+		_, err := f.AddSheet(invalidChar)
+		c.Assert(err, NotNil)
+	}
+}
+
 // Test that we can create a Workbook and marshal it to XML.
 // Test that we can create a Workbook and marshal it to XML.
 func (l *FileSuite) TestMarshalWorkbook(c *C) {
 func (l *FileSuite) TestMarshalWorkbook(c *C) {
 	var f *File
 	var f *File