Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
此号慢热型
excelize
提交
9a6f66a9
excelize
项目概览
此号慢热型
/
excelize
与 Fork 源项目一致
Fork自
xuri / excelize
通知
2
Star
0
Fork
0
代码
文件
提交
分支
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,发现更多精彩内容 >>
已验证
提交
9a6f66a9
编写于
12月 26, 2018
作者:
xurime
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New feature: the function `SearchSheet` now support regular expression, relate pull request #316
上级
7b7ca99f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
30 addition
and
71 deletion
+30
-71
excelize.go
excelize.go
+2
-0
excelize_test.go
excelize_test.go
+4
-13
sheet.go
sheet.go
+24
-58
未找到文件。
excelize.go
浏览文件 @
9a6f66a9
...
...
@@ -243,6 +243,8 @@ func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, rowIndex, offset int) {
}
}
// ajustSingleRowDimensions provides a function to ajust single row
// dimensions.
func
(
f
*
File
)
ajustSingleRowDimensions
(
r
*
xlsxRow
,
offset
int
)
{
r
.
R
+=
offset
for
i
,
col
:=
range
r
.
C
{
...
...
excelize_test.go
浏览文件 @
9a6f66a9
...
...
@@ -1053,6 +1053,7 @@ func TestDuplicateRow(t *testing.T) {
xlsx
.
SetCellStr
(
sheet
,
b1
,
bnValue
)
t
.
Run
(
"FromSingleRow"
,
func
(
t
*
testing
.
T
)
{
xlsx
.
DuplicateRow
(
sheet
,
-
1
)
xlsx
.
DuplicateRow
(
sheet
,
1
)
xlsx
.
DuplicateRow
(
sheet
,
2
)
...
...
@@ -1333,19 +1334,9 @@ func TestSearchSheet(t *testing.T) {
// Test search a not exists value.
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet1"
,
"X"
))
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet1"
,
"A"
))
}
func
TestRegSearchSheet
(
t
*
testing
.
T
)
{
xlsx
,
err
:=
OpenFile
(
"./test/Book1.xlsx"
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
}
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet1"
,
"[0-9]"
))
// Test search in a not exists worksheet.
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet4"
,
""
))
// Test search a not exists value.
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet1"
,
""
))
// Test search the coordinates where the numerical value in the range of
// "0-9" of Sheet1 is described by regular expression:
t
.
Log
(
xlsx
.
SearchSheet
(
"Sheet1"
,
"[0-9]"
,
true
))
}
func
TestProtectSheet
(
t
*
testing
.
T
)
{
...
...
sheet.go
浏览文件 @
9a6f66a9
...
...
@@ -658,66 +658,26 @@ func (f *File) GetSheetVisible(name string) bool {
return
visible
}
// SearchSheet provides a function to get coordinates by given worksheet name
// and cell value. This function only supports exact match of strings and
// numbers, doesn't support the calculated result, formatted numbers and
// conditional lookup currently. If it is a merged cell, it will return the
// coordinates of the upper left corner of the merged area. For example,
// search the coordinates of the value of "100" on Sheet1:
// SearchSheet provides a function to get coordinates by given worksheet name,
// cell value, and regular expression. The function doesn't support searching
// on the calculated result, formatted numbers and conditional lookup
// currently. If it is a merged cell, it will return the coordinates of the
// upper left corner of the merged area.
//
// An example of search the coordinates of the value of "100" on Sheet1:
//
// xlsx.SearchSheet("Sheet1", "100")
//
func
(
f
*
File
)
SearchSheet
(
sheet
,
value
string
)
[]
string
{
xlsx
:=
f
.
workSheetReader
(
sheet
)
result
:=
[]
string
{}
name
,
ok
:=
f
.
sheetMap
[
trimSheetName
(
sheet
)]
if
!
ok
{
return
result
}
if
xlsx
!=
nil
{
output
,
_
:=
xml
.
Marshal
(
f
.
Sheet
[
name
])
f
.
saveFileList
(
name
,
replaceWorkSheetsRelationshipsNameSpaceBytes
(
output
))
}
xml
.
NewDecoder
(
bytes
.
NewReader
(
f
.
readXML
(
name
)))
d
:=
f
.
sharedStringsReader
()
var
inElement
string
var
r
xlsxRow
decoder
:=
xml
.
NewDecoder
(
bytes
.
NewReader
(
f
.
readXML
(
name
)))
for
{
token
,
_
:=
decoder
.
Token
()
if
token
==
nil
{
break
}
switch
startElement
:=
token
.
(
type
)
{
case
xml
.
StartElement
:
inElement
=
startElement
.
Name
.
Local
if
inElement
==
"row"
{
r
=
xlsxRow
{}
_
=
decoder
.
DecodeElement
(
&
r
,
&
startElement
)
for
_
,
colCell
:=
range
r
.
C
{
val
,
_
:=
colCell
.
getValueFrom
(
f
,
d
)
if
val
!=
value
{
continue
}
result
=
append
(
result
,
fmt
.
Sprintf
(
"%s%d"
,
strings
.
Map
(
letterOnlyMapF
,
colCell
.
R
),
r
.
R
))
}
}
default
:
}
}
return
result
}
// RegSearchSheet provides the ability to retrieve coordinates
// with the given worksheet name and regular expression
// For a merged cell, get the coordinates
// of the upper left corner of the merge area.
// :example)
// Search the coordinates where the numerical value in the range of "0-9" of Sheet 1 is described:
// An example of search the coordinates where the numerical value in the range
// of "0-9" of Sheet1 is described:
//
// xlsx.
RegSearchSheet("Sheet1", "[0-9]"
)
// xlsx.
SearchSheet("Sheet1", "[0-9]", true
)
//
func
(
f
*
File
)
RegSearchSheet
(
sheet
,
value
string
)
[]
string
{
func
(
f
*
File
)
SearchSheet
(
sheet
,
value
string
,
reg
...
bool
)
[]
string
{
var
regSearch
bool
for
_
,
r
:=
range
reg
{
regSearch
=
r
}
xlsx
:=
f
.
workSheetReader
(
sheet
)
result
:=
[]
string
{}
name
,
ok
:=
f
.
sheetMap
[
trimSheetName
(
sheet
)]
...
...
@@ -746,9 +706,15 @@ func (f *File) RegSearchSheet(sheet, value string) []string {
_
=
decoder
.
DecodeElement
(
&
r
,
&
startElement
)
for
_
,
colCell
:=
range
r
.
C
{
val
,
_
:=
colCell
.
getValueFrom
(
f
,
d
)
regex
:=
regexp
.
MustCompile
(
value
)
if
!
regex
.
MatchString
(
val
)
{
continue
if
regSearch
{
regex
:=
regexp
.
MustCompile
(
value
)
if
!
regex
.
MatchString
(
val
)
{
continue
}
}
else
{
if
val
!=
value
{
continue
}
}
result
=
append
(
result
,
fmt
.
Sprintf
(
"%s%d"
,
strings
.
Map
(
letterOnlyMapF
,
colCell
.
R
),
r
.
R
))
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录