forked from p30928647/excelize
Support checking cell value length with multi-bytes characters (#1517)
This commit is contained in:
parent
4196348f9f
commit
635ec33576
9
cell.go
9
cell.go
|
@ -20,6 +20,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CellType is the type of cell value type.
|
// CellType is the type of cell value type.
|
||||||
|
@ -397,8 +398,8 @@ func (f *File) SetCellStr(sheet, cell, value string) error {
|
||||||
// setCellString provides a function to set string type to shared string
|
// setCellString provides a function to set string type to shared string
|
||||||
// table.
|
// table.
|
||||||
func (f *File) setCellString(value string) (t, v string, err error) {
|
func (f *File) setCellString(value string) (t, v string, err error) {
|
||||||
if len(value) > TotalCellChars {
|
if utf8.RuneCountInString(value) > TotalCellChars {
|
||||||
value = value[:TotalCellChars]
|
value = string([]rune(value)[:TotalCellChars])
|
||||||
}
|
}
|
||||||
t = "s"
|
t = "s"
|
||||||
var si int
|
var si int
|
||||||
|
@ -458,8 +459,8 @@ func (f *File) setSharedString(val string) (int, error) {
|
||||||
|
|
||||||
// trimCellValue provides a function to set string type to cell.
|
// trimCellValue provides a function to set string type to cell.
|
||||||
func trimCellValue(value string) (v string, ns xml.Attr) {
|
func trimCellValue(value string) (v string, ns xml.Attr) {
|
||||||
if len(value) > TotalCellChars {
|
if utf8.RuneCountInString(value) > TotalCellChars {
|
||||||
value = value[:TotalCellChars]
|
value = string([]rune(value)[:TotalCellChars])
|
||||||
}
|
}
|
||||||
if len(value) > 0 {
|
if len(value) > 0 {
|
||||||
prefix, suffix := value[0], value[len(value)-1]
|
prefix, suffix := value[0], value[len(value)-1]
|
||||||
|
|
13
cell_test.go
13
cell_test.go
|
@ -176,6 +176,19 @@ func TestSetCellFloat(t *testing.T) {
|
||||||
assert.EqualError(t, f.SetCellFloat("Sheet:1", "A1", 123.42, -1, 64), ErrSheetNameInvalid.Error())
|
assert.EqualError(t, f.SetCellFloat("Sheet:1", "A1", 123.42, -1, 64), ErrSheetNameInvalid.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetCellValuesMultiByte(t *testing.T) {
|
||||||
|
value := strings.Repeat("\u042B", TotalCellChars+1)
|
||||||
|
|
||||||
|
f := NewFile()
|
||||||
|
err := f.SetCellValue("Sheet1", "A1", value)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
v, err := f.GetCellValue("Sheet1", "A1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEqual(t, value, v)
|
||||||
|
assert.Equal(t, TotalCellChars, len([]rune(v)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSetCellValue(t *testing.T) {
|
func TestSetCellValue(t *testing.T) {
|
||||||
f := NewFile()
|
f := NewFile()
|
||||||
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
||||||
|
|
Loading…
Reference in New Issue