Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
fb1aab7a
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 搜索 >>
已验证
提交
fb1aab7a
编写于
9月 08, 2022
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
This closes #744, the `Save`, `Write` and `WriteTo` function accept saving options
上级
0c5cdfec
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
44 addition
and
30 deletion
+44
-30
crypt_test.go
crypt_test.go
+2
-0
excelize.go
excelize.go
+2
-2
excelize_test.go
excelize_test.go
+3
-6
file.go
file.go
+20
-22
file_test.go
file_test.go
+8
-0
xmlDrawing.go
xmlDrawing.go
+9
-0
未找到文件。
crypt_test.go
浏览文件 @
fb1aab7a
...
...
@@ -48,6 +48,8 @@ func TestEncrypt(t *testing.T) {
cell
,
err
=
f
.
GetCellValue
(
"Sheet1"
,
"A1"
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
"SECRET"
,
cell
)
// Test remove password by save workbook with options
assert
.
NoError
(
t
,
f
.
Save
(
Options
{
Password
:
""
}))
assert
.
NoError
(
t
,
f
.
Close
())
}
...
...
excelize.go
浏览文件 @
fb1aab7a
...
...
@@ -136,13 +136,13 @@ func newFile() *File {
// OpenReader read data stream from io.Reader and return a populated
// spreadsheet file.
func
OpenReader
(
r
io
.
Reader
,
opt
...
Options
)
(
*
File
,
error
)
{
func
OpenReader
(
r
io
.
Reader
,
opt
s
...
Options
)
(
*
File
,
error
)
{
b
,
err
:=
ioutil
.
ReadAll
(
r
)
if
err
!=
nil
{
return
nil
,
err
}
f
:=
newFile
()
f
.
options
=
parseOptions
(
opt
...
)
f
.
options
=
parseOptions
(
opt
s
...
)
if
f
.
options
.
UnzipSizeLimit
==
0
{
f
.
options
.
UnzipSizeLimit
=
UnzipSizeLimit
if
f
.
options
.
UnzipXMLSizeLimit
>
f
.
options
.
UnzipSizeLimit
{
...
...
excelize_test.go
浏览文件 @
fb1aab7a
...
...
@@ -186,18 +186,15 @@ func TestOpenFile(t *testing.T) {
func
TestSaveFile
(
t
*
testing
.
T
)
{
f
,
err
:=
OpenFile
(
filepath
.
Join
(
"test"
,
"Book1.xlsx"
))
if
!
assert
.
NoError
(
t
,
err
)
{
t
.
FailNow
()
}
assert
.
NoError
(
t
,
err
)
assert
.
EqualError
(
t
,
f
.
SaveAs
(
filepath
.
Join
(
"test"
,
"TestSaveFile.xlsb"
)),
ErrWorkbookFileFormat
.
Error
())
for
_
,
ext
:=
range
[]
string
{
".xlam"
,
".xlsm"
,
".xlsx"
,
".xltm"
,
".xltx"
}
{
assert
.
NoError
(
t
,
f
.
SaveAs
(
filepath
.
Join
(
"test"
,
fmt
.
Sprintf
(
"TestSaveFile%s"
,
ext
))))
}
assert
.
NoError
(
t
,
f
.
Close
())
f
,
err
=
OpenFile
(
filepath
.
Join
(
"test"
,
"TestSaveFile.xlsx"
))
if
!
assert
.
NoError
(
t
,
err
)
{
t
.
FailNow
()
}
assert
.
NoError
(
t
,
err
)
assert
.
NoError
(
t
,
f
.
Save
())
assert
.
NoError
(
t
,
f
.
Close
())
}
...
...
file.go
浏览文件 @
fb1aab7a
...
...
@@ -55,44 +55,32 @@ func NewFile() *File {
}
// Save provides a function to override the spreadsheet with origin path.
func
(
f
*
File
)
Save
()
error
{
func
(
f
*
File
)
Save
(
opts
...
Options
)
error
{
if
f
.
Path
==
""
{
return
ErrSave
}
if
f
.
options
!=
nil
{
return
f
.
SaveAs
(
f
.
Path
,
*
f
.
options
)
for
i
:=
range
opts
{
f
.
options
=
&
opts
[
i
]
}
return
f
.
SaveAs
(
f
.
Path
)
return
f
.
SaveAs
(
f
.
Path
,
*
f
.
options
)
}
// SaveAs provides a function to create or update to a spreadsheet at the
// provided path.
func
(
f
*
File
)
SaveAs
(
name
string
,
opt
...
Options
)
error
{
func
(
f
*
File
)
SaveAs
(
name
string
,
opt
s
...
Options
)
error
{
if
len
(
name
)
>
MaxFilePathLength
{
return
ErrMaxFilePathLength
}
f
.
Path
=
name
contentType
,
ok
:=
map
[
string
]
string
{
".xlam"
:
ContentTypeAddinMacro
,
".xlsm"
:
ContentTypeMacro
,
".xlsx"
:
ContentTypeSheetML
,
".xltm"
:
ContentTypeTemplateMacro
,
".xltx"
:
ContentTypeTemplate
,
}[
filepath
.
Ext
(
f
.
Path
)]
if
!
ok
{
if
_
,
ok
:=
supportedContentType
[
filepath
.
Ext
(
f
.
Path
)];
!
ok
{
return
ErrWorkbookFileFormat
}
f
.
setContentTypePartProjectExtensions
(
contentType
)
file
,
err
:=
os
.
OpenFile
(
filepath
.
Clean
(
name
),
os
.
O_WRONLY
|
os
.
O_TRUNC
|
os
.
O_CREATE
,
os
.
ModePerm
)
if
err
!=
nil
{
return
err
}
defer
file
.
Close
()
f
.
options
=
nil
for
i
:=
range
opt
{
f
.
options
=
&
opt
[
i
]
}
return
f
.
Write
(
file
)
return
f
.
Write
(
file
,
opts
...
)
}
// Close closes and cleanup the open temporary file for the spreadsheet.
...
...
@@ -113,13 +101,23 @@ func (f *File) Close() error {
}
// Write provides a function to write to an io.Writer.
func
(
f
*
File
)
Write
(
w
io
.
Writer
)
error
{
_
,
err
:=
f
.
WriteTo
(
w
)
func
(
f
*
File
)
Write
(
w
io
.
Writer
,
opts
...
Options
)
error
{
_
,
err
:=
f
.
WriteTo
(
w
,
opts
...
)
return
err
}
// WriteTo implements io.WriterTo to write the file.
func
(
f
*
File
)
WriteTo
(
w
io
.
Writer
)
(
int64
,
error
)
{
func
(
f
*
File
)
WriteTo
(
w
io
.
Writer
,
opts
...
Options
)
(
int64
,
error
)
{
for
i
:=
range
opts
{
f
.
options
=
&
opts
[
i
]
}
if
len
(
f
.
Path
)
!=
0
{
contentType
,
ok
:=
supportedContentType
[
filepath
.
Ext
(
f
.
Path
)]
if
!
ok
{
return
0
,
ErrWorkbookFileFormat
}
f
.
setContentTypePartProjectExtensions
(
contentType
)
}
if
f
.
options
!=
nil
&&
f
.
options
.
Password
!=
""
{
buf
,
err
:=
f
.
WriteToBuffer
()
if
err
!=
nil
{
...
...
file_test.go
浏览文件 @
fb1aab7a
...
...
@@ -71,6 +71,14 @@ func TestWriteTo(t *testing.T) {
_
,
err
:=
f
.
WriteTo
(
bufio
.
NewWriter
(
&
buf
))
assert
.
EqualError
(
t
,
err
,
"zip: FileHeader.Name too long"
)
}
// Test write with unsupported workbook file format
{
f
,
buf
:=
File
{
Pkg
:
sync
.
Map
{}},
bytes
.
Buffer
{}
f
.
Pkg
.
Store
(
"/d"
,
[]
byte
(
"s"
))
f
.
Path
=
"Book1.xls"
_
,
err
:=
f
.
WriteTo
(
bufio
.
NewWriter
(
&
buf
))
assert
.
EqualError
(
t
,
err
,
ErrWorkbookFileFormat
.
Error
())
}
}
func
TestClose
(
t
*
testing
.
T
)
{
...
...
xmlDrawing.go
浏览文件 @
fb1aab7a
...
...
@@ -144,6 +144,15 @@ const (
// supportedImageTypes defined supported image types.
var
supportedImageTypes
=
map
[
string
]
string
{
".gif"
:
".gif"
,
".jpg"
:
".jpeg"
,
".jpeg"
:
".jpeg"
,
".png"
:
".png"
,
".tif"
:
".tiff"
,
".tiff"
:
".tiff"
,
".emf"
:
".emf"
,
".wmf"
:
".wmf"
,
".emz"
:
".emz"
,
".wmz"
:
".wmz"
}
// supportedContentType defined supported file format types.
var
supportedContentType
=
map
[
string
]
string
{
".xlam"
:
ContentTypeAddinMacro
,
".xlsm"
:
ContentTypeMacro
,
".xlsx"
:
ContentTypeSheetML
,
".xltm"
:
ContentTypeTemplateMacro
,
".xltx"
:
ContentTypeTemplate
,
}
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
// element specifies non-visual canvas properties. This allows for additional
// information that does not affect the appearance of the picture to be stored.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录