Breaking changes: rename exported variable `ErrTableNameLength` to `ErrNameLength`

- Check the defined name
- Improve the cell comment box shape size compatibility with KingSoft WPS
- Update unit test
This commit is contained in:
xuri 2023-04-21 08:51:04 +08:00
parent fb6ce60bd5
commit 63d8a09082
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
6 changed files with 31 additions and 25 deletions

View File

@ -40,10 +40,10 @@ func newInvalidExcelDateError(dateValue float64) error {
return fmt.Errorf("invalid date value %f, negative values are not supported", dateValue) return fmt.Errorf("invalid date value %f, negative values are not supported", dateValue)
} }
// newInvalidTableNameError defined the error message on receiving the invalid // newInvalidNameError defined the error message on receiving the invalid
// table name. // defined name or table name.
func newInvalidTableNameError(name string) error { func newInvalidNameError(name string) error {
return fmt.Errorf("invalid table name %q", name) 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 // newUnsupportedChartType defined the error message on receiving the chart
@ -236,9 +236,9 @@ var (
// ErrSheetNameLength defined the error message on receiving the sheet // ErrSheetNameLength defined the error message on receiving the sheet
// name length exceeds the limit. // name length exceeds the limit.
ErrSheetNameLength = fmt.Errorf("the sheet name length exceeds the %d characters limit", MaxSheetNameLength) ErrSheetNameLength = fmt.Errorf("the sheet name length exceeds the %d characters limit", MaxSheetNameLength)
// ErrTableNameLength defined the error message on receiving the table name // ErrNameLength defined the error message on receiving the defined name or
// length exceeds the limit. // table name length exceeds the limit.
ErrTableNameLength = fmt.Errorf("the table name length exceeds the %d characters limit", MaxFieldLength) ErrNameLength = fmt.Errorf("the name length exceeds the %d characters limit", MaxFieldLength)
// ErrExistsTableName defined the error message on given table already exists. // ErrExistsTableName defined the error message on given table already exists.
ErrExistsTableName = errors.New("the same name table already exists") ErrExistsTableName = errors.New("the same name table already exists")
// ErrCellStyles defined the error message on cell styles exceeds the limit. // ErrCellStyles defined the error message on cell styles exceeds the limit.

View File

@ -1551,6 +1551,9 @@ func (f *File) SetDefinedName(definedName *DefinedName) error {
if definedName.Name == "" || definedName.RefersTo == "" { if definedName.Name == "" || definedName.RefersTo == "" {
return ErrParameterInvalid return ErrParameterInvalid
} }
if err := checkDefinedName(definedName.Name); err != nil {
return err
}
wb, err := f.workbookReader() wb, err := f.workbookReader()
if err != nil { if err != nil {
return err return err

View File

@ -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: "A:B1"}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A1:B"}), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error()) assert.EqualError(t, streamWriter.AddTable(&Table{Range: "A1:B"}), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
// Test add table with invalid table name // 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 // Test add table with unsupported charset content types
file.ContentTypes = nil file.ContentTypes = nil
file.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) file.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)

View File

@ -33,7 +33,7 @@ func parseTableOptions(opts *Table) (*Table, error) {
if opts.ShowRowStripes == nil { if opts.ShowRowStripes == nil {
opts.ShowRowStripes = boolPtr(true) opts.ShowRowStripes = boolPtr(true)
} }
if err = checkTableName(opts.Name); err != nil { if err = checkDefinedName(opts.Name); err != nil {
return opts, err return opts, err
} }
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 return tableColumns, nil
} }
// checkSheetName check whether there are illegal characters in the table name. // checkDefinedName check whether there are illegal characters in the defined
// Verify that the name: // name or table name. Verify that the name:
// 1. Starts with a letter or underscore (_) // 1. Starts with a letter or underscore (_)
// 2. Doesn't include a space or character that isn't allowed // 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 { if utf8.RuneCountInString(name) > MaxFieldLength {
return ErrTableNameLength return ErrNameLength
} }
for i, c := range name { for i, c := range name {
if string(c) == "_" { if string(c) == "_" {
@ -200,7 +200,7 @@ func checkTableName(name string) error {
if i > 0 && unicode.IsDigit(c) { if i > 0 && unicode.IsDigit(c) {
continue continue
} }
return newInvalidTableNameError(name) return newInvalidNameError(name)
} }
return nil return nil
} }

View File

@ -46,24 +46,27 @@ func TestAddTable(t *testing.T) {
f = NewFile() 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", "", 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]") 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 { for _, cases := range []struct {
name string name string
err error err error
}{ }{
{name: "1Table", err: newInvalidTableNameError("1Table")}, {name: "1Table", err: newInvalidNameError("1Table")},
{name: "-Table", err: newInvalidTableNameError("-Table")}, {name: "-Table", err: newInvalidNameError("-Table")},
{name: "'Table", err: newInvalidTableNameError("'Table")}, {name: "'Table", err: newInvalidNameError("'Table")},
{name: "Table 1", err: newInvalidTableNameError("Table 1")}, {name: "Table 1", err: newInvalidNameError("Table 1")},
{name: "A&B", err: newInvalidTableNameError("A&B")}, {name: "A&B", err: newInvalidNameError("A&B")},
{name: "_1Table'", err: newInvalidTableNameError("_1Table'")}, {name: "_1Table'", err: newInvalidNameError("_1Table'")},
{name: "\u0f5f\u0fb3\u0f0b\u0f21", err: newInvalidTableNameError("\u0f5f\u0fb3\u0f0b\u0f21")}, {name: "\u0f5f\u0fb3\u0f0b\u0f21", err: newInvalidNameError("\u0f5f\u0fb3\u0f0b\u0f21")},
{name: strings.Repeat("c", MaxFieldLength+1), err: ErrTableNameLength}, {name: strings.Repeat("c", MaxFieldLength+1), err: ErrNameLength},
} { } {
assert.EqualError(t, f.AddTable("Sheet1", &Table{ assert.EqualError(t, f.AddTable("Sheet1", &Table{
Range: "A1:B2", Range: "A1:B2",
Name: cases.name, Name: cases.name,
}), cases.err.Error()) }), 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 // Test check duplicate table name with unsupported charset table parts
f = NewFile() f = NewFile()

View File

@ -117,8 +117,8 @@ type xlsxDiv struct {
// element. // element.
type xClientData struct { type xClientData struct {
ObjectType string `xml:"ObjectType,attr"` ObjectType string `xml:"ObjectType,attr"`
MoveWithCells string `xml:"x:MoveWithCells,omitempty"` MoveWithCells string `xml:"x:MoveWithCells"`
SizeWithCells string `xml:"x:SizeWithCells,omitempty"` SizeWithCells string `xml:"x:SizeWithCells"`
Anchor string `xml:"x:Anchor"` Anchor string `xml:"x:Anchor"`
AutoFill string `xml:"x:AutoFill"` AutoFill string `xml:"x:AutoFill"`
Row int `xml:"x:Row"` Row int `xml:"x:Row"`