Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
2d21b5b5
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 搜索 >>
提交
2d21b5b5
编写于
10月 15, 2019
作者:
S
streboryaj
提交者:
xurime
10月 15, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added accessors for Getting/Setting Page Margins (#497)
* Added accessors for Getting/Setting Page Margins * Added test cases
上级
af100372
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
231 addition
and
0 deletion
+231
-0
sheet.go
sheet.go
+159
-0
sheet_test.go
sheet_test.go
+62
-0
xmlWorksheet.go
xmlWorksheet.go
+10
-0
未找到文件。
sheet.go
浏览文件 @
2d21b5b5
...
...
@@ -1401,3 +1401,162 @@ func makeContiguousColumns(xlsx *xlsxWorksheet, fromRow, toRow, colCount int) {
fillColumns
(
rowData
,
colCount
,
fromRow
)
}
}
type
(
PageMarginBottom
float64
PageMarginFooter
float64
PageMarginHeader
float64
PageMarginLeft
float64
PageMarginRight
float64
PageMarginTop
float64
)
// setPageMargins provides a method to set the bottom margin for the worksheet.
func
(
p
PageMarginBottom
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Bottom
=
float64
(
p
)
}
// setPageMargins provides a method to get the bottom margin for the worksheet.
func
(
o
*
PageMarginBottom
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Bottom
==
0
{
*
o
=
0.75
return
}
*
o
=
PageMarginBottom
(
ps
.
Bottom
)
}
// setPageMargins provides a method to set the Footer margin for the worksheet.
func
(
p
PageMarginFooter
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Footer
=
float64
(
p
)
}
// setPageMargins provides a method to get the Footer margin for the worksheet.
func
(
o
*
PageMarginFooter
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Footer
==
0
{
*
o
=
0.3
return
}
*
o
=
PageMarginFooter
(
ps
.
Footer
)
}
// setPageMargins provides a method to set the Header margin for the worksheet.
func
(
p
PageMarginHeader
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Header
=
float64
(
p
)
}
// setPageMargins provides a method to get the Header margin for the worksheet.
func
(
o
*
PageMarginHeader
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Header
==
0
{
*
o
=
0.3
return
}
*
o
=
PageMarginHeader
(
ps
.
Header
)
}
// setPageMargins provides a method to set the left margin for the worksheet.
func
(
p
PageMarginLeft
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Left
=
float64
(
p
)
}
// setPageMargins provides a method to get the left margin for the worksheet.
func
(
o
*
PageMarginLeft
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Left
==
0
{
*
o
=
0.7
return
}
*
o
=
PageMarginLeft
(
ps
.
Left
)
}
// setPageMargins provides a method to set the right margin for the worksheet.
func
(
p
PageMarginRight
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Right
=
float64
(
p
)
}
// setPageMargins provides a method to get the right margin for the worksheet.
func
(
o
*
PageMarginRight
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Right
==
0
{
*
o
=
0.7
return
}
*
o
=
PageMarginRight
(
ps
.
Right
)
}
// setPageMargins provides a method to set the top margin for the worksheet.
func
(
p
PageMarginTop
)
setPageMargins
(
ps
*
xlsxPageMargins
)
{
ps
.
Top
=
float64
(
p
)
}
// setPageMargins provides a method to get the top margin for the worksheet.
func
(
o
*
PageMarginTop
)
getPageMargins
(
ps
*
xlsxPageMargins
)
{
// Excel default: portrait
if
ps
==
nil
||
ps
.
Top
==
0
{
*
o
=
0.75
return
}
*
o
=
PageMarginTop
(
ps
.
Top
)
}
// PageMarginsOptions is an option of a page margin of a worksheet. See
// SetPageMargins().
type
PageMarginsOptions
interface
{
setPageMargins
(
layout
*
xlsxPageMargins
)
}
// PageMarginsOptionsPtr is a writable PageMarginsOptions. See GetPageMargins().
type
PageMarginsOptionsPtr
interface
{
PageMarginsOptions
getPageMargins
(
layout
*
xlsxPageMargins
)
}
// SetPageMargins provides a function to set worksheet page lmargins.
//
// Available options:
// PageMarginBotom(float64)
// PageMarginFooter(float64)
// PageMarginHeader(float64)
// PageMarginLeft(float64)
// PageMarginRightfloat64)
// PageMarginTop(float64)
func
(
f
*
File
)
SetPageMargins
(
sheet
string
,
opts
...
PageMarginsOptions
)
error
{
s
,
err
:=
f
.
workSheetReader
(
sheet
)
if
err
!=
nil
{
return
err
}
ps
:=
s
.
PageMargins
if
ps
==
nil
{
ps
=
new
(
xlsxPageMargins
)
s
.
PageMargins
=
ps
}
for
_
,
opt
:=
range
opts
{
opt
.
setPageMargins
(
ps
)
}
return
err
}
// GetPageMargins provides a function to get worksheet page margins.
//
// Available options:
// PageMarginBotom(float64)
// PageMarginFooter(float64)
// PageMarginHeader(float64)
// PageMarginLeft(float64)
// PageMarginRightfloat64)
// PageMarginTop(float64)
func
(
f
*
File
)
GetPageMargins
(
sheet
string
,
opts
...
PageMarginsOptionsPtr
)
error
{
s
,
err
:=
f
.
workSheetReader
(
sheet
)
if
err
!=
nil
{
return
err
}
ps
:=
s
.
PageMargins
for
_
,
opt
:=
range
opts
{
opt
.
getPageMargins
(
ps
)
}
return
err
}
sheet_test.go
浏览文件 @
2d21b5b5
...
...
@@ -247,3 +247,65 @@ func TestGetSheetMap(t *testing.T) {
}
assert
.
Equal
(
t
,
len
(
sheetMap
),
2
)
}
func
TestPageMarginsOption
(
t
*
testing
.
T
)
{
const
sheet
=
"Sheet1"
testData
:=
[]
struct
{
container
excelize
.
PageMarginsOptionsPtr
nonDefault
excelize
.
PageMarginsOptions
}{
{
new
(
excelize
.
PageMarginTop
),
excelize
.
PageMarginTop
(
1.0
)},
{
new
(
excelize
.
PageMarginBottom
),
excelize
.
PageMarginBottom
(
1.0
)},
{
new
(
excelize
.
PageMarginLeft
),
excelize
.
PageMarginLeft
(
1.0
)},
{
new
(
excelize
.
PageMarginRight
),
excelize
.
PageMarginRight
(
1.0
)},
{
new
(
excelize
.
PageMarginHeader
),
excelize
.
PageMarginHeader
(
1.0
)},
{
new
(
excelize
.
PageMarginFooter
),
excelize
.
PageMarginFooter
(
1.0
)},
}
for
i
,
test
:=
range
testData
{
t
.
Run
(
fmt
.
Sprintf
(
"TestData%d"
,
i
),
func
(
t
*
testing
.
T
)
{
opt
:=
test
.
nonDefault
t
.
Logf
(
"option %T"
,
opt
)
def
:=
deepcopy
.
Copy
(
test
.
container
)
.
(
excelize
.
PageMarginsOptionsPtr
)
val1
:=
deepcopy
.
Copy
(
def
)
.
(
excelize
.
PageMarginsOptionsPtr
)
val2
:=
deepcopy
.
Copy
(
def
)
.
(
excelize
.
PageMarginsOptionsPtr
)
f
:=
excelize
.
NewFile
()
// Get the default value
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
def
),
opt
)
// Get again and check
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
val1
),
opt
)
if
!
assert
.
Equal
(
t
,
val1
,
def
,
opt
)
{
t
.
FailNow
()
}
// Set the same value
assert
.
NoError
(
t
,
f
.
SetPageMargins
(
sheet
,
val1
),
opt
)
// Get again and check
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
val1
),
opt
)
if
!
assert
.
Equal
(
t
,
val1
,
def
,
"%T: value should not have changed"
,
opt
)
{
t
.
FailNow
()
}
// Set a different value
assert
.
NoError
(
t
,
f
.
SetPageMargins
(
sheet
,
test
.
nonDefault
),
opt
)
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
val1
),
opt
)
// Get again and compare
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
val2
),
opt
)
if
!
assert
.
Equal
(
t
,
val1
,
val2
,
"%T: value should not have changed"
,
opt
)
{
t
.
FailNow
()
}
// Value should not be the same as the default
if
!
assert
.
NotEqual
(
t
,
def
,
val1
,
"%T: value should have changed from default"
,
opt
)
{
t
.
FailNow
()
}
// Restore the default value
assert
.
NoError
(
t
,
f
.
SetPageMargins
(
sheet
,
def
),
opt
)
assert
.
NoError
(
t
,
f
.
GetPageMargins
(
sheet
,
val1
),
opt
)
if
!
assert
.
Equal
(
t
,
def
,
val1
)
{
t
.
FailNow
()
}
})
}
}
xmlWorksheet.go
浏览文件 @
2d21b5b5
...
...
@@ -801,3 +801,13 @@ type FormatHeaderFooter struct {
FirstFooter
string
FirstHeader
string
}
// FormatPageMargins directly maps the settings of page margins
type
FormatPageMargins
struct
{
Bottom
string
Footer
string
Header
string
Left
string
Right
string
Top
string
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录