Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
此号慢热型
excelize
提交
35e48575
excelize
项目概览
此号慢热型
/
excelize
与 Fork 源项目一致
Fork自
xuri / excelize
通知
2
Star
0
Fork
0
代码
文件
提交
分支
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,发现更多精彩内容 >>
已验证
提交
35e48575
编写于
7月 21, 2019
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Resolve #217, new function add VBA project supported.
上级
855c3605
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
89 addition
and
0 deletion
+89
-0
excelize.go
excelize.go
+77
-0
excelize_test.go
excelize_test.go
+11
-0
test/vbaProject.bin
test/vbaProject.bin
+0
-0
xmlDrawing.go
xmlDrawing.go
+1
-0
未找到文件。
excelize.go
浏览文件 @
35e48575
...
...
@@ -19,7 +19,9 @@ import (
"io"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
)
// File define a populated XLSX file struct.
...
...
@@ -226,3 +228,78 @@ func (f *File) UpdateLinkedValue() error {
}
return
nil
}
// AddVBAProject provides the method to add vbaProject.bin file which contains
// functions and/or macros. The file extension should be .xlsm. For example:
//
// err := f.SetSheetPrOptions("Sheet1", excelize.CodeName("Sheet1"))
// if err != nil {
// fmt.Println(err)
// }
// err = f.AddVBAProject("vbaProject.bin")
// if err != nil {
// fmt.Println(err)
// }
// err = f.SaveAs("macros.xlsm")
// if err != nil {
// fmt.Println(err)
// }
//
func
(
f
*
File
)
AddVBAProject
(
bin
string
)
error
{
var
err
error
// Check vbaProject.bin exists first.
if
_
,
err
=
os
.
Stat
(
bin
);
os
.
IsNotExist
(
err
)
{
return
err
}
if
path
.
Ext
(
bin
)
!=
".bin"
{
return
errors
.
New
(
"unsupported VBA project extension"
)
}
f
.
setContentTypePartVBAProjectExtensions
()
wb
:=
f
.
workbookRelsReader
()
var
rID
int
var
ok
bool
for
_
,
rel
:=
range
wb
.
Relationships
{
if
rel
.
Target
==
"vbaProject.bin"
&&
rel
.
Type
==
SourceRelationshipVBAProject
{
ok
=
true
continue
}
t
,
_
:=
strconv
.
Atoi
(
strings
.
TrimPrefix
(
rel
.
ID
,
"rId"
))
if
t
>
rID
{
rID
=
t
}
}
rID
++
if
!
ok
{
wb
.
Relationships
=
append
(
wb
.
Relationships
,
xlsxWorkbookRelation
{
ID
:
"rId"
+
strconv
.
Itoa
(
rID
),
Target
:
"vbaProject.bin"
,
Type
:
SourceRelationshipVBAProject
,
})
}
file
,
_
:=
ioutil
.
ReadFile
(
bin
)
f
.
XLSX
[
"xl/vbaProject.bin"
]
=
file
return
err
}
// setContentTypePartVBAProjectExtensions provides a function to set the
// content type for relationship parts and the main document part.
func
(
f
*
File
)
setContentTypePartVBAProjectExtensions
()
{
var
ok
bool
content
:=
f
.
contentTypesReader
()
for
_
,
v
:=
range
content
.
Defaults
{
if
v
.
Extension
==
"bin"
{
ok
=
true
}
}
for
idx
,
o
:=
range
content
.
Overrides
{
if
o
.
PartName
==
"/xl/workbook.xml"
{
content
.
Overrides
[
idx
]
.
ContentType
=
"application/vnd.ms-excel.sheet.macroEnabled.main+xml"
}
}
if
!
ok
{
content
.
Defaults
=
append
(
content
.
Defaults
,
xlsxDefault
{
Extension
:
"bin"
,
ContentType
:
"application/vnd.ms-office.vbaProject"
,
})
}
}
excelize_test.go
浏览文件 @
35e48575
...
...
@@ -1078,6 +1078,17 @@ func TestSetDefaultTimeStyle(t *testing.T) {
assert
.
EqualError
(
t
,
f
.
setDefaultTimeStyle
(
"SheetN"
,
""
,
0
),
"sheet SheetN is not exist"
)
}
func
TestAddVBAProject
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
assert
.
NoError
(
t
,
f
.
SetSheetPrOptions
(
"Sheet1"
,
CodeName
(
"Sheet1"
)))
assert
.
EqualError
(
t
,
f
.
AddVBAProject
(
"macros.bin"
),
"stat macros.bin: no such file or directory"
)
assert
.
EqualError
(
t
,
f
.
AddVBAProject
(
filepath
.
Join
(
"test"
,
"Book1.xlsx"
)),
"unsupported VBA project extension"
)
assert
.
NoError
(
t
,
f
.
AddVBAProject
(
filepath
.
Join
(
"test"
,
"vbaProject.bin"
)))
// Test add VBA project twice.
assert
.
NoError
(
t
,
f
.
AddVBAProject
(
filepath
.
Join
(
"test"
,
"vbaProject.bin"
)))
assert
.
NoError
(
t
,
f
.
SaveAs
(
filepath
.
Join
(
"test"
,
"TestAddVBAProject.xlsm"
)))
}
func
prepareTestBook1
()
(
*
File
,
error
)
{
f
,
err
:=
OpenFile
(
filepath
.
Join
(
"test"
,
"Book1.xlsx"
))
if
err
!=
nil
{
...
...
test/vbaProject.bin
0 → 100755
浏览文件 @
35e48575
文件已添加
xmlDrawing.go
浏览文件 @
35e48575
...
...
@@ -22,6 +22,7 @@ const (
SourceRelationshipDrawingVML
=
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
SourceRelationshipHyperLink
=
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
SourceRelationshipWorkSheet
=
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
SourceRelationshipVBAProject
=
"http://schemas.microsoft.com/office/2006/relationships/vbaProject"
SourceRelationshipChart201506
=
"http://schemas.microsoft.com/office/drawing/2015/06/chart"
SourceRelationshipChart20070802
=
"http://schemas.microsoft.com/office/drawing/2007/8/2/chart"
SourceRelationshipChart2014
=
"http://schemas.microsoft.com/office/drawing/2014/chart"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录