@@ -17,18 +17,24 @@ import (
1717 "regexp"
1818 "strconv"
1919 "strings"
20+ "unicode"
21+ "unicode/utf8"
2022)
2123
2224// parseTableOptions provides a function to parse the format settings of the
2325// table with default value.
24- func parseTableOptions (opts * TableOptions ) * TableOptions {
26+ func parseTableOptions (opts * TableOptions ) (* TableOptions , error ) {
27+ var err error
2528 if opts == nil {
26- return & TableOptions {ShowRowStripes : boolPtr (true )}
29+ return & TableOptions {ShowRowStripes : boolPtr (true )}, err
2730 }
2831 if opts .ShowRowStripes == nil {
2932 opts .ShowRowStripes = boolPtr (true )
3033 }
31- return opts
34+ if err = checkTableName (opts .Name ); err != nil {
35+ return opts , err
36+ }
37+ return opts , err
3238}
3339
3440// AddTable provides the method to add table in a worksheet by given worksheet
@@ -54,15 +60,20 @@ func parseTableOptions(opts *TableOptions) *TableOptions {
5460// header row data of the table before calling the AddTable function. Multiple
5561// tables range reference that can't have an intersection.
5662//
57- // Name: The name of the table, in the same worksheet name of the table should be unique
63+ // Name: The name of the table, in the same worksheet name of the table should
64+ // be unique, starts with a letter or underscore (_), doesn't include a
65+ // space or character, and should be no more than 255 characters
5866//
5967// StyleName: The built-in table style names
6068//
6169// TableStyleLight1 - TableStyleLight21
6270// TableStyleMedium1 - TableStyleMedium28
6371// TableStyleDark1 - TableStyleDark11
6472func (f * File ) AddTable (sheet , rangeRef string , opts * TableOptions ) error {
65- options := parseTableOptions (opts )
73+ options , err := parseTableOptions (opts )
74+ if err != nil {
75+ return err
76+ }
6677 // Coordinate conversion, convert C1:B3 to 2,0,1,2.
6778 coordinates , err := rangeRefToCoordinates (rangeRef )
6879 if err != nil {
@@ -147,6 +158,29 @@ func (f *File) setTableHeader(sheet string, x1, y1, x2 int) ([]*xlsxTableColumn,
147158 return tableColumns , nil
148159}
149160
161+ // checkSheetName check whether there are illegal characters in the table name.
162+ // Verify that the name:
163+ // 1. Starts with a letter or underscore (_)
164+ // 2. Doesn't include a space or character that isn't allowed
165+ func checkTableName (name string ) error {
166+ if utf8 .RuneCountInString (name ) > MaxFieldLength {
167+ return ErrTableNameLength
168+ }
169+ for i , c := range name {
170+ if string (c ) == "_" {
171+ continue
172+ }
173+ if unicode .IsLetter (c ) {
174+ continue
175+ }
176+ if i > 0 && unicode .IsDigit (c ) {
177+ continue
178+ }
179+ return newInvalidTableNameError (name )
180+ }
181+ return nil
182+ }
183+
150184// addTable provides a function to add table by given worksheet name,
151185// range reference and format set.
152186func (f * File ) addTable (sheet , tableXML string , x1 , y1 , x2 , y2 , i int , opts * TableOptions ) error {
0 commit comments