diff --git a/errors.go b/errors.go index 7c7143c..1a6cc8a 100644 --- a/errors.go +++ b/errors.go @@ -40,10 +40,10 @@ func newInvalidExcelDateError(dateValue float64) error { return fmt.Errorf("invalid date value %f, negative values are not supported", dateValue) } -// newInvalidTableNameError defined the error message on receiving the invalid -// table name. -func newInvalidTableNameError(name string) error { - return fmt.Errorf("invalid table name %q", name) +// newInvalidNameError defined the error message on receiving the invalid +// defined name or table name. +func newInvalidNameError(name string) error { + return fmt.Errorf("invalid name %q, the name should be starts with a letter or underscore, can not include a space or character, and can not conflict with an existing name in the workbook", name) } // newUnsupportedChartType defined the error message on receiving the chart @@ -236,9 +236,9 @@ var ( // ErrSheetNameLength defined the error message on receiving the sheet // name length exceeds the limit. ErrSheetNameLength = fmt.Errorf("the sheet name length exceeds the %d characters limit", MaxSheetNameLength) - // ErrTableNameLength defined the error message on receiving the table name - // length exceeds the limit. - ErrTableNameLength = fmt.Errorf("the table name length exceeds the %d characters limit", MaxFieldLength) + // ErrNameLength defined the error message on receiving the defined name or + // table name length exceeds the limit. + ErrNameLength = fmt.Errorf("the name length exceeds the %d characters limit", MaxFieldLength) // ErrExistsTableName defined the error message on given table already exists. ErrExistsTableName = errors.New("the same name table already exists") // ErrCellStyles defined the error message on cell styles exceeds the limit. diff --git a/sheet.go b/sheet.go index a6e3d03..92aa146 100644 --- a/sheet.go +++ b/sheet.go @@ -1551,6 +1551,9 @@ func (f *File) SetDefinedName(definedName *DefinedName) error { if definedName.Name == "" || definedName.RefersTo == "" { return ErrParameterInvalid } + if err := checkDefinedName(definedName.Name); err != nil { + return err + } wb, err := f.workbookReader() if err != nil { return err diff --git a/stream_test.go b/stream_test.go index 720f598..d5f3ed2 100644 --- a/stream_test.go +++ b/stream_test.go @@ -223,7 +223,7 @@ func TestStreamTable(t *testing.T) { assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A:B1"}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error()) assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A1:B"}), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error()) // Test add table with invalid table name - assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A:B1", Name: "1Table"}), newInvalidTableNameError("1Table").Error()) + assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A:B1", Name: "1Table"}), newInvalidNameError("1Table").Error()) // Test add table with unsupported charset content types file.ContentTypes = nil file.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) diff --git a/table.go b/table.go index 3869427..9cadd8c 100644 --- a/table.go +++ b/table.go @@ -33,7 +33,7 @@ func parseTableOptions(opts *Table) (*Table, error) { if opts.ShowRowStripes == nil { opts.ShowRowStripes = boolPtr(true) } - if err = checkTableName(opts.Name); err != nil { + if err = checkDefinedName(opts.Name); err != nil { return opts, err } return opts, err @@ -182,13 +182,13 @@ func (f *File) setTableHeader(sheet string, showHeaderRow bool, x1, y1, x2 int) return tableColumns, nil } -// checkSheetName check whether there are illegal characters in the table name. -// Verify that the name: +// checkDefinedName check whether there are illegal characters in the defined +// name or table name. Verify that the name: // 1. Starts with a letter or underscore (_) // 2. Doesn't include a space or character that isn't allowed -func checkTableName(name string) error { +func checkDefinedName(name string) error { if utf8.RuneCountInString(name) > MaxFieldLength { - return ErrTableNameLength + return ErrNameLength } for i, c := range name { if string(c) == "_" { @@ -200,7 +200,7 @@ func checkTableName(name string) error { if i > 0 && unicode.IsDigit(c) { continue } - return newInvalidTableNameError(name) + return newInvalidNameError(name) } return nil } diff --git a/table_test.go b/table_test.go index 6eec139..e6a67fb 100644 --- a/table_test.go +++ b/table_test.go @@ -46,24 +46,27 @@ func TestAddTable(t *testing.T) { f = NewFile() assert.EqualError(t, f.addTable("sheet1", "", 0, 0, 0, 0, 0, nil), "invalid cell reference [0, 0]") assert.EqualError(t, f.addTable("sheet1", "", 1, 1, 0, 0, 0, nil), "invalid cell reference [0, 0]") - // Test add table with invalid table name + // Test set defined name and add table with invalid name for _, cases := range []struct { name string err error }{ - {name: "1Table", err: newInvalidTableNameError("1Table")}, - {name: "-Table", err: newInvalidTableNameError("-Table")}, - {name: "'Table", err: newInvalidTableNameError("'Table")}, - {name: "Table 1", err: newInvalidTableNameError("Table 1")}, - {name: "A&B", err: newInvalidTableNameError("A&B")}, - {name: "_1Table'", err: newInvalidTableNameError("_1Table'")}, - {name: "\u0f5f\u0fb3\u0f0b\u0f21", err: newInvalidTableNameError("\u0f5f\u0fb3\u0f0b\u0f21")}, - {name: strings.Repeat("c", MaxFieldLength+1), err: ErrTableNameLength}, + {name: "1Table", err: newInvalidNameError("1Table")}, + {name: "-Table", err: newInvalidNameError("-Table")}, + {name: "'Table", err: newInvalidNameError("'Table")}, + {name: "Table 1", err: newInvalidNameError("Table 1")}, + {name: "A&B", err: newInvalidNameError("A&B")}, + {name: "_1Table'", err: newInvalidNameError("_1Table'")}, + {name: "\u0f5f\u0fb3\u0f0b\u0f21", err: newInvalidNameError("\u0f5f\u0fb3\u0f0b\u0f21")}, + {name: strings.Repeat("c", MaxFieldLength+1), err: ErrNameLength}, } { assert.EqualError(t, f.AddTable("Sheet1", &Table{ Range: "A1:B2", Name: cases.name, }), cases.err.Error()) + assert.EqualError(t, f.SetDefinedName(&DefinedName{ + Name: cases.name, RefersTo: "Sheet1!$A$2:$D$5", + }), cases.err.Error()) } // Test check duplicate table name with unsupported charset table parts f = NewFile() diff --git a/vmlDrawing.go b/vmlDrawing.go index be1212e..1a49d72 100644 --- a/vmlDrawing.go +++ b/vmlDrawing.go @@ -117,8 +117,8 @@ type xlsxDiv struct { // element. type xClientData struct { ObjectType string `xml:"ObjectType,attr"` - MoveWithCells string `xml:"x:MoveWithCells,omitempty"` - SizeWithCells string `xml:"x:SizeWithCells,omitempty"` + MoveWithCells string `xml:"x:MoveWithCells"` + SizeWithCells string `xml:"x:SizeWithCells"` Anchor string `xml:"x:Anchor"` AutoFill string `xml:"x:AutoFill"` Row int `xml:"x:Row"`