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
This commit is contained in:
xuri 2023-04-17 08:48:30 +08:00
parent 17c029494a
commit d0ad0f39ec
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
6 changed files with 33 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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