forked from p30928647/excelize
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:
parent
17c029494a
commit
d0ad0f39ec
4
cell.go
4
cell.go
|
@ -462,9 +462,9 @@ func trimCellValue(value string, escape bool) (v string, ns xml.Attr) {
|
||||||
if utf8.RuneCountInString(value) > TotalCellChars {
|
if utf8.RuneCountInString(value) > TotalCellChars {
|
||||||
value = string([]rune(value)[:TotalCellChars])
|
value = string([]rune(value)[:TotalCellChars])
|
||||||
}
|
}
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
if escape {
|
if escape {
|
||||||
_ = xml.EscapeText(buf, []byte(value))
|
var buf bytes.Buffer
|
||||||
|
_ = xml.EscapeText(&buf, []byte(value))
|
||||||
value = buf.String()
|
value = buf.String()
|
||||||
}
|
}
|
||||||
if len(value) > 0 {
|
if len(value) > 0 {
|
||||||
|
|
|
@ -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 {
|
if err = f.addDrawingVML(commentID, drawingVML, comment.Cell, rows+1, cols); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,6 +239,8 @@ var (
|
||||||
// ErrTableNameLength defined the error message on receiving the table name
|
// ErrTableNameLength defined the error message on receiving the table name
|
||||||
// length exceeds the limit.
|
// length exceeds the limit.
|
||||||
ErrTableNameLength = fmt.Errorf("the table name length exceeds the %d characters limit", MaxFieldLength)
|
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 defined the error message on cell styles exceeds the limit.
|
||||||
ErrCellStyles = fmt.Errorf("the cell styles exceeds the %d limit", MaxCellStyles)
|
ErrCellStyles = fmt.Errorf("the cell styles exceeds the %d limit", MaxCellStyles)
|
||||||
// ErrUnprotectWorkbook defined the error message on workbook has set no
|
// ErrUnprotectWorkbook defined the error message on workbook has set no
|
||||||
|
|
|
@ -773,7 +773,7 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style))
|
assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style))
|
||||||
cellValue, err := f.GetCellValue("Sheet2", c)
|
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)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
table.go
19
table.go
|
@ -12,8 +12,10 @@
|
||||||
package excelize
|
package excelize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -75,6 +77,23 @@ func (f *File) AddTable(sheet string, table *Table) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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.
|
// Coordinate conversion, convert C1:B3 to 2,0,1,2.
|
||||||
coordinates, err := rangeRefToCoordinates(options.Range)
|
coordinates, err := rangeRefToCoordinates(options.Range)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -28,6 +28,8 @@ func TestAddTable(t *testing.T) {
|
||||||
}))
|
}))
|
||||||
assert.NoError(t, f.AddTable("Sheet2", &Table{Range: "F1:F1", StyleName: "TableStyleMedium8"}))
|
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
|
// Test add table with invalid table options
|
||||||
assert.Equal(t, f.AddTable("Sheet1", nil), ErrParameterInvalid)
|
assert.Equal(t, f.AddTable("Sheet1", nil), ErrParameterInvalid)
|
||||||
// Test add table in not exist worksheet
|
// Test add table in not exist worksheet
|
||||||
|
@ -63,6 +65,10 @@ func TestAddTable(t *testing.T) {
|
||||||
Name: cases.name,
|
Name: cases.name,
|
||||||
}), cases.err.Error())
|
}), 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) {
|
func TestSetTableHeader(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue