mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
spreadsheet: add validation for sheet name length
This commit is contained in:
parent
4d1c3ec657
commit
8957cf73e3
@ -105,6 +105,7 @@ func (s Sheet) SetName(name string) {
|
||||
// Validate validates the sheet, returning an error if it is found to be invalid.
|
||||
func (s Sheet) Validate() error {
|
||||
|
||||
// check for re-used row numbers
|
||||
usedRows := map[uint32]struct{}{}
|
||||
for _, r := range s.x.SheetData.Row {
|
||||
if r.RAttr != nil {
|
||||
@ -113,6 +114,7 @@ func (s Sheet) Validate() error {
|
||||
}
|
||||
usedRows[*r.RAttr] = struct{}{}
|
||||
}
|
||||
// or re-used column labels within a row
|
||||
usedCells := map[string]struct{}{}
|
||||
for _, c := range r.C {
|
||||
if c.RAttr == nil {
|
||||
@ -125,6 +127,10 @@ func (s Sheet) Validate() error {
|
||||
usedCells[*c.RAttr] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if len(s.Name()) > 31 {
|
||||
return fmt.Errorf("sheet name '%s' has %d characters, max length is 31", s.Name(), len(s.Name()))
|
||||
}
|
||||
if err := s.cts.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -119,5 +119,16 @@ func TestAutoFilter(t *testing.T) {
|
||||
if sheet.X().AutoFilter != nil {
|
||||
t.Errorf("autofilter should have been nil after clear")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSheetNameLength(t *testing.T) {
|
||||
wb := spreadsheet.New()
|
||||
sheet := wb.AddSheet()
|
||||
if err := sheet.Validate(); err != nil {
|
||||
t.Errorf("expected no validaton error on new sheet, got %s:", err)
|
||||
}
|
||||
sheet.SetName("01234567890123456789012345678901")
|
||||
if err := sheet.Validate(); err == nil {
|
||||
t.Errorf("expected validation error with sheet name too long")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user