Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
4ed49381
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 搜索 >>
未验证
提交
4ed49381
编写于
3月 06, 2024
作者:
E
Evan lu
提交者:
GitHub
3月 06, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
This closes #1835, support get data validations which storage in the extension lists (#1834)
上级
f20bbd1f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
90 addition
and
28 deletion
+90
-28
cell_test.go
cell_test.go
+1
-1
datavalidation.go
datavalidation.go
+61
-26
datavalidation_test.go
datavalidation_test.go
+26
-0
templates.go
templates.go
+1
-1
xmlWorksheet.go
xmlWorksheet.go
+1
-0
未找到文件。
cell_test.go
浏览文件 @
4ed49381
...
...
@@ -91,7 +91,7 @@ func TestConcurrency(t *testing.T) {
// Concurrency add data validation
dv
:=
NewDataValidation
(
true
)
dv
.
Sqref
=
fmt
.
Sprintf
(
"A%d:B%d"
,
val
,
val
)
dv
.
SetRange
(
10
,
20
,
DataValidationTypeWhole
,
DataValidationOperatorGreaterThan
)
assert
.
NoError
(
t
,
dv
.
SetRange
(
10
,
20
,
DataValidationTypeWhole
,
DataValidationOperatorGreaterThan
)
)
dv
.
SetInput
(
fmt
.
Sprintf
(
"title:%d"
,
val
),
strconv
.
Itoa
(
val
))
assert
.
NoError
(
t
,
f
.
AddDataValidation
(
"Sheet1"
,
dv
))
// Concurrency delete data validation with reference sequence
...
...
datavalidation.go
浏览文件 @
4ed49381
...
...
@@ -13,6 +13,7 @@ package excelize
import
(
"fmt"
"io"
"math"
"strings"
"unicode/utf16"
...
...
@@ -293,36 +294,70 @@ func (f *File) GetDataValidations(sheet string) ([]*DataValidation, error) {
if
err
!=
nil
{
return
nil
,
err
}
if
ws
.
DataValidations
==
nil
||
len
(
ws
.
DataValidations
.
DataValidation
)
==
0
{
return
nil
,
err
var
(
dataValidations
[]
*
DataValidation
decodeExtLst
=
new
(
decodeExtLst
)
decodeDataValidations
*
xlsxDataValidations
ext
*
xlsxExt
)
if
ws
.
DataValidations
!=
nil
{
dataValidations
=
append
(
dataValidations
,
getDataValidations
(
ws
.
DataValidations
)
...
)
}
var
dvs
[]
*
DataValidation
for
_
,
dv
:=
range
ws
.
DataValidations
.
DataValidation
{
if
dv
!=
nil
{
dataValidation
:=
&
DataValidation
{
AllowBlank
:
dv
.
AllowBlank
,
Error
:
dv
.
Error
,
ErrorStyle
:
dv
.
ErrorStyle
,
ErrorTitle
:
dv
.
ErrorTitle
,
Operator
:
dv
.
Operator
,
Prompt
:
dv
.
Prompt
,
PromptTitle
:
dv
.
PromptTitle
,
ShowDropDown
:
dv
.
ShowDropDown
,
ShowErrorMessage
:
dv
.
ShowErrorMessage
,
ShowInputMessage
:
dv
.
ShowInputMessage
,
Sqref
:
dv
.
Sqref
,
Type
:
dv
.
Type
,
}
if
dv
.
Formula1
!=
nil
{
dataValidation
.
Formula1
=
unescapeDataValidationFormula
(
dv
.
Formula1
.
Content
)
}
if
dv
.
Formula2
!=
nil
{
dataValidation
.
Formula2
=
unescapeDataValidationFormula
(
dv
.
Formula2
.
Content
)
if
ws
.
ExtLst
!=
nil
{
if
err
=
f
.
xmlNewDecoder
(
strings
.
NewReader
(
"<extLst>"
+
ws
.
ExtLst
.
Ext
+
"</extLst>"
))
.
Decode
(
decodeExtLst
);
err
!=
nil
&&
err
!=
io
.
EOF
{
return
dataValidations
,
err
}
for
_
,
ext
=
range
decodeExtLst
.
Ext
{
if
ext
.
URI
==
ExtURIDataValidations
{
decodeDataValidations
=
new
(
xlsxDataValidations
)
_
=
f
.
xmlNewDecoder
(
strings
.
NewReader
(
ext
.
Content
))
.
Decode
(
decodeDataValidations
)
dataValidations
=
append
(
dataValidations
,
getDataValidations
(
decodeDataValidations
)
...
)
}
dvs
=
append
(
dvs
,
dataValidation
)
}
}
return
dvs
,
err
return
dataValidations
,
err
}
// getDataValidations returns data validations list by given worksheet data
// validations.
func
getDataValidations
(
dvs
*
xlsxDataValidations
)
[]
*
DataValidation
{
if
dvs
==
nil
{
return
nil
}
var
dataValidations
[]
*
DataValidation
for
_
,
dv
:=
range
dvs
.
DataValidation
{
if
dv
==
nil
{
continue
}
dataValidation
:=
&
DataValidation
{
AllowBlank
:
dv
.
AllowBlank
,
Error
:
dv
.
Error
,
ErrorStyle
:
dv
.
ErrorStyle
,
ErrorTitle
:
dv
.
ErrorTitle
,
Operator
:
dv
.
Operator
,
Prompt
:
dv
.
Prompt
,
PromptTitle
:
dv
.
PromptTitle
,
ShowDropDown
:
dv
.
ShowDropDown
,
ShowErrorMessage
:
dv
.
ShowErrorMessage
,
ShowInputMessage
:
dv
.
ShowInputMessage
,
Sqref
:
dv
.
Sqref
,
Type
:
dv
.
Type
,
}
if
dv
.
Formula1
!=
nil
{
dataValidation
.
Formula1
=
unescapeDataValidationFormula
(
dv
.
Formula1
.
Content
)
}
if
dv
.
Formula2
!=
nil
{
dataValidation
.
Formula2
=
unescapeDataValidationFormula
(
dv
.
Formula2
.
Content
)
}
if
dv
.
XMSqref
!=
""
{
dataValidation
.
Sqref
=
dv
.
XMSqref
dataValidation
.
Formula1
=
strings
.
TrimSuffix
(
strings
.
TrimPrefix
(
dataValidation
.
Formula1
,
"<xm:f>"
),
"</xm:f>"
)
dataValidation
.
Formula2
=
strings
.
TrimSuffix
(
strings
.
TrimPrefix
(
dataValidation
.
Formula2
,
"<xm:f>"
),
"</xm:f>"
)
}
dataValidations
=
append
(
dataValidations
,
dataValidation
)
}
return
dataValidations
}
// DeleteDataValidation delete data validation by given worksheet name and
...
...
datavalidation_test.go
浏览文件 @
4ed49381
...
...
@@ -12,6 +12,7 @@
package
excelize
import
(
"fmt"
"math"
"path/filepath"
"strings"
...
...
@@ -104,6 +105,31 @@ func TestDataValidation(t *testing.T) {
dataValidations
,
err
=
f
.
GetDataValidations
(
"Sheet1"
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
[]
*
DataValidation
(
nil
),
dataValidations
)
// Test get data validations which storage in the extension lists
f
=
NewFile
()
ws
,
ok
:=
f
.
Sheet
.
Load
(
"xl/worksheets/sheet1.xml"
)
assert
.
True
(
t
,
ok
)
ws
.
(
*
xlsxWorksheet
)
.
ExtLst
=
&
xlsxExtLst
{
Ext
:
fmt
.
Sprintf
(
`<ext uri="%s" xmlns:x14="%s"><x14:dataValidations><x14:dataValidation type="list" allowBlank="1"><x14:formula1><xm:f>Sheet1!$B$1:$B$5</xm:f></x14:formula1><xm:sqref>A7:B8</xm:sqref></x14:dataValidation></x14:dataValidations></ext>`
,
ExtURIDataValidations
,
NameSpaceSpreadSheetX14
.
Value
)}
dataValidations
,
err
=
f
.
GetDataValidations
(
"Sheet1"
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
[]
*
DataValidation
{
{
AllowBlank
:
true
,
Type
:
"list"
,
Formula1
:
"Sheet1!$B$1:$B$5"
,
Sqref
:
"A7:B8"
,
},
},
dataValidations
)
// Test get data validations with invalid extension list characters
ws
.
(
*
xlsxWorksheet
)
.
ExtLst
=
&
xlsxExtLst
{
Ext
:
fmt
.
Sprintf
(
`<ext uri="%s" xmlns:x14="%s"><x14:dataValidations></x14:dataValidation></x14:dataValidations></ext>`
,
ExtURIDataValidations
,
NameSpaceSpreadSheetX14
.
Value
)}
_
,
err
=
f
.
GetDataValidations
(
"Sheet1"
)
assert
.
EqualError
(
t
,
err
,
"XML syntax error on line 1: element <dataValidations> closed by </dataValidation>"
)
// Test get validations without validations
assert
.
Nil
(
t
,
getDataValidations
(
nil
))
assert
.
Nil
(
t
,
getDataValidations
(
&
xlsxDataValidations
{
DataValidation
:
[]
*
xlsxDataValidation
{
nil
}}))
}
func
TestDataValidationError
(
t
*
testing
.
T
)
{
...
...
templates.go
浏览文件 @
4ed49381
...
...
@@ -103,7 +103,7 @@ const (
ExtURIConditionalFormattingRuleID
=
"{B025F937-C7B1-47D3-B67F-A62EFF666E3E}"
ExtURIConditionalFormattings
=
"{78C0D931-6437-407d-A8EE-F0AAD7539E65}"
ExtURIDataModel
=
"{FCE2AD5D-F65C-4FA6-A056-5C36A1767C68}"
ExtURIDataValidations
=
"{CCE6A557-97BC-4
B
89-ADB6-D9C93CAAB3DF}"
ExtURIDataValidations
=
"{CCE6A557-97BC-4
b
89-ADB6-D9C93CAAB3DF}"
ExtURIDrawingBlip
=
"{28A0092B-C50C-407E-A947-70E740481C1C}"
ExtURIExternalLinkPr
=
"{FCE6A71B-6B00-49CD-AB44-F6B1AE7CDE65}"
ExtURIIgnoredErrors
=
"{01252117-D84E-4E92-8308-4BE1C098FCBB}"
...
...
xmlWorksheet.go
浏览文件 @
4ed49381
...
...
@@ -441,6 +441,7 @@ type xlsxDataValidation struct {
ShowErrorMessage
bool
`xml:"showErrorMessage,attr,omitempty"`
ShowInputMessage
bool
`xml:"showInputMessage,attr,omitempty"`
Sqref
string
`xml:"sqref,attr"`
XMSqref
string
`xml:"sqref,omitempty"`
Type
string
`xml:"type,attr,omitempty"`
Formula1
*
xlsxInnerXML
`xml:"formula1"`
Formula2
*
xlsxInnerXML
`xml:"formula2"`
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录