From d0ad0f39ec04debb4e082fb74f361b61ada5a184 Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 17 Apr 2023 08:48:30 +0800 Subject: [PATCH] This commit contains 5 changes: - Fix incorrect comment box size for multi-line plain text comments - Prevent create duplicate tables with the same name - Add new exported error variable `ErrExistsTableName` - Allocate buffer inside escape XML characters - Update the unit tests --- cell.go | 4 ++-- comment.go | 3 +++ errors.go | 2 ++ excelize_test.go | 2 +- table.go | 19 +++++++++++++++++++ table_test.go | 6 ++++++ 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cell.go b/cell.go index edfcb3c..da022cd 100644 --- a/cell.go +++ b/cell.go @@ -462,9 +462,9 @@ func trimCellValue(value string, escape bool) (v string, ns xml.Attr) { if utf8.RuneCountInString(value) > TotalCellChars { value = string([]rune(value)[:TotalCellChars]) } - buf := &bytes.Buffer{} if escape { - _ = xml.EscapeText(buf, []byte(value)) + var buf bytes.Buffer + _ = xml.EscapeText(&buf, []byte(value)) value = buf.String() } if len(value) > 0 { diff --git a/comment.go b/comment.go index 39f1176..25564cb 100644 --- a/comment.go +++ b/comment.go @@ -126,6 +126,9 @@ func (f *File) AddComment(sheet string, comment Comment) error { } } } + if len(comment.Runs) == 0 { + rows, cols = 1, len(comment.Text) + } if err = f.addDrawingVML(commentID, drawingVML, comment.Cell, rows+1, cols); err != nil { return err } diff --git a/errors.go b/errors.go index 8da9dae..7c7143c 100644 --- a/errors.go +++ b/errors.go @@ -239,6 +239,8 @@ var ( // 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) + // 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. ErrCellStyles = fmt.Errorf("the cell styles exceeds the %d limit", MaxCellStyles) // ErrUnprotectWorkbook defined the error message on workbook has set no diff --git a/excelize_test.go b/excelize_test.go index f7afccc..17d16f0 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -773,7 +773,7 @@ func TestSetCellStyleNumberFormat(t *testing.T) { } assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style)) cellValue, err := f.GetCellValue("Sheet2", c) - assert.Equal(t, expected[i][k], cellValue, "Sheet2!"+c, i, k) + assert.Equal(t, expected[i][k], cellValue, fmt.Sprintf("Sheet2!%s value: %s, number format: %d", c, value[i], k)) assert.NoError(t, err) } } diff --git a/table.go b/table.go index a914e15..3869427 100644 --- a/table.go +++ b/table.go @@ -12,8 +12,10 @@ package excelize import ( + "bytes" "encoding/xml" "fmt" + "io" "regexp" "strconv" "strings" @@ -75,6 +77,23 @@ func (f *File) AddTable(sheet string, table *Table) error { if err != nil { return err } + var exist bool + f.Pkg.Range(func(k, v interface{}) bool { + if strings.Contains(k.(string), "xl/tables/table") { + var t xlsxTable + if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(v.([]byte)))). + Decode(&t); err != nil && err != io.EOF { + return true + } + if exist = t.Name == options.Name; exist { + return false + } + } + return true + }) + if exist { + return ErrExistsTableName + } // Coordinate conversion, convert C1:B3 to 2,0,1,2. coordinates, err := rangeRefToCoordinates(options.Range) if err != nil { diff --git a/table_test.go b/table_test.go index 046a933..6eec139 100644 --- a/table_test.go +++ b/table_test.go @@ -28,6 +28,8 @@ func TestAddTable(t *testing.T) { })) assert.NoError(t, f.AddTable("Sheet2", &Table{Range: "F1:F1", StyleName: "TableStyleMedium8"})) + // Test add table with already exist table name + assert.Equal(t, f.AddTable("Sheet2", &Table{Name: "Table1"}), ErrExistsTableName) // Test add table with invalid table options assert.Equal(t, f.AddTable("Sheet1", nil), ErrParameterInvalid) // Test add table in not exist worksheet @@ -63,6 +65,10 @@ func TestAddTable(t *testing.T) { Name: cases.name, }), cases.err.Error()) } + // Test check duplicate table name with unsupported charset table parts + f = NewFile() + f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset) + assert.NoError(t, f.AddTable("Sheet1", &Table{Range: "A1:B2"})) } func TestSetTableHeader(t *testing.T) {