Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
b1c9884f
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 搜索 >>
提交
b1c9884f
编写于
4月 25, 2019
作者:
H
Harris
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add the ability to change the default font
Closes #390
上级
f91f5486
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
65 addition
and
14 deletion
+65
-14
comment.go
comment.go
+3
-2
shape.go
shape.go
+1
-1
styles.go
styles.go
+31
-9
styles_test.go
styles_test.go
+29
-1
templates.go
templates.go
+1
-1
未找到文件。
comment.go
浏览文件 @
b1c9884f
...
...
@@ -239,6 +239,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
},
}
}
defaultFont
:=
f
.
GetDefaultFont
()
cmt
:=
xlsxComment
{
Ref
:
cell
,
AuthorID
:
0
,
...
...
@@ -251,7 +252,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
Color
:
&
xlsxColor
{
Indexed
:
81
,
},
RFont
:
&
attrValString
{
Val
:
"Calibri"
},
RFont
:
&
attrValString
{
Val
:
defaultFont
},
Family
:
&
attrValInt
{
Val
:
2
},
},
T
:
a
,
...
...
@@ -262,7 +263,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
Color
:
&
xlsxColor
{
Indexed
:
81
,
},
RFont
:
&
attrValString
{
Val
:
"Calibri"
},
RFont
:
&
attrValString
{
Val
:
defaultFont
},
Family
:
&
attrValInt
{
Val
:
2
},
},
T
:
t
,
...
...
shape.go
浏览文件 @
b1c9884f
...
...
@@ -381,7 +381,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
Bold
:
false
,
Italic
:
false
,
Underline
:
"none"
,
Family
:
"Calibri"
,
Family
:
f
.
GetDefaultFont
()
,
Size
:
11
,
Color
:
"#000000"
,
},
...
...
styles.go
浏览文件 @
b1c9884f
...
...
@@ -1896,7 +1896,7 @@ func (f *File) NewStyle(style string) (int, error) {
if
fs
.
Font
!=
nil
{
s
.
Fonts
.
Count
++
s
.
Fonts
.
Font
=
append
(
s
.
Fonts
.
Font
,
setFont
(
fs
))
s
.
Fonts
.
Font
=
append
(
s
.
Fonts
.
Font
,
f
.
setFont
(
fs
))
fontID
=
s
.
Fonts
.
Count
-
1
}
...
...
@@ -1932,7 +1932,7 @@ func (f *File) NewConditionalStyle(style string) (int, error) {
Border
:
setBorders
(
fs
),
}
if
fs
.
Font
!=
nil
{
dxf
.
Font
=
setFont
(
fs
)
dxf
.
Font
=
f
.
setFont
(
fs
)
}
dxfStr
,
_
:=
xml
.
Marshal
(
dxf
)
if
s
.
Dxfs
==
nil
{
...
...
@@ -1945,9 +1945,32 @@ func (f *File) NewConditionalStyle(style string) (int, error) {
return
s
.
Dxfs
.
Count
-
1
,
nil
}
// GetDefaultFont provides the default font name currently set in the workbook
// Documents generated by excelize start with Calibri
func
(
f
*
File
)
GetDefaultFont
()
string
{
font
:=
f
.
readDefaultFont
()
return
font
.
Name
.
Val
}
// SetDefaultFont changes the default font in the workbook
func
(
f
*
File
)
SetDefaultFont
(
fontName
string
)
{
font
:=
f
.
readDefaultFont
()
font
.
Name
.
Val
=
fontName
s
:=
f
.
stylesReader
()
s
.
Fonts
.
Font
[
0
]
=
font
custom
:=
true
s
.
CellStyles
.
CellStyle
[
0
]
.
CustomBuiltIn
=
&
custom
}
// readDefaultFont provides an unmarshalled font value
func
(
f
*
File
)
readDefaultFont
()
*
xlsxFont
{
s
:=
f
.
stylesReader
()
return
s
.
Fonts
.
Font
[
0
]
}
// setFont provides a function to add font style by given cell format
// settings.
func
setFont
(
formatStyle
*
formatStyle
)
*
xlsxFont
{
func
(
f
*
File
)
setFont
(
formatStyle
*
formatStyle
)
*
xlsxFont
{
fontUnderlineType
:=
map
[
string
]
string
{
"single"
:
"single"
,
"double"
:
"double"
}
if
formatStyle
.
Font
.
Size
<
1
{
formatStyle
.
Font
.
Size
=
11
...
...
@@ -1955,7 +1978,7 @@ func setFont(formatStyle *formatStyle) *xlsxFont {
if
formatStyle
.
Font
.
Color
==
""
{
formatStyle
.
Font
.
Color
=
"#000000"
}
f
:=
xlsxFont
{
f
nt
:=
xlsxFont
{
B
:
formatStyle
.
Font
.
Bold
,
I
:
formatStyle
.
Font
.
Italic
,
Sz
:
&
attrValInt
{
Val
:
formatStyle
.
Font
.
Size
},
...
...
@@ -1963,15 +1986,14 @@ func setFont(formatStyle *formatStyle) *xlsxFont {
Name
:
&
attrValString
{
Val
:
formatStyle
.
Font
.
Family
},
Family
:
&
attrValInt
{
Val
:
2
},
}
if
f
.
Name
.
Val
==
""
{
f
.
Name
.
Val
=
"Calibri"
f
.
Scheme
=
&
attrValString
{
Val
:
"minor"
}
if
fnt
.
Name
.
Val
==
""
{
fnt
.
Name
.
Val
=
f
.
GetDefaultFont
()
}
val
,
ok
:=
fontUnderlineType
[
formatStyle
.
Font
.
Underline
]
if
ok
{
f
.
U
=
&
attrValString
{
Val
:
val
}
f
nt
.
U
=
&
attrValString
{
Val
:
val
}
}
return
&
f
return
&
f
nt
}
// setNumFmt provides a function to check if number format code in the range
...
...
styles_test.go
浏览文件 @
b1c9884f
...
...
@@ -25,7 +25,7 @@ func TestStyleFill(t *testing.T) {
xl
:=
NewFile
()
styleID
,
err
:=
xl
.
NewStyle
(
testCase
.
format
)
if
err
!=
nil
{
t
.
Fatal
f
(
"%v"
,
err
)
t
.
Fatal
(
err
)
}
styles
:=
xl
.
stylesReader
()
...
...
@@ -165,3 +165,31 @@ func TestSetConditionalFormat(t *testing.T) {
assert
.
EqualValues
(
t
,
testCase
.
rules
,
cf
[
0
]
.
CfRule
,
testCase
.
label
)
}
}
func
TestNewStyle
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
styleID
,
err
:=
f
.
NewStyle
(
`{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
styles
:=
f
.
stylesReader
()
fontID
:=
styles
.
CellXfs
.
Xf
[
styleID
]
.
FontID
font
:=
styles
.
Fonts
.
Font
[
fontID
]
assert
.
Contains
(
t
,
font
.
Name
.
Val
,
"Berlin Sans FB Demi"
,
"Stored font should contain font name"
)
assert
.
Equal
(
t
,
2
,
styles
.
CellXfs
.
Count
,
"Should have 2 styles"
)
}
func
TestGetDefaultFont
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
s
:=
f
.
GetDefaultFont
()
assert
.
Equal
(
t
,
s
,
"Calibri"
,
"Default font should be Calibri"
)
}
func
TestSetDefaultFont
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
f
.
SetDefaultFont
(
"Ariel"
)
styles
:=
f
.
stylesReader
()
s
:=
f
.
GetDefaultFont
()
assert
.
Equal
(
t
,
s
,
"Ariel"
,
"Default font should change to Ariel"
)
assert
.
Equal
(
t
,
*
styles
.
CellStyles
.
CellStyle
[
0
]
.
CustomBuiltIn
,
true
)
}
templates.go
浏览文件 @
b1c9884f
...
...
@@ -27,7 +27,7 @@ const templateContentTypes = `<Types xmlns="http://schemas.openxmlformats.org/pa
const
templateWorkbook
=
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"><fileVersion appName="xl" lastEdited="6" lowestEdited="6" rupBuild="14420" /><workbookPr filterPrivacy="1" defaultThemeVersion="164011" /><bookViews><workbookView xWindow="0" yWindow="0" windowWidth="14805" windowHeight="8010" /></bookViews><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1" /></sheets><calcPr calcId="122211" /></workbook>`
const
templateStyles
=
`<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main"><fonts count="1" x14ac:knownFonts="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><
scheme val="minor"/></font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs><cellStyles count="1"><cellStyle name="常规
" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/></styleSheet>`
const
templateStyles
=
`<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main"><fonts count="1" x14ac:knownFonts="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><
/font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs><cellStyles count="1"><cellStyle name="Normal
" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/></styleSheet>`
const
templateSheet
=
`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="15"/><sheetData/><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>`
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录