@@ -5,7 +5,6 @@ Apache poi dsl for scala
5
5
## Usage
6
6
7
7
``` scala
8
- scala> import info .folone .scala .poi ._
9
8
import info .folone .scala .poi ._
10
9
11
10
scala> import scalaz ._
@@ -22,6 +21,99 @@ scala> import std.list._
22
21
import std .list ._
23
22
24
23
// Creating a test workbook
24
+ scala> val sheetOne = Workbook {
25
+ | Set (Sheet (" name" ) {
26
+ | Set (Row (1 ) {
27
+ | Set (NumericCell (1 , 13.0 / 5 ), FormulaCell (2 , " ABS(A1)" ))
28
+ | },
29
+ | Row (2 ) {
30
+ | Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))
31
+ | })
32
+ | },
33
+ | Sheet (" name2" ) {
34
+ | Set (Row (2 ) {
35
+ | Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ))
36
+ | })
37
+ | })
38
+ | }
39
+ sheetOne : info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
40
+
41
+ scala> val path = " /tmp/workbook.xls"
42
+ path: String = / tmp/ workbook.xls
43
+
44
+ // Saving the result (yes, it does control side-effects via scalaz.IO)
45
+ scala> sheetOne.safeToFile(path).fold(ex ⇒ throw ex, identity).unsafePerformIO
46
+
47
+ // Let's create another workbook
48
+ scala> val sheetTwo = Workbook {
49
+ | Set (Sheet (" name" ) {
50
+ | Set (Row (1 ) {
51
+ | Set (StringCell (1 , " newdata" ), StringCell (2 , " data2" ), StringCell (3 , " data3" ))
52
+ | },
53
+ | Row (2 ) {
54
+ | Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))
55
+ | },
56
+ | Row (3 ) {
57
+ | Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))
58
+ | })
59
+ | },
60
+ | Sheet (" name" ) {
61
+ | Set (Row (2 ) {
62
+ | Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))
63
+ | })
64
+ | })
65
+ | }
66
+ sheetTwo: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (StringCell (1 , " newdata" ), StringCell (2 , " data2" ), StringCell (3 , " data3" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))), Row (3 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name" )(Set (Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" )))))))
67
+
68
+ scala> import syntax .equal ._
69
+ import syntax .equal ._
70
+
71
+ // And let's merge the saved workbook from before with the new one using the Monoid instance
72
+ scala> val res = Workbook (path).fold(
73
+ | ex => false ,
74
+ | workbook => (workbook |+| sheetTwo) === (sheetOne |+| sheetTwo)
75
+ | )
76
+
77
+ scala> res.unsafePerformIO
78
+ res1: Boolean = true
79
+
80
+ // The impure syntax:
81
+ scala> import impure ._
82
+ import impure ._
83
+
84
+ scala> sheetOne.overwrite(path)
85
+
86
+ scala> sheetOne
87
+ res3: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
88
+
89
+ scala> val mergeSheets = sheetOne |+| sheetTwo
90
+ mergeSheets: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ))))), Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" )))))))
91
+
92
+ scala> val sheetOneReloaded = load(path)
93
+ sheetOneReloaded: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
94
+
95
+ scala> val mergeSheets2 = sheetOneReloaded |+| sheetTwo
96
+ mergeSheets2: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ))))), Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" )))))))
97
+
98
+ scala> mergeSheets == mergeSheets2
99
+ res4: Boolean = true
100
+ // Replaying 19 commands from transcript.
101
+
102
+ scala> import info .folone .scala .poi ._
103
+ import info .folone .scala .poi ._
104
+
105
+ scala> import scalaz ._
106
+ import scalaz ._
107
+
108
+ scala> import syntax .monoid ._
109
+ import syntax .monoid ._
110
+
111
+ scala> import syntax .foldable ._
112
+ import syntax .foldable ._
113
+
114
+ scala> import std .list ._
115
+ import std .list ._
116
+
25
117
scala> val sheetOne = Workbook {
26
118
Set (Sheet (" name" ) {
27
119
Set (Row (1 ) {
@@ -42,11 +134,8 @@ sheetOne: info.folone.scala.poi.Workbook = Workbook(Set(Sheet ("name")(Set(Row (
42
134
scala> val path = " /tmp/workbook.xls"
43
135
path: String = / tmp/ workbook.xls
44
136
45
- // Saving the result (yes, it does control side-effects via scalaz.IO)
46
- scala> sheetOne.safeToFile(path).unsafePerformIO
47
- res0: scalaz.\/ [Throwable ,Unit ] = \/- (())
137
+ scala> sheetOne.safeToFile(path).fold(ex ⇒ throw ex, identity).unsafePerformIO
48
138
49
- // Let's create another workbook
50
139
scala> val sheetTwo = Workbook {
51
140
Set (Sheet (" name" ) {
52
141
Set (Row (1 ) {
@@ -70,36 +159,34 @@ sheetTwo: info.folone.scala.poi.Workbook = Workbook(Set(Sheet ("name")(Set(Row (
70
159
scala> import syntax .equal ._
71
160
import syntax .equal ._
72
161
73
- // And let's merge the saved workbook from before with the new one using the Monoid instance
74
- scala> val res0 = { Workbook (path).map {
75
- case \/- (workbook) => (workbook |+| sheetTwo) === (sheetOne |+| sheetTwo)
76
- case -\/ (_) => false
77
- } }
78
- res0: scalaz.effect.IO [Boolean ] = scalaz.effect.IOFunctions $$anon$5 @ 5fc1a208
162
+ scala> val res = Workbook (path).fold(
163
+ ex => false ,
164
+ workbook => (workbook |+| sheetTwo) === (sheetOne |+| sheetTwo)
165
+ )
166
+ res: scalaz.effect.IO [Boolean ] = scalaz.effect.IOFunctions $$anon$5 @ 7ad4ad93
79
167
80
- scala> res0 .unsafePerformIO
81
- res1 : Boolean = true
168
+ scala> res .unsafePerformIO
169
+ res5 : Boolean = true
82
170
83
- // The impure syntax:
84
171
scala> import impure ._
85
172
import impure ._
86
173
87
174
scala> sheetOne.overwrite(path)
88
175
89
176
scala> sheetOne
90
- res3 : info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
177
+ res7 : info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
91
178
92
179
scala> val mergeSheets = sheetOne |+| sheetTwo
93
180
mergeSheets: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ))))), Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" )))))))
94
181
95
182
scala> val sheetOneReloaded = load(path)
96
- sheetOneReloaded: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name " )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1) " ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))))), Sheet ( " name2 " )( Set ( Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 )))))))
183
+ sheetOneReloaded: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name2 " )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ) )))), Sheet ( " name " )( Set ( Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" ))), Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1) " )))))))
97
184
98
185
scala> val mergeSheets2 = sheetOneReloaded |+| sheetTwo
99
186
mergeSheets2: info.folone.scala.poi.Workbook = Workbook (Set (Sheet (" name2" )(Set (Row (2 )(Set (BooleanCell (1 , true ), NumericCell (2 , 2.4 ))))), Sheet (" name" )(Set (Row (1 )(Set (NumericCell (1 , 2.6 ), FormulaCell (2 , " =ABS(A1)" ))), Row (2 )(Set (StringCell (1 , " data" ), StringCell (2 , " data2" )))))))
100
187
101
188
scala> mergeSheets == mergeSheets2
102
- res4 : Boolean = true
189
+ res8 : Boolean = true
103
190
```
104
191
105
192
0 commit comments