Skip to content

Commit 61fa415

Browse files
committed
Update docs. Bugfix related to cell fill
1 parent c052c56 commit 61fa415

File tree

16 files changed

+540
-63
lines changed

16 files changed

+540
-63
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ asciidoctor {
137137
'groupId': project.group,
138138
'artifactId': project.name,
139139
'sourcedir': "${projectDir}/src/main/groovy",
140+
'poiApiUrl': 'https://poi.apache.org/apidocs/',
140141
'projectUrl': project.githubUrl
141142
}
142143

docs/cells.adoc

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@ This module supports setting basic and custom types to cell values. The followin
88
* String
99
* Calendar
1010
* Date
11-
* Double
11+
* Number
1212
* Boolean
1313

1414
Support for additional types can be easily added without the need to convert your custom objects each time.
1515

16+
=== Underlying API
17+
18+
To get access to the Apache POI API for a given cell, simply store the return value of the `cell` method:
19+
20+
[source,groovy]
21+
----
22+
import org.apache.poi.xssf.usermodel.XSSFCell
23+
24+
ExcelBuilder.build {
25+
sheet {
26+
row {
27+
XSSFCell customCell = cell("Foo")
28+
//Modify customCell as needed
29+
}
30+
}
31+
}
32+
----
33+
1634
=== Custom Renderers
1735

1836
You can register a custom cell renderer to marshal your custom type into a type that is supported by Excel.
@@ -48,9 +66,27 @@ ExcelBuilder.build {
4866
}
4967
----
5068

69+
IMPORTANT: If a renderer is registered for a superclass, it will resolve for subclasses. If you wish to provide customized functionality for a subclass you can register a renderer for that class with a higher priority. The default is `-1`.
70+
71+
[source,groovy]
72+
----
73+
class Foo {
74+
String name
75+
}
76+
class Bar extends Foo {
77+
String title
78+
}
79+
80+
Excel.registerCellRenderer(Foo) { it.name }
81+
82+
// With the current behavior, all values of type "Foo" or any subclass of "Foo" will be rendered with the "name" property. If you wish to override the rendering for a given type, you can specify another renderer with a higher priority.
83+
84+
Excel.registerCellRenderer(Bar, 0) { it.title }
85+
----
86+
5187
=== Cell Styling
5288

53-
All calls to `cell` can be accompanied with a map of arguments to style the cell. The arguments passed will be merged with the default styles of the row and sheet, if provided
89+
All calls to `cell` can be accompanied with a map of arguments to style the cell. The arguments passed will be merged with the default styles of the row and sheet, if provided.
5490

5591
[source,groovy]
5692
----
@@ -67,4 +103,83 @@ ExcelBuilder.build {
67103

68104
See the section in this guide on link:#styles[styling cells] to learn what options are available.
69105

106+
=== Cell Formatting
107+
108+
It is not necessary for you to pre format your data before creating cells. You can apply a format to any given class and that format will be used when data of that type is written to a cell. There are several data types that are already configured. You can override that configuration and add your own.
109+
110+
The following classes to format mappings are created by default.
111+
112+
[width="75%"]
113+
|=======
114+
|Class |Format
115+
116+
|java.math.BigDecimal |`$\#,\##0.00_);[Red]($\#,##0.00)`
117+
|java.lang.Double |`\#,##0.00`
118+
|java.lang.Float |`\#,##0.00`
119+
|java.lang.Integer |`\#,##0`
120+
|java.lang.Long |`\#,##0`
121+
|java.lang.Short |`\#,##0`
122+
|java.math.BigInteger |`\#,##0`
123+
|java.util.Date |`m/d/yyyy`
124+
|=======
125+
126+
To override or add additional format mappings, register them with the link:./groovydoc/com/jameskleeh/excel/Excel.html[Excel] class.
127+
128+
[source,groovy]
129+
----
130+
import com.jameskleeh.excel.Excel
131+
import java.time.OffsetDateTime
132+
133+
//Override the existing format for BigDecimal
134+
Excel.registerCellFormat(BigDecimal, "\"$\"#,##0.00_);(\"$\"#,##0.00)")
135+
136+
//Create a new format mapping
137+
Excel.registerCellFormat(OffsetDateTime, "m/d/yy h:mm")
138+
----
139+
140+
There are a list of built in formats you can reference by their index. The list is documented in a comment in the Apache POI {poiApiUrl}org/apache/poi/ss/usermodel/BuiltinFormats.html[BuiltinFormats] class.
141+
142+
To register one of the built in formats, simply use the `Integer` representation.
143+
144+
[source,groovy]
145+
----
146+
import com.jameskleeh.excel.Excel
147+
148+
Excel.registerCellFormat(Float, 10) // "0.00%"
149+
----
150+
151+
In addition to providing global formats by class, you can also override the format for any given cell.
152+
153+
[source,groovy]
154+
----
155+
ExcelBuilder.build {
156+
sheet {
157+
row {
158+
cell(0.105F, [format: 10])
159+
}
160+
}
161+
}
162+
----
163+
164+
The following result will be produced.
165+
166+
[width="15%"]
167+
|=======
168+
| |A
169+
170+
|*1* |10.50%
171+
|=======
172+
173+
IMPORTANT: If a format is registered for a superclass, it will resolve for subclasses. If you wish to provide customized functionality for a subclass you can register a format for that class with a higher priority. The default is `-1`.
174+
175+
[source,groovy]
176+
----
177+
import java.time.temporal.TemporalAccessor
178+
import java.time.LocalTime
179+
180+
Excel.registerCellFormat(TemporalAccessor, "m/d/yy h:mm")
181+
182+
// With the current behavior, all Java 8 date types will be rendered with the supplied format. If you wish to override the format for a given type, you can specify another format with a higher priority.
70183
184+
Excel.registerCellFormat(LocalTime, 0, "h:mm:ss")
185+
----

docs/formulas.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[[formuals]]
22
== Formulas
33

4-
The Groovy Excel Builder supports formulas in excel documents by providing an easy way to create formulas with support for helper methods to get cell references
4+
The Groovy Excel Builder supports formulas in excel documents by providing an easy way to create formulas with support for helper methods to get cell references.
55

66
=== Simple String
77

8-
You can specify the exact text of your formula using the example below
8+
You can specify the exact text of your formula using the example below.
99

1010
[source,groovy]
1111
----
@@ -20,7 +20,7 @@ ExcelBuilder.build {
2020

2121
=== Closure Customization
2222

23-
An additional way to create a formula is to supply a closure to the `formula` method. The provided closure will have access to a few methods to make referencing cells easier. Whatever gets returned from the closure will be set as the formula
23+
An additional way to create a formula is to supply a closure to the `formula` method. The provided closure will have access to a few methods to make referencing cells easier. Whatever gets returned from the closure will be set as the formula.
2424

2525
[source,groovy]
2626
----
@@ -37,7 +37,7 @@ ExcelBuilder.build {
3737

3838
==== Exact Cell Reference
3939

40-
Inside of the forumla closure you can get a reference to an exact cell by providing the index of the row and column
40+
Inside of the formula closure you can get a reference to an exact cell by providing the index of the row and column.
4141

4242
[source,groovy]
4343
----
@@ -72,7 +72,7 @@ The first call to exact cell will return "A1" because the `foo` column is in col
7272

7373
==== Relative Cell References
7474

75-
In addition to retrieving exact cell references, you can also retrieve references relative to the formula
75+
In addition to retrieving exact cell references, you can also retrieve references relative to the formula.
7676

7777
[source,groovy]
7878
----
@@ -91,9 +91,9 @@ ExcelBuilder.build {
9191

9292
The first call to `relativeCell` will reference the cell in the previous column in the same row. In that example that would be "A2". The second call to `relativeCell` will resolve to "A1" because the first argument indicates to go back 1 column and the second argument says to go back one row. The formula will be put in cell "B2", so minus one column and minus one row would be "A1".
9393

94-
==== Anchoring Cells
94+
=== Anchoring Cells
9595

96-
Once you have a reference to any cell, you can anchor the column or row or both
96+
Once you have a reference to any cell, you can anchor the column or row or both.
9797

9898
[source,groovy]
9999
----
@@ -105,6 +105,6 @@ formula {
105105
...
106106
----
107107

108-
The resulting formula will be
108+
The resulting formula will be:
109109

110110
`SUM($A2, A$3, $A$4)`

docs/gettingStarted.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ To get started using this great module, include it into your build:
2121
compile '{groupId}:{artifactId}:{version}'
2222
----
2323

24+
TIP: If you want to use a snapshot version, add the jfrog snapshot repository to your build: http://oss.jfrog.org/oss-snapshot-local. Normal releases are available in maven central and jcenter.
25+
2426
=== Creating your first Excel document
2527

26-
Here is the simplest example to create an Excel document
28+
Here is the simplest example to create an Excel document:
2729

2830
[source,groovy]
2931
----

docs/rows.adoc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
[[rows]]
22
== Rows
33

4-
In addition to creating cells, there are also a few other methods available to you to make documents easier
4+
In addition to creating cells, there are also a few other methods available to you to make documents easier.
5+
6+
=== Underlying API
7+
8+
To get access to the Apache POI API for a given row, simply receive an argument to the `row` closure:
9+
10+
[source,groovy]
11+
----
12+
import org.apache.poi.xssf.usermodel.XSSFRow
13+
14+
ExcelBuilder.build {
15+
sheet {
16+
row { XSSFRow myRow ->
17+
//Modify myRow as needed
18+
}
19+
}
20+
}
21+
----
522

623
=== Defaults
724

@@ -34,7 +51,7 @@ ExcelBuilder.build {
3451
}
3552
----
3653

37-
The previous example will produce a document with "foo" in cell A1 and "bar" in D1
54+
The previous example will produce a document with "foo" in cell A1 and "bar" in D1.
3855

3956
==== Skipping to a column
4057

@@ -58,7 +75,7 @@ ExcelBuilder.build {
5875

5976
The example above will put the text "C2" in cell C2.
6077

61-
WARNING: The `skipTo` method will set the cell index so the next cell call will output data into the desired cell. Subsequent cells will start from that index. View the example below
78+
WARNING: The `skipTo` method will set the cell index so the next cell call will output data into the desired cell. Subsequent cells will start from that index. View the example below:
6279

6380
[source,groovy]
6481
----

docs/sheets.adoc

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
[[sheets]]
22
== Sheets
33

4-
With the Groovy Excel Builder, you can create documents with as many sheets as you like. In addition, there are several options you have at the sheet level for configuring your document
4+
With the Groovy Excel Builder, you can create documents with as many sheets as you like. In addition, there are several options you have at the sheet level for configuring your document.
5+
6+
=== Underlying API
7+
8+
To get access to the Apache POI API for a given sheet, simply receive an argument to the `sheet` closure:
9+
10+
[source,groovy]
11+
----
12+
import org.apache.poi.xssf.usermodel.XSSFSheet
13+
14+
ExcelBuilder.build {
15+
sheet { XSSFSheet mySheet ->
16+
//Modify mySheet as needed
17+
}
18+
}
19+
----
520

621
=== Sheet Name
722

8-
You can configure what name you would like to give each sheet by executing the `sheet` method with a `String` argument
23+
You can configure what name you would like to give each sheet by executing the `sheet` method with a `String` argument.
924

1025
[source,groovy]
1126
----
@@ -17,7 +32,7 @@ ExcelBuilder.build {
1732

1833
=== Defaults
1934

20-
You can configure defaults for column width and row height in a given sheet by passing a `Map` to the `sheet` method
35+
You can configure defaults for column width and row height in a given sheet by passing a `Map` to the `sheet` method.
2136

2237
[source,groovy]
2338
----
@@ -35,7 +50,7 @@ The width must be an `Integer` and the height can be a `Float` if you want to se
3550

3651
=== Styling
3752

38-
You can set default styles for a sheet by passing a map of options into the `defaultStyle` method. To see what options are available, visit the link:#styles[Styles] section of this document
53+
You can set default styles for a sheet by passing a map of options into the `defaultStyle` method. To see what options are available, visit the link:#styles[Styles] section of this document.
3954

4055
[source,groovy]
4156
----
@@ -48,7 +63,7 @@ ExcelBuilder.build {
4863

4964
=== Header Row
5065

51-
It is possible to define a header row and assign an identifier to each cell for later use
66+
It is possible to define a header row and assign an identifier to each cell for later use.
5267

5368
[source,groovy]
5469
----
@@ -62,11 +77,11 @@ ExcelBuilder.build {
6277
}
6378
----
6479

65-
In future rows, you can do things like skip to a given column or reference that column in a formula. The first argument is assigned to the cell value and the second argument is the unique identifier
80+
In future rows, you can do things like skip to a given column or reference that column in a formula. The first argument is assigned to the cell value and the second argument is the unique identifier.
6681

6782
=== Creating Rows
6883

69-
You can easily create rows with the following syntax
84+
You can easily create rows with the following syntax:
7085

7186
[source,groovy]
7287
----
@@ -90,11 +105,11 @@ ExcelBuilder.build {
90105
}
91106
----
92107

93-
To reference everything you can do in a row, see the section in the documentation on link:#rows[Rows]
108+
To reference everything you can do in a row, see the section in the documentation on link:#rows[Rows].
94109

95110
=== Skipping Rows
96111

97-
It is very easy to skip any number of rows. See the following example
112+
It is very easy to skip any number of rows. See the following example:
98113

99114
[source,groovy]
100115
----
@@ -107,7 +122,7 @@ ExcelBuilder.build {
107122
}
108123
----
109124

110-
The above example will produce a sheet that looks something like this
125+
The above example will produce a sheet that looks something like this:
111126

112127
[width="50%"]
113128
|=======

0 commit comments

Comments
 (0)