Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
324f87bc
excelize
项目概览
xuri
/
excelize
通知
13
Star
2
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
excelize
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
已验证
提交
324f87bc
编写于
9月 18, 2020
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add checking and limits for the worksheet
上级
96917e46
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
55 addition
and
32 deletion
+55
-32
col.go
col.go
+3
-0
col_test.go
col_test.go
+2
-0
file.go
file.go
+1
-1
rows.go
rows.go
+3
-1
rows_test.go
rows_test.go
+13
-15
styles.go
styles.go
+23
-14
styles_test.go
styles_test.go
+5
-0
xmlDrawing.go
xmlDrawing.go
+5
-1
未找到文件。
col.go
浏览文件 @
324f87bc
...
@@ -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
}
}
...
...
col_test.go
浏览文件 @
324f87bc
...
@@ -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"
)
...
...
file.go
浏览文件 @
324f87bc
...
@@ -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
)
>
Max
FileNameLength
{
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
)
...
...
rows.go
浏览文件 @
324f87bc
...
@@ -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
...
...
rows_test.go
浏览文件 @
324f87bc
...
@@ -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
()
}
}
...
...
styles.go
浏览文件 @
324f87bc
...
@@ -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
...
...
styles_test.go
浏览文件 @
324f87bc
...
@@ -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
)
{
...
...
xmlDrawing.go
浏览文件 @
324f87bc
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录