summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorr-uchino <46125593+r-uchino@users.noreply.github.com>2018-12-26 14:30:59 +0900
committerxuri <xuri.me@gmail.com>2018-12-26 13:30:59 +0800
commit9b8baf75ad7613dba24635b4c66791e404c6b7d5 (patch)
tree4069b6e01db691f39cc51c78fc99335043334891 /sheet.go
parentb04107c4a374dfc0a625d1513b0e5cb1d907260b (diff)
Add RegSearchSheet (#316)
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/sheet.go b/sheet.go
index f546561..cf05fe9 100644
--- a/sheet.go
+++ b/sheet.go
@@ -18,6 +18,7 @@ import (
"io/ioutil"
"os"
"path"
+ "regexp"
"strconv"
"strings"
"unicode/utf8"
@@ -707,6 +708,57 @@ func (f *File) SearchSheet(sheet, value string) []string {
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:
+//
+// xlsx.RegSearchSheet("Sheet1", "[0-9]")
+//
+func (f *File) RegSearchSheet(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)
+ regex := regexp.MustCompile(value)
+ if !regex.MatchString(val) {
+ continue
+ }
+ result = append(result, fmt.Sprintf("%s%d", strings.Map(letterOnlyMapF, colCell.R), r.R))
+ }
+ }
+ default:
+ }
+ }
+ return result
+}
+
// ProtectSheet provides a function to prevent other users from accidentally
// or deliberately changing, moving, or deleting data in a worksheet. For
// example, protect Sheet1 with protection settings: