Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
c63ae6d2
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 搜索 >>
已验证
提交
c63ae6d2
编写于
8月 17, 2023
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
This fixed #1610, support to create a conditional format with number format and protection
上级
a1810aa0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
64 addition
and
11 deletion
+64
-11
styles.go
styles.go
+47
-5
styles_test.go
styles_test.go
+17
-1
xmlStyles.go
xmlStyles.go
+0
-5
未找到文件。
styles.go
浏览文件 @
c63ae6d2
...
...
@@ -1032,6 +1032,7 @@ func (f *File) NewStyle(style *Style) (int, error) {
return
setCellXfs
(
s
,
fontID
,
numFmtID
,
fillID
,
borderID
,
applyAlignment
,
applyProtection
,
alignment
,
protection
)
}
// getXfIDFuncs provides a function to get xfID by given style.
var
getXfIDFuncs
=
map
[
string
]
func
(
int
,
xlsxXf
,
*
Style
)
bool
{
"numFmt"
:
func
(
numFmtID
int
,
xf
xlsxXf
,
style
*
Style
)
bool
{
if
style
.
CustomNumFmt
==
nil
&&
numFmtID
==
-
1
{
...
...
@@ -1121,7 +1122,10 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
if
err
!=
nil
{
return
0
,
err
}
dxf
:=
dxf
{
if
fs
.
DecimalPlaces
!=
nil
&&
(
*
fs
.
DecimalPlaces
<
0
||
*
fs
.
DecimalPlaces
>
30
)
{
fs
.
DecimalPlaces
=
intPtr
(
2
)
}
dxf
:=
xlsxDxf
{
Fill
:
newFills
(
fs
,
false
),
}
if
fs
.
Alignment
!=
nil
{
...
...
@@ -1133,17 +1137,55 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
if
fs
.
Font
!=
nil
{
dxf
.
Font
,
_
=
f
.
newFont
(
fs
)
}
dxfStr
,
_
:=
xml
.
Marshal
(
dxf
)
if
fs
.
Protection
!=
nil
{
dxf
.
Protection
=
newProtection
(
fs
)
}
dxf
.
NumFmt
=
newDxfNumFmt
(
s
,
style
,
&
dxf
)
if
s
.
Dxfs
==
nil
{
s
.
Dxfs
=
&
xlsxDxfs
{}
}
s
.
Dxfs
.
Count
++
s
.
Dxfs
.
Dxfs
=
append
(
s
.
Dxfs
.
Dxfs
,
&
xlsxDxf
{
Dxf
:
string
(
dxfStr
[
5
:
len
(
dxfStr
)
-
6
]),
})
s
.
Dxfs
.
Dxfs
=
append
(
s
.
Dxfs
.
Dxfs
,
&
dxf
)
return
s
.
Dxfs
.
Count
-
1
,
nil
}
// newDxfNumFmt provides a function to create number format for conditional
// format styles.
func
newDxfNumFmt
(
styleSheet
*
xlsxStyleSheet
,
style
*
Style
,
dxf
*
xlsxDxf
)
*
xlsxNumFmt
{
dp
,
numFmtID
:=
"0"
,
164
// Default custom number format code from 164.
if
style
.
DecimalPlaces
!=
nil
&&
*
style
.
DecimalPlaces
>
0
{
dp
+=
"."
for
i
:=
0
;
i
<
*
style
.
DecimalPlaces
;
i
++
{
dp
+=
"0"
}
}
if
style
.
CustomNumFmt
!=
nil
{
if
styleSheet
.
Dxfs
!=
nil
{
for
_
,
d
:=
range
styleSheet
.
Dxfs
.
Dxfs
{
if
d
!=
nil
&&
d
.
NumFmt
!=
nil
&&
d
.
NumFmt
.
NumFmtID
>
numFmtID
{
numFmtID
=
d
.
NumFmt
.
NumFmtID
}
}
}
return
&
xlsxNumFmt
{
NumFmtID
:
numFmtID
+
1
,
FormatCode
:
*
style
.
CustomNumFmt
}
}
numFmtCode
,
ok
:=
builtInNumFmt
[
style
.
NumFmt
]
if
style
.
NumFmt
>
0
&&
ok
{
return
&
xlsxNumFmt
{
NumFmtID
:
style
.
NumFmt
,
FormatCode
:
numFmtCode
}
}
fc
,
currency
:=
currencyNumFmt
[
style
.
NumFmt
]
if
!
currency
{
return
nil
}
if
style
.
DecimalPlaces
!=
nil
{
fc
=
strings
.
ReplaceAll
(
fc
,
"0.00"
,
dp
)
}
if
style
.
NegRed
{
fc
=
fc
+
";[Red]"
+
fc
}
return
&
xlsxNumFmt
{
NumFmtID
:
numFmtID
,
FormatCode
:
fc
}
}
// GetDefaultFont provides the default font name currently set in the
// workbook. The spreadsheet generated by excelize default font is Calibri.
func
(
f
*
File
)
GetDefaultFont
()
(
string
,
error
)
{
...
...
styles_test.go
浏览文件 @
c63ae6d2
...
...
@@ -355,10 +355,26 @@ func TestNewStyle(t *testing.T) {
func
TestNewConditionalStyle
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
_
,
err
:=
f
.
NewConditionalStyle
(
&
Style
{
Protection
:
&
Protection
{
Hidden
:
true
,
Locked
:
true
}})
assert
.
NoError
(
t
,
err
)
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
DecimalPlaces
:
intPtr
(
4
),
NumFmt
:
165
,
NegRed
:
true
})
assert
.
NoError
(
t
,
err
)
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
DecimalPlaces
:
intPtr
(
-
1
)})
assert
.
NoError
(
t
,
err
)
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
NumFmt
:
1
})
assert
.
NoError
(
t
,
err
)
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
NumFmt
:
27
})
assert
.
NoError
(
t
,
err
)
numFmt
:=
"general"
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
CustomNumFmt
:
&
numFmt
})
assert
.
NoError
(
t
,
err
)
numFmt1
:=
"0.00"
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
CustomNumFmt
:
&
numFmt1
})
assert
.
NoError
(
t
,
err
)
// Test create conditional style with unsupported charset style sheet
f
.
Styles
=
nil
f
.
Pkg
.
Store
(
defaultXMLPathStyles
,
MacintoshCyrillicCharset
)
_
,
err
:
=
f
.
NewConditionalStyle
(
&
Style
{
Font
:
&
Font
{
Color
:
"9A0511"
},
Fill
:
Fill
{
Type
:
"pattern"
,
Color
:
[]
string
{
"FEC7CE"
},
Pattern
:
1
}})
_
,
err
=
f
.
NewConditionalStyle
(
&
Style
{
Font
:
&
Font
{
Color
:
"9A0511"
},
Fill
:
Fill
{
Type
:
"pattern"
,
Color
:
[]
string
{
"FEC7CE"
},
Pattern
:
1
}})
assert
.
EqualError
(
t
,
err
,
"XML syntax error on line 1: invalid UTF-8"
)
}
...
...
xmlStyles.go
浏览文件 @
c63ae6d2
...
...
@@ -251,11 +251,6 @@ type xlsxDxfs struct {
// xlsxDxf directly maps the dxf element. A single dxf record, expressing
// incremental formatting to be applied.
type
xlsxDxf
struct
{
Dxf
string
`xml:",innerxml"`
}
// dxf directly maps the dxf element.
type
dxf
struct
{
Font
*
xlsxFont
`xml:"font"`
NumFmt
*
xlsxNumFmt
`xml:"numFmt"`
Fill
*
xlsxFill
`xml:"fill"`
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录