已验证 提交 324f87bc 编写于 作者: xurime's avatar xurime

add checking and limits for the worksheet

上级 96917e46
...@@ -444,6 +444,9 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error ...@@ -444,6 +444,9 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error
if err != nil { if err != nil {
return err return err
} }
if width > MaxColumnWidth {
return errors.New("the width of the column must be smaller than or equal to 255 characters")
}
if min > max { if min > max {
min, max = max, min min, max = max, min
} }
......
...@@ -236,6 +236,8 @@ func TestOutlineLevel(t *testing.T) { ...@@ -236,6 +236,8 @@ func TestOutlineLevel(t *testing.T) {
assert.EqualError(t, err, "sheet Shee2 is not exist") assert.EqualError(t, err, "sheet Shee2 is not exist")
assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13)) assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
assert.EqualError(t, f.SetColWidth("Sheet2", "A", "D", MaxColumnWidth+1), "the width of the column must be smaller than or equal to 255 characters")
assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2)) assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7)) assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level") assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
......
...@@ -65,7 +65,7 @@ func (f *File) Save() error { ...@@ -65,7 +65,7 @@ func (f *File) Save() error {
// SaveAs provides a function to create or update to an xlsx file at the // SaveAs provides a function to create or update to an xlsx file at the
// provided path. // provided path.
func (f *File) SaveAs(name string, opt ...Options) error { func (f *File) SaveAs(name string, opt ...Options) error {
if len(name) > FileNameLength { if len(name) > MaxFileNameLength {
return errors.New("file name length exceeds maximum limit") return errors.New("file name length exceeds maximum limit")
} }
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666) file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
......
...@@ -225,7 +225,9 @@ func (f *File) SetRowHeight(sheet string, row int, height float64) error { ...@@ -225,7 +225,9 @@ func (f *File) SetRowHeight(sheet string, row int, height float64) error {
if row < 1 { if row < 1 {
return newInvalidRowNumberError(row) return newInvalidRowNumberError(row)
} }
if height > MaxRowHeight {
return errors.New("the height of the row must be smaller than or equal to 409 points")
}
xlsx, err := f.workSheetReader(sheet) xlsx, err := f.workSheetReader(sheet)
if err != nil { if err != nil {
return err return err
......
...@@ -91,40 +91,38 @@ func TestRowsError(t *testing.T) { ...@@ -91,40 +91,38 @@ func TestRowsError(t *testing.T) {
} }
func TestRowHeight(t *testing.T) { func TestRowHeight(t *testing.T) {
xlsx := NewFile() f := NewFile()
sheet1 := xlsx.GetSheetName(0) sheet1 := f.GetSheetName(0)
assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0") assert.EqualError(t, f.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
_, err := xlsx.GetRowHeight("Sheet1", 0) _, err := f.GetRowHeight("Sheet1", 0)
assert.EqualError(t, err, "invalid row number 0") assert.EqualError(t, err, "invalid row number 0")
assert.NoError(t, xlsx.SetRowHeight(sheet1, 1, 111.0)) assert.NoError(t, f.SetRowHeight(sheet1, 1, 111.0))
height, err := xlsx.GetRowHeight(sheet1, 1) height, err := f.GetRowHeight(sheet1, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 111.0, height) assert.Equal(t, 111.0, height)
assert.NoError(t, xlsx.SetRowHeight(sheet1, 4, 444.0)) // Test set row height overflow max row height limit.
height, err = xlsx.GetRowHeight(sheet1, 4) assert.EqualError(t, f.SetRowHeight(sheet1, 4, MaxRowHeight+1), "the height of the row must be smaller than or equal to 409 points")
assert.NoError(t, err)
assert.Equal(t, 444.0, height)
// Test get row height that rows index over exists rows. // Test get row height that rows index over exists rows.
height, err = xlsx.GetRowHeight(sheet1, 5) height, err = f.GetRowHeight(sheet1, 5)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, defaultRowHeight, height) assert.Equal(t, defaultRowHeight, height)
// Test get row height that rows heights haven't changed. // Test get row height that rows heights haven't changed.
height, err = xlsx.GetRowHeight(sheet1, 3) height, err = f.GetRowHeight(sheet1, 3)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, defaultRowHeight, height) assert.Equal(t, defaultRowHeight, height)
// Test set and get row height on not exists worksheet. // Test set and get row height on not exists worksheet.
assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist") assert.EqualError(t, f.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")
_, err = xlsx.GetRowHeight("SheetN", 3) _, err = f.GetRowHeight("SheetN", 3)
assert.EqualError(t, err, "sheet SheetN is not exist") assert.EqualError(t, err, "sheet SheetN is not exist")
err = xlsx.SaveAs(filepath.Join("test", "TestRowHeight.xlsx")) err = f.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
......
...@@ -1037,10 +1037,26 @@ func (f *File) sharedStringsWriter() { ...@@ -1037,10 +1037,26 @@ func (f *File) sharedStringsWriter() {
// parseFormatStyleSet provides a function to parse the format settings of the // parseFormatStyleSet provides a function to parse the format settings of the
// cells and conditional formats. // cells and conditional formats.
func parseFormatStyleSet(style string) (*Style, error) { func parseFormatStyleSet(style interface{}) (*Style, error) {
format := Style{} fs := Style{}
err := json.Unmarshal([]byte(style), &format) var err error
return &format, err switch v := style.(type) {
case string:
err = json.Unmarshal([]byte(v), &fs)
case *Style:
fs = *v
default:
err = errors.New("invalid parameter type")
}
if fs.Font != nil {
if len(fs.Font.Family) > MaxFontFamilyLength {
return &fs, errors.New("the length of the font family name must be smaller than or equal to 31")
}
if fs.Font.Size > MaxFontSize {
return &fs, errors.New("font size must be between 1 and 409 points")
}
}
return &fs, err
} }
// NewStyle provides a function to create the style for cells by given JSON or // NewStyle provides a function to create the style for cells by given JSON or
...@@ -1909,16 +1925,9 @@ func (f *File) NewStyle(style interface{}) (int, error) { ...@@ -1909,16 +1925,9 @@ func (f *File) NewStyle(style interface{}) (int, error) {
var fs *Style var fs *Style
var err error var err error
var cellXfsID, fontID, borderID, fillID int var cellXfsID, fontID, borderID, fillID int
switch v := style.(type) { fs, err = parseFormatStyleSet(style)
case string: if err != nil {
fs, err = parseFormatStyleSet(v) return cellXfsID, err
if err != nil {
return cellXfsID, err
}
case *Style:
fs = v
default:
return cellXfsID, errors.New("invalid parameter type")
} }
if fs.DecimalPlaces == 0 { if fs.DecimalPlaces == 0 {
fs.DecimalPlaces = 2 fs.DecimalPlaces = 2
......
...@@ -3,6 +3,7 @@ package excelize ...@@ -3,6 +3,7 @@ package excelize
import ( import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
...@@ -200,6 +201,10 @@ func TestNewStyle(t *testing.T) { ...@@ -200,6 +201,10 @@ func TestNewStyle(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
_, err = f.NewStyle(Style{}) _, err = f.NewStyle(Style{})
assert.EqualError(t, err, "invalid parameter type") assert.EqualError(t, err, "invalid parameter type")
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
assert.EqualError(t, err, "the length of the font family name must be smaller than or equal to 31")
_, err = f.NewStyle(&Style{Font: &Font{Size: MaxFontSize + 1}})
assert.EqualError(t, err, "font size must be between 1 and 409 points")
} }
func TestGetDefaultFont(t *testing.T) { func TestGetDefaultFont(t *testing.T) {
......
...@@ -89,7 +89,11 @@ const ( ...@@ -89,7 +89,11 @@ const (
// Excel specifications and limits // Excel specifications and limits
const ( const (
FileNameLength = 207 MaxFontFamilyLength = 31
MaxFontSize = 409
MaxFileNameLength = 207
MaxColumnWidth = 255
MaxRowHeight = 409
TotalRows = 1048576 TotalRows = 1048576
TotalColumns = 16384 TotalColumns = 16384
TotalSheetHyperlinks = 65529 TotalSheetHyperlinks = 65529
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册