Skip to content

Commit

Permalink
This closes qax-os#1061, support multi-byte language on set header fo…
Browse files Browse the repository at this point in the history
…oter

typo fixed and simplify code for read the data values arguments of formula functions
  • Loading branch information
xuri committed Nov 15, 2021
1 parent 6935c83 commit aa3f91c
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 244 deletions.
336 changes: 111 additions & 225 deletions calc.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
a := formatSet.Author
t := formatSet.Text
if len(a) > 255 {
a = a[0:255]
if len(a) > MaxFieldLength {
a = a[:MaxFieldLength]
}
if len(t) > 32512 {
t = t[0:32512]
t = t[:32512]
}
comments := f.commentsReader(commentsXML)
authorID := 0
Expand Down
7 changes: 1 addition & 6 deletions datavalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ const (
DataValidationTypeWhole
)

const (
// dataValidationFormulaStrLen 255 characters
dataValidationFormulaStrLen = 255
)

// DataValidationErrorStyle defined the style of data validation error alert.
type DataValidationErrorStyle int

Expand Down Expand Up @@ -120,7 +115,7 @@ func (dd *DataValidation) SetInput(title, msg string) {
// SetDropList data validation list.
func (dd *DataValidation) SetDropList(keys []string) error {
formula := strings.Join(keys, ",")
if dataValidationFormulaStrLen < len(utf16.Encode([]rune(formula))) {
if MaxFieldLength < len(utf16.Encode([]rune(formula))) {
return ErrDataValidationFormulaLenth
}
dd.Formula1 = fmt.Sprintf(`<formula1>"%s"</formula1>`, formulaEscaper.Replace(formula))
Expand Down
4 changes: 2 additions & 2 deletions datavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func TestDataValidation(t *testing.T) {
dvRange.Sqref = "A5:B6"
for _, listValid := range [][]string{
{"1", "2", "3"},
{strings.Repeat("&", 255)},
{strings.Repeat("\u4E00", 255)},
{strings.Repeat("&", MaxFieldLength)},
{strings.Repeat("\u4E00", MaxFieldLength)},
{strings.Repeat("\U0001F600", 100), strings.Repeat("\u4E01", 50), "<&>"},
{`A<`, `B>`, `C"`, "D\t", `E'`, `F`},
} {
Expand Down
5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func newInvalidStyleID(styleID int) error {
return fmt.Errorf("invalid style ID %d, negative values are not supported", styleID)
}

// newFieldLengthError defined the error message on receiving the field length overflow.
func newFieldLengthError(name string) error {
return fmt.Errorf("field %s must be less or equal than 255 characters", name)
}

var (
// ErrStreamSetColWidth defined the error message on set column width in
// stream writing mode.
Expand Down
4 changes: 2 additions & 2 deletions pivotTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ func (f *File) getPivotTableFieldsSubtotal(fields []PivotTableField) []string {
func (f *File) getPivotTableFieldsName(fields []PivotTableField) []string {
field := make([]string, len(fields))
for idx, fld := range fields {
if len(fld.Name) > 255 {
field[idx] = fld.Name[0:255]
if len(fld.Name) > MaxFieldLength {
field[idx] = fld.Name[:MaxFieldLength]
continue
}
field[idx] = fld.Name
Expand Down
5 changes: 3 additions & 2 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sort"
"strconv"
"strings"
"unicode/utf16"
"unicode/utf8"

"github.com/mohae/deepcopy"
Expand Down Expand Up @@ -1092,8 +1093,8 @@ func (f *File) SetHeaderFooter(sheet string, settings *FormatHeaderFooter) error
// Check 6 string type fields: OddHeader, OddFooter, EvenHeader, EvenFooter,
// FirstFooter, FirstHeader
for i := 4; i < v.NumField()-1; i++ {
if v.Field(i).Len() >= 255 {
return fmt.Errorf("field %s must be less than 255 characters", v.Type().Field(i).Name)
if len(utf16.Encode([]rune(v.Field(i).String()))) > MaxFieldLength {
return newFieldLengthError(v.Type().Field(i).Name)
}
}
ws.HeaderFooter = &xlsxHeaderFooter{
Expand Down
12 changes: 10 additions & 2 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,18 @@ func TestSetHeaderFooter(t *testing.T) {
assert.EqualError(t, f.SetHeaderFooter("SheetN", nil), "sheet SheetN is not exist")
// Test set header and footer with illegal setting.
assert.EqualError(t, f.SetHeaderFooter("Sheet1", &FormatHeaderFooter{
OddHeader: strings.Repeat("c", 256),
}), "field OddHeader must be less than 255 characters")
OddHeader: strings.Repeat("c", MaxFieldLength+1),
}), "field OddHeader must be less or equal than 255 characters")

assert.NoError(t, f.SetHeaderFooter("Sheet1", nil))
text := strings.Repeat("一", MaxFieldLength)
assert.NoError(t, f.SetHeaderFooter("Sheet1", &FormatHeaderFooter{
OddHeader: text,
OddFooter: text,
EvenHeader: text,
EvenFooter: text,
FirstHeader: text,
}))
assert.NoError(t, f.SetHeaderFooter("Sheet1", &FormatHeaderFooter{
DifferentFirst: true,
DifferentOddEven: true,
Expand Down
4 changes: 2 additions & 2 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ func TestGetDefaultFont(t *testing.T) {

func TestSetDefaultFont(t *testing.T) {
f := NewFile()
f.SetDefaultFont("Ariel")
f.SetDefaultFont("Arial")
styles := f.stylesReader()
s := f.GetDefaultFont()
assert.Equal(t, s, "Ariel", "Default font should change to Ariel")
assert.Equal(t, s, "Arial", "Default font should change to Arial")
assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true)
}

Expand Down
1 change: 1 addition & 0 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const (
MaxFontFamilyLength = 31
MaxFontSize = 409
MaxFileNameLength = 207
MaxFieldLength = 255
MaxColumnWidth = 255
MaxRowHeight = 409
TotalRows = 1048576
Expand Down

0 comments on commit aa3f91c

Please sign in to comment.