Skip to content

Commit 29517e0

Browse files
authored
Merge pull request #93 from devlights/add-range-find
Add Range.Find(), Range.FindNext(), Range.FindPrevious()
2 parents d299a02 + 534db17 commit 29517e0

File tree

6 files changed

+147
-9
lines changed

6 files changed

+147
-9
lines changed

constants/xlFindLookIn.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type (
1212
//
1313
// noinspection GoUnusedConst
1414
const (
15-
XlComments XlFindLookIn = -4144 //コメント
16-
XlFormulas XlFindLookIn = -4123 //数式
17-
XlValues XlFindLookIn = -4163 //値
15+
XlFindLookInComments XlFindLookIn = -4144 //コメント
16+
XlFindLookInFormulas XlFindLookIn = -4123 //数式
17+
XlFindLookInValues XlFindLookIn = -4163 //値
18+
XlFindLookInCommentsThreaded XlFindLookIn = -4184 //スレッド化されたコメント
1819
)

constants/xlLookAt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ type (
1212
//
1313
// noinspection GoUnusedConst
1414
const (
15-
XlPart XlLookAt = 2 //検索テキストの一部を検索します。
16-
XlWhole XlLookAt = 1 //検索テキスト全体を検索します。
15+
XlLookAtPart XlLookAt = 2 //検索テキストの一部を検索します。
16+
XlLookAtWhole XlLookAt = 1 //検索テキスト全体を検索します。
1717
)

constants/xlSearchDirection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ type (
1212
//
1313
// noinspection GoUnusedConst
1414
const (
15-
XlNext XlSearchDirection = 1 //範囲内で、一致する次の値を検索します。
16-
XlPrevious XlSearchDirection = 2 //範囲内で、一致する前の値を検索します。
15+
XlSearchDirectionNext XlSearchDirection = 1 //範囲内で、一致する次の値を検索します。
16+
XlSearchDirectionPrevious XlSearchDirection = 2 //範囲内で、一致する前の値を検索します。
1717
)

constants/xlSearchOrder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ type (
1212
//
1313
// noinspection GoUnusedConst
1414
const (
15-
XlByColumns XlSearchOrder = 2 //列を下方向に検索してから、次の列に移動します。
16-
XlByRows XlSearchOrder = 1 //行を横方向に検索してから、次の行に移動します。
15+
XlSearchOrderByColumns XlSearchOrder = 2 //列を下方向に検索してから、次の列に移動します。
16+
XlSearchOrderByRows XlSearchOrder = 1 //行を横方向に検索してから、次の行に移動します。
1717
)

range.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,56 @@ func (r *XlRange) Walk(walkFn func(r *XlRange, c *Cell, row, col int) error) (*C
308308

309309
return nil, nil
310310
}
311+
312+
func (r *XlRange) Find(what string, after *Cell) (*XlRange, bool, error) {
313+
var (
314+
lookIn = constants.XlFindLookInValues
315+
lookAt = constants.XlLookAtPart
316+
searchOrder = constants.XlSearchOrderByRows
317+
searchDirection = constants.XlSearchDirectionNext
318+
matchCase = false
319+
matchByte = true
320+
)
321+
result, err := oleutil.CallMethod(r.ComObject(), "Find", what, after.ComObject(), int32(lookIn), int32(lookAt), int32(searchOrder), int32(searchDirection), matchCase, matchByte)
322+
if err != nil {
323+
return nil, false, err
324+
}
325+
326+
dispatch := result.ToIDispatch()
327+
if dispatch == nil {
328+
return nil, false, nil
329+
}
330+
331+
newR := NewRangeFromRange(r, dispatch)
332+
return newR, true, nil
333+
}
334+
335+
func (r *XlRange) FindNext(after *Cell) (*XlRange, bool, error) {
336+
result, err := oleutil.CallMethod(r.ComObject(), "FindNext", after.ComObject())
337+
if err != nil {
338+
return nil, false, err
339+
}
340+
341+
dispatch := result.ToIDispatch()
342+
if dispatch == nil {
343+
return nil, false, nil
344+
}
345+
346+
newR := NewRangeFromRange(r, dispatch)
347+
return newR, true, nil
348+
}
349+
350+
func (r *XlRange) FindPrevious(before *Cell) (*XlRange, bool, error) {
351+
result, err := oleutil.CallMethod(r.ComObject(), "FindPrevious", before.ComObject())
352+
if err != nil {
353+
return nil, false, err
354+
}
355+
356+
dispatch := result.ToIDispatch()
357+
if dispatch == nil {
358+
return nil, false, nil
359+
}
360+
361+
newR := NewRangeFromRange(r, dispatch)
362+
return newR, true, nil
363+
}

range_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,90 @@ import (
77
"testing"
88
)
99

10+
func TestXlRange_Find(t *testing.T) {
11+
quit := MustInitGoxcel()
12+
defer quit()
13+
14+
excel, release := MustNewGoxcel()
15+
defer release()
16+
17+
wbs := excel.MustWorkbooks()
18+
wb, wbr := wbs.MustAdd()
19+
defer wbr()
20+
21+
ws := wb.MustSheets(1)
22+
23+
rows := []int{1, 2, 3, 4, 5}
24+
for _, row := range rows {
25+
cols := []int{1, 2, 3, 4, 5}
26+
for _, col := range cols {
27+
c, _ := ws.Cells(row, col)
28+
c.MustSetValue(fmt.Sprintf("%v,%v", row, col))
29+
}
30+
}
31+
32+
//
33+
// Range.Find
34+
//
35+
rng, _ := ws.UsedRange()
36+
after, _ := rng.Cells(1, 1)
37+
foundRange, found, err := rng.Find("1,", after)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
if !found {
43+
t.Fatalf("expected range to be found, got nothing")
44+
}
45+
46+
value, err := foundRange.Value()
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
t.Log(value)
52+
53+
//
54+
// Range.FindNext
55+
//
56+
after, _ = foundRange.Cells(1, 1)
57+
foundRange, found, err = rng.FindNext(after)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
if !found {
63+
t.Fatalf("expected range to be found, got nothing 2")
64+
}
65+
66+
value, err = foundRange.Value()
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
71+
t.Log(value)
72+
73+
//
74+
// Range.FindPrevious
75+
//
76+
before, _ := foundRange.Cells(1, 1)
77+
foundRange, found, err = rng.FindPrevious(before)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
if !found {
83+
t.Fatalf("expected range to be found, got nothing 3")
84+
}
85+
86+
value, err = foundRange.Value()
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
t.Log(value)
92+
}
93+
1094
func TestXlRange_CopyPicture(t *testing.T) {
1195
quit := MustInitGoxcel()
1296
defer quit()

0 commit comments

Comments
 (0)