Skip to content

Commit 97ebc4a

Browse files
authored
Merge pull request #80 from devlights/issue-79
Add Worksheet.UsedRange
2 parents a0a5fce + 59090d6 commit 97ebc4a

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

range.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func NewRangeFromShape(s *Shape, c *ole.IDispatch) *XlRange {
5858
return newRange(s, c)
5959
}
6060

61+
func NewRangeFromWorksheet(ws *Worksheet, c *ole.IDispatch) *XlRange {
62+
return newRange(ws, c)
63+
}
64+
6165
func (r *XlRange) ComObject() *ole.IDispatch {
6266
return r.comObj
6367
}

worksheet.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,14 @@ func (ws *Worksheet) PrintOut() error {
288288
_, err := oleutil.CallMethod(ws.ComObject(), "PrintOut", nil)
289289
return err
290290
}
291+
292+
func (ws *Worksheet) UsedRange() (*XlRange, error) {
293+
v, err := oleutil.GetProperty(ws.ComObject(), "UsedRange")
294+
if err != nil {
295+
return nil, err
296+
}
297+
298+
ra := NewRangeFromWorksheet(ws, v.ToIDispatch())
299+
300+
return ra, nil
301+
}

worksheet_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goxcel
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"testing"
@@ -9,6 +10,53 @@ import (
910
"github.com/devlights/goxcel/testutil"
1011
)
1112

13+
func TestWorksheet_UsedRange(t *testing.T) {
14+
f := MustInitGoxcel()
15+
defer f()
16+
17+
g, r := MustNewGoxcel()
18+
defer r()
19+
20+
g.MustSilent(true)
21+
22+
wbs := g.MustWorkbooks()
23+
wb, wbr := wbs.MustAdd()
24+
defer wbr()
25+
26+
ws := wb.MustSheets(1)
27+
28+
c := ws.MustCells(1, 1)
29+
c.MustSetValue(fmt.Sprintf("%v_%v", 1, 1))
30+
31+
c = ws.MustCells(100, 1)
32+
c.MustSetValue(fmt.Sprintf("%v_%v", 100, 1))
33+
34+
c = ws.MustCells(50, 100)
35+
c.MustSetValue(fmt.Sprintf("%v_%v", 50, 100))
36+
37+
// UsedRange は、書式設定されているだけのセルも範囲に入る
38+
c = ws.MustCells(200, 1)
39+
interior, _ := c.Interior()
40+
_ = interior.SetColor(constants.RgbGreen)
41+
42+
ra, err := ws.UsedRange()
43+
if err != nil {
44+
t.Error(err)
45+
}
46+
47+
rows, _ := ra.Rows()
48+
count, _ := rows.Count()
49+
if count != 200 {
50+
t.Errorf("[want] 200\t[got] %v", count)
51+
}
52+
53+
cols, _ := ra.Columns()
54+
count, _ = cols.Count()
55+
if count != 100 {
56+
t.Errorf("[want] 100\t[got] %v", count)
57+
}
58+
}
59+
1260
func TestWorksheet_MaxRowCol(t *testing.T) {
1361
f := MustInitGoxcel()
1462
defer f()
@@ -30,6 +78,11 @@ func TestWorksheet_MaxRowCol(t *testing.T) {
3078
c = ws.MustCells(100, 1)
3179
c.MustSetValue("world")
3280

81+
// MaxRow, MaxCol, MaxRowCol は、書式設定されているだけのセルは範囲に入れない
82+
c = ws.MustCells(200, 1)
83+
interior, _ := c.Interior()
84+
_ = interior.SetColor(constants.RgbGreen)
85+
3386
maxRow, maxCol, err := ws.MaxRowCol(1, 1)
3487
if err != nil {
3588
t.Error(err)

0 commit comments

Comments
 (0)