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)
}
// 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.

View File

@ -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

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: "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)

View File

@ -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
}

View File

@ -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()

View File

@ -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"`