Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
c0a30208
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 搜索 >>
提交
c0a30208
编写于
1月 25, 2017
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support create merge cell.
上级
bd5b033b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
111 addition
and
0 deletion
+111
-0
cell.go
cell.go
+92
-0
excelize_test.go
excelize_test.go
+19
-0
未找到文件。
cell.go
浏览文件 @
c0a30208
...
...
@@ -139,3 +139,95 @@ func (f *File) SetCellHyperLink(sheet, axis, link string) {
output
,
_
:=
xml
.
Marshal
(
xlsx
)
f
.
saveFileList
(
name
,
replaceWorkSheetsRelationshipsNameSpace
(
string
(
output
)))
}
// MergeCell provides function to merge cells by given axis and sheet name.
// For example create a merged cell of A1:B2 on Sheet1:
//
// xlsx.MergeCell("sheet1", "D9", "E9")
//
// If you create a merged cell that overlaps with another existing merged cell,
// those merged cells that already exist will be removed.
func
(
f
*
File
)
MergeCell
(
sheet
,
hcell
,
vcell
string
)
{
if
hcell
==
vcell
{
return
}
hcell
=
strings
.
ToUpper
(
hcell
)
vcell
=
strings
.
ToUpper
(
vcell
)
// Coordinate conversion, convert C1:B3 to 2,0,1,2.
hcol
:=
string
(
strings
.
Map
(
letterOnlyMapF
,
hcell
))
hrow
,
_
:=
strconv
.
Atoi
(
strings
.
Map
(
intOnlyMapF
,
hcell
))
hyAxis
:=
hrow
-
1
hxAxis
:=
titleToNumber
(
hcol
)
vcol
:=
string
(
strings
.
Map
(
letterOnlyMapF
,
vcell
))
vrow
,
_
:=
strconv
.
Atoi
(
strings
.
Map
(
intOnlyMapF
,
vcell
))
vyAxis
:=
vrow
-
1
vxAxis
:=
titleToNumber
(
vcol
)
if
vxAxis
<
hxAxis
{
hcell
,
vcell
=
vcell
,
hcell
vxAxis
,
hxAxis
=
hxAxis
,
vxAxis
}
if
vyAxis
<
hyAxis
{
hcell
,
vcell
=
vcell
,
hcell
vyAxis
,
hyAxis
=
hyAxis
,
vyAxis
}
var
xlsx
xlsxWorksheet
name
:=
"xl/worksheets/"
+
strings
.
ToLower
(
sheet
)
+
".xml"
xml
.
Unmarshal
([]
byte
(
f
.
readXML
(
name
)),
&
xlsx
)
if
xlsx
.
MergeCells
!=
nil
{
mergeCell
:=
xlsxMergeCell
{}
// Correct the coordinate area, such correct C1:B3 to B1:C3.
mergeCell
.
Ref
=
toAlphaString
(
hxAxis
+
1
)
+
strconv
.
Itoa
(
hyAxis
+
1
)
+
":"
+
toAlphaString
(
vxAxis
+
1
)
+
strconv
.
Itoa
(
vyAxis
+
1
)
// Delete the merged cells of the overlapping area.
for
i
:=
0
;
i
<
len
(
xlsx
.
MergeCells
.
Cells
);
i
++
{
if
checkCellInArea
(
hcell
,
xlsx
.
MergeCells
.
Cells
[
i
]
.
Ref
)
||
checkCellInArea
(
strings
.
Split
(
xlsx
.
MergeCells
.
Cells
[
i
]
.
Ref
,
":"
)[
0
],
mergeCell
.
Ref
)
{
xlsx
.
MergeCells
.
Cells
=
append
(
xlsx
.
MergeCells
.
Cells
[
:
i
],
xlsx
.
MergeCells
.
Cells
[
i
+
1
:
]
...
)
}
else
if
checkCellInArea
(
vcell
,
xlsx
.
MergeCells
.
Cells
[
i
]
.
Ref
)
||
checkCellInArea
(
strings
.
Split
(
xlsx
.
MergeCells
.
Cells
[
i
]
.
Ref
,
":"
)[
1
],
mergeCell
.
Ref
)
{
xlsx
.
MergeCells
.
Cells
=
append
(
xlsx
.
MergeCells
.
Cells
[
:
i
],
xlsx
.
MergeCells
.
Cells
[
i
+
1
:
]
...
)
}
}
xlsx
.
MergeCells
.
Cells
=
append
(
xlsx
.
MergeCells
.
Cells
,
&
mergeCell
)
}
else
{
mergeCell
:=
xlsxMergeCell
{}
// Correct the coordinate area, such correct C1:B3 to B1:C3.
mergeCell
.
Ref
=
toAlphaString
(
hxAxis
+
1
)
+
strconv
.
Itoa
(
hyAxis
+
1
)
+
":"
+
toAlphaString
(
vxAxis
+
1
)
+
strconv
.
Itoa
(
vyAxis
+
1
)
mergeCells
:=
xlsxMergeCells
{}
mergeCells
.
Cells
=
append
(
mergeCells
.
Cells
,
&
mergeCell
)
xlsx
.
MergeCells
=
&
mergeCells
}
output
,
_
:=
xml
.
Marshal
(
xlsx
)
f
.
saveFileList
(
name
,
replaceWorkSheetsRelationshipsNameSpace
(
string
(
output
)))
}
// checkCellInArea provides function to determine if a given coordinate is
// within an area.
func
checkCellInArea
(
cell
,
area
string
)
bool
{
result
:=
false
cell
=
strings
.
ToUpper
(
cell
)
col
:=
string
(
strings
.
Map
(
letterOnlyMapF
,
cell
))
row
,
_
:=
strconv
.
Atoi
(
strings
.
Map
(
intOnlyMapF
,
cell
))
xAxis
:=
row
-
1
yAxis
:=
titleToNumber
(
col
)
ref
:=
strings
.
Split
(
area
,
":"
)
hCol
:=
string
(
strings
.
Map
(
letterOnlyMapF
,
ref
[
0
]))
hRow
,
_
:=
strconv
.
Atoi
(
strings
.
Map
(
intOnlyMapF
,
ref
[
0
]))
hyAxis
:=
hRow
-
1
hxAxis
:=
titleToNumber
(
hCol
)
vCol
:=
string
(
strings
.
Map
(
letterOnlyMapF
,
ref
[
1
]))
vRow
,
_
:=
strconv
.
Atoi
(
strings
.
Map
(
intOnlyMapF
,
ref
[
1
]))
vyAxis
:=
vRow
-
1
vxAxis
:=
titleToNumber
(
vCol
)
if
hxAxis
<=
yAxis
&&
yAxis
<=
vxAxis
&&
hyAxis
<=
xAxis
&&
xAxis
<=
vyAxis
{
result
=
true
}
return
result
}
excelize_test.go
浏览文件 @
c0a30208
...
...
@@ -235,3 +235,22 @@ func TestSetSheetBackground(t *testing.T) {
t
.
Log
(
err
)
}
}
func
TestSMergeCell
(
t
*
testing
.
T
)
{
xlsx
,
err
:=
OpenFile
(
"./test/Workbook1.xlsx"
)
if
err
!=
nil
{
t
.
Log
(
err
)
}
xlsx
.
MergeCell
(
"sheet1"
,
"D9"
,
"D9"
)
xlsx
.
MergeCell
(
"sheet1"
,
"D9"
,
"E9"
)
xlsx
.
MergeCell
(
"sheet1"
,
"H14"
,
"G13"
)
xlsx
.
MergeCell
(
"sheet1"
,
"C9"
,
"D8"
)
xlsx
.
MergeCell
(
"sheet1"
,
"F11"
,
"G13"
)
xlsx
.
MergeCell
(
"sheet1"
,
"H7"
,
"B15"
)
xlsx
.
MergeCell
(
"sheet1"
,
"D11"
,
"F13"
)
xlsx
.
MergeCell
(
"sheet1"
,
"G10"
,
"I11"
)
err
=
xlsx
.
Save
()
if
err
!=
nil
{
t
.
Log
(
err
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录