Skip to content

Commit 5d7bacf

Browse files
committed
Update readme
1 parent d5dd5b7 commit 5d7bacf

File tree

1 file changed

+104
-17
lines changed

1 file changed

+104
-17
lines changed

README.md

+104-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Apache poi dsl for scala
55
## Usage
66

77
```scala
8-
scala> import info.folone.scala.poi._
98
import info.folone.scala.poi._
109

1110
scala> import scalaz._
@@ -22,6 +21,99 @@ scala> import std.list._
2221
import std.list._
2322

2423
// 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+
25117
scala> val sheetOne = Workbook {
26118
Set(Sheet("name") {
27119
Set(Row(1) {
@@ -42,11 +134,8 @@ sheetOne: info.folone.scala.poi.Workbook = Workbook(Set(Sheet ("name")(Set(Row (
42134
scala> val path = "/tmp/workbook.xls"
43135
path: String = /tmp/workbook.xls
44136

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
48138

49-
// Let's create another workbook
50139
scala> val sheetTwo = Workbook {
51140
Set(Sheet("name") {
52141
Set(Row(1) {
@@ -70,36 +159,34 @@ sheetTwo: info.folone.scala.poi.Workbook = Workbook(Set(Sheet ("name")(Set(Row (
70159
scala> import syntax.equal._
71160
import syntax.equal._
72161

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
79167

80-
scala> res0.unsafePerformIO
81-
res1: Boolean = true
168+
scala> res.unsafePerformIO
169+
res5: Boolean = true
82170

83-
// The impure syntax:
84171
scala> import impure._
85172
import impure._
86173

87174
scala> sheetOne.overwrite(path)
88175

89176
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)))))))
91178

92179
scala> val mergeSheets = sheetOne |+| sheetTwo
93180
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")))))))
94181

95182
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)")))))))
97184

98185
scala> val mergeSheets2 = sheetOneReloaded |+| sheetTwo
99186
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")))))))
100187

101188
scala> mergeSheets == mergeSheets2
102-
res4: Boolean = true
189+
res8: Boolean = true
103190
```
104191

105192

0 commit comments

Comments
 (0)