Skip to content

Commit d299a02

Browse files
authored
Merge pull request #92 from devlights/add-workbook-mustsave
Add Workbook.{MustSave,MustSaveAs}
2 parents e2dce2f + e077fb1 commit d299a02

File tree

7 files changed

+61
-19
lines changed

7 files changed

+61
-19
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func run() (int, string) {
4848
// 1. Create new Goxcel instance.
4949
excel, release := goxcel.MustNewGoxcel()
5050

51-
// must call goxcel's release function when function exited
51+
// must call goxcel release function when function exited
5252
// otherwise excel process was remained.
5353
defer release()
5454

@@ -62,7 +62,7 @@ func run() (int, string) {
6262
// 3. Add Workbook
6363
wb, wbRelease := wbs.MustAdd()
6464

65-
// call workbook's release funciton
65+
// call workbook's release function
6666
defer wbRelease()
6767

6868
// 4. Get Worksheet
@@ -78,10 +78,7 @@ func run() (int, string) {
7878
log.Printf("SAVE FILE: %s\n", p)
7979

8080
// 7. Save
81-
if err := wb.SaveAs(p); err != nil {
82-
log.Println(err)
83-
return 7, ""
84-
}
81+
wb.MustSaveAs(p)
8582

8683
// Workbook::SetSaved(true) and Workbook::Close() is automatically called when `defer wbReleaseFn()`.
8784
// Excel::Quit() and Excel::Release() is automatically called when `defer release()`.

examples/must_methods/helloworld_use_mustmethods.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func run() (int, string) {
3434
// 1. Create new Goxcel instance.
3535
g, goxcelReleaseFn := goxcel.MustNewGoxcel()
3636

37-
// must call goxcel's release function when function exited
37+
// must call goxcel release function when function exited
3838
// otherwise excel process was remained.
3939
defer goxcelReleaseFn()
4040

@@ -48,7 +48,7 @@ func run() (int, string) {
4848
// 3. Add Workbook
4949
wb, wbReleaseFn := wbs.MustAdd()
5050

51-
// call workbook's release funciton
51+
// call workbook's release function
5252
defer wbReleaseFn()
5353

5454
// 4. Get Worksheet
@@ -67,10 +67,7 @@ func run() (int, string) {
6767
log.Printf("SAVE FILE: %s\n", p)
6868

6969
// 7. Save
70-
if err := wb.SaveAs(p); err != nil {
71-
log.Println(err)
72-
return 7, ""
73-
}
70+
wb.MustSaveAs(p)
7471

7572
// Workbook::SetSaved(true) and Workbook::Close() is automatically called when `defer wbReleaseFn()`.
7673
// Excel::Quit() and Excel::Release() is automatically called when `defer goxcelReleaseFn()`.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/go-ole/go-ole v1.3.0
77
github.com/skanehira/clipboard-image/v2 v2.0.0
8-
golang.org/x/sync v0.3.0
8+
golang.org/x/sync v0.6.0
99
)
1010

11-
require golang.org/x/sys v0.11.0 // indirect
11+
require golang.org/x/sys v0.18.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ github.com/skanehira/clipboard-image/v2 v2.0.0 h1:Kp+RNOgIlgzDkP3EskwuBnM0Fk4sc+
44
github.com/skanehira/clipboard-image/v2 v2.0.0/go.mod h1:NXSYl4FJinIUFKJfeP1lGz8DIEUYjnEqwdMZ777S1E0=
55
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
66
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
7+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
8+
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
79
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
810
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
911
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
13+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

testutil/testutil.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testutil
22

3-
import "time"
3+
import (
4+
"time"
5+
)
46

57
func Interval() {
68
time.Sleep(3 * time.Second)

workbook.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,24 @@ func (w *Workbook) Save() error {
9696
return err
9797
}
9898

99+
func (w *Workbook) MustSave() {
100+
err := w.Save()
101+
if err != nil {
102+
panic(err)
103+
}
104+
}
105+
99106
func (w *Workbook) SaveAs(filePath string) error {
100107
return w.SaveAsWithFileFormat(filePath, constants.XlOpenXMLWorkbook)
101108
}
102109

110+
func (w *Workbook) MustSaveAs(filePath string) {
111+
err := w.SaveAs(filePath)
112+
if err != nil {
113+
panic(err)
114+
}
115+
}
116+
103117
func (w *Workbook) SaveAsWithFileFormat(filePath string, format constants.XlFileFormat) error {
104118
_, err := oleutil.CallMethod(w.ComObject(), "SaveAs", filePath, int(format))
105119
return err

workbook_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import (
88
"github.com/devlights/goxcel/testutil"
99
)
1010

11+
func createExcelBook(xlsxPath string) {
12+
var err error
13+
14+
if _, err = os.Stat(xlsxPath); !os.IsNotExist(err) {
15+
err = os.Remove(xlsxPath)
16+
if err != nil {
17+
panic(err)
18+
}
19+
}
20+
21+
quit := MustInitGoxcel()
22+
defer quit()
23+
24+
excel, release := MustNewGoxcel()
25+
defer release()
26+
27+
excel.MustSilent(false)
28+
wbs := excel.MustWorkbooks()
29+
wb, wbRelease := wbs.MustAdd()
30+
defer wbRelease()
31+
32+
wb.MustSaveAs(xlsxPath)
33+
}
34+
1135
func TestWorkbook_MustMethods(t *testing.T) {
1236
f := MustInitGoxcel()
1337
defer f()
@@ -39,6 +63,10 @@ func TestWorkbook_Save(t *testing.T) {
3963
testutil.Interval()
4064
defer testutil.Interval()
4165

66+
userHomeDir, _ := os.UserHomeDir()
67+
xlsxPath := filepath.Join(userHomeDir, "Book1.xlsx")
68+
createExcelBook(xlsxPath)
69+
4270
g, r, err := NewGoxcel()
4371

4472
if err != nil {
@@ -55,8 +83,6 @@ func TestWorkbook_Save(t *testing.T) {
5583
t.Error(err)
5684
}
5785

58-
userHomeDir, _ := os.UserHomeDir()
59-
xlsxPath := filepath.Join(userHomeDir, "Book1.xlsx")
6086
wb, wbReleaseFn, _ := wbs.Open(xlsxPath)
6187
defer wbReleaseFn()
6288

@@ -76,6 +102,10 @@ func TestWorkbook_SaveAs(t *testing.T) {
76102
testutil.Interval()
77103
defer testutil.Interval()
78104

105+
userHomeDir, _ := os.UserHomeDir()
106+
srcXlsxPath := filepath.Join(userHomeDir, "Book1.xlsx")
107+
createExcelBook(srcXlsxPath)
108+
79109
g, r, err := NewGoxcel()
80110

81111
if err != nil {
@@ -92,8 +122,6 @@ func TestWorkbook_SaveAs(t *testing.T) {
92122
t.Error(err)
93123
}
94124

95-
userHomeDir, _ := os.UserHomeDir()
96-
srcXlsxPath := filepath.Join(userHomeDir, "Book1.xlsx")
97125
wb, wbReleaseFn, _ := wbs.Open(srcXlsxPath)
98126
defer wbReleaseFn()
99127

0 commit comments

Comments
 (0)