Skip to content

Commit fbab0ec

Browse files
committed
Support POI 3.16. Add/update documentation
1 parent 8756205 commit fbab0ec

File tree

9 files changed

+112
-17
lines changed

9 files changed

+112
-17
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ apply plugin: "jacoco"
1414

1515

1616
group 'com.jameskleeh'
17-
version '0.3.0'
17+
version '0.4.0'
1818

1919
targetCompatibility = 1.7
2020

@@ -26,7 +26,7 @@ repositories {
2626

2727
dependencies {
2828
provided 'org.codehaus.groovy:groovy-all:2.4.7'
29-
compile "org.apache.poi:poi-ooxml:3.14"
29+
compile "org.apache.poi:poi-ooxml:3.16"
3030
codenarc 'org.codenarc:CodeNarc:0.25.2'
3131
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
3232
testRuntime "org.slf4j:slf4j-api:1.7.10"

docs/formulas.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,16 @@ formula {
107107

108108
The resulting formula will be:
109109

110-
`SUM($A2, A$3, $A$4)`
110+
`SUM($A2, A$3, $A$4)`
111+
112+
=== Formula Styling
113+
114+
Formulas support a Map parameter that is the styling options
115+
116+
[source,groovy]
117+
----
118+
formula("CONCATENATE(A1,A2)", [font: Font.BOLD])
119+
formula([font: Font.BOLD]) {
120+
...
121+
}
122+
----

docs/index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ include::columns.adoc[]
1313

1414
include::cells.adoc[]
1515

16+
include::merging.adoc[]
17+
1618
include::links.adoc[]
1719

1820
include::formulas.adoc[]

docs/introduction.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33

44
This module exposes a Groovy DSL to create real Excel documents using {apachePoi}[Apache POI] under the hood. The goal of this module is to abstract away the most common uses of {apachePoi}[Apache POI] to make creating Excel documents very easy, while providing a hook into the underlying API to allow users to do anything that is not provided automatically.
55

6-
If you encounter any issues or inconsistencies while using this library, or if you have a great idea for improving this library, please https://github.com/jameskleeh/groovy-excel-builder/issues[create an issue]!
6+
If you encounter any issues or inconsistencies while using this library, or if you have a great idea for improving this library, please https://github.com/jameskleeh/groovy-excel-builder/issues[create an issue]!
7+
8+
=== Common Problems
9+
10+
If you encounter a class not found exception for `org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExtensionList`, try adding `org.apache.poi:ooxml-schemas:1.3` and `org.apache.xmlbeans:xmlbeans:2.6.0` to your dependencies.

docs/merging.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[[merging]]
2+
== Merging Cells
3+
4+
This module supports merging cells with an easy to use API.
5+
6+
[source,groovy]
7+
----
8+
row {
9+
merge {
10+
//Anything you can do inside row directly
11+
cell("Foo")
12+
cell("Bar")
13+
}
14+
}
15+
----
16+
17+
You can also apply default styling to the merged cells
18+
19+
[source,groovy]
20+
----
21+
row {
22+
merge(font: Font.BOLD) {
23+
//Anything you can do inside row directly
24+
cell("Foo")
25+
cell("Bar")
26+
}
27+
}
28+
----
29+
30+
If you only have one piece of data, but want that data to account for multiple columns, you can pass a number to the merge method.
31+
32+
[source,groovy]
33+
----
34+
row {
35+
//The value "Foo" will span 3 columns
36+
merge("Foo", 3)
37+
}
38+
----
39+
40+
You can also apply default styling to the merged cells created in that way
41+
42+
[source,groovy]
43+
----
44+
row {
45+
merge("Foo", 3, [font: Font.BOLD])
46+
}
47+
----

docs/styles.adoc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
[[styles]]
22
== Styling Cells
33

4-
There are many options available for styling cells to meet your requirements.
4+
There are many options available for styling cells to meet your requirements. Any method that creates a cell can have styling applied to it.
5+
6+
Examples:
7+
8+
* column(value, id, style)
9+
* cell(value, style)
10+
* formula(value, style)
11+
* formula(style, closure)
12+
13+
In addition, you can apply default styling to other elements.
14+
15+
* row { defaultStyle(style) ... }
16+
* sheet { defaultStyle(style) ... }
17+
* merge(style) { }
518

619
=== Font
720

@@ -52,7 +65,7 @@ The font name can be set as well:
5265
[source,groovy]
5366
----
5467
...
55-
cell("A", [font: [name: "Arial"])
68+
cell("A", [font: [name: "Arial"]])
5669
...
5770
----
5871

src/main/groovy/com/jameskleeh/excel/CellStyleBuilder.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class CellStyleBuilder {
249249
}
250250

251251
if (hAlign != null) {
252-
cellStyle.setAlignment((short)hAlign.ordinal())
252+
cellStyle.setAlignment(hAlign)
253253
} else {
254254
throw new IllegalArgumentException("The horizontal alignment must be an instance of ${HorizontalAlignment.getCanonicalName()}")
255255
}
@@ -264,7 +264,7 @@ class CellStyleBuilder {
264264
}
265265

266266
if (vAlign != null) {
267-
cellStyle.setVerticalAlignment((short)vAlign.ordinal())
267+
cellStyle.setVerticalAlignment(vAlign)
268268
} else {
269269
throw new IllegalArgumentException("The vertical alignment must be an instance of ${VerticalAlignment.getCanonicalName()}")
270270
}

src/main/groovy/com/jameskleeh/excel/internal/CreatesCells.groovy

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.jameskleeh.excel.Excel
2323
import com.jameskleeh.excel.Font
2424
import com.jameskleeh.excel.CellFinder
2525
import org.apache.poi.common.usermodel.Hyperlink
26+
import org.apache.poi.common.usermodel.HyperlinkType
2627
import org.apache.poi.xssf.usermodel.XSSFCell
2728
import org.apache.poi.xssf.usermodel.XSSFHyperlink
2829
import org.apache.poi.xssf.usermodel.XSSFSheet
@@ -205,7 +206,7 @@ abstract class CreatesCells {
205206
cell
206207
}
207208

208-
protected XSSFCell handleLink(XSSFCell cell, String address, int linkType) {
209+
protected XSSFCell handleLink(XSSFCell cell, String address, HyperlinkType linkType) {
209210
XSSFHyperlink link = workbook.creationHelper.createHyperlink(linkType)
210211
link.address = address
211212
cell.hyperlink = link
@@ -219,8 +220,23 @@ abstract class CreatesCells {
219220
* @param address The link address
220221
* @param linkType The type of link. One of {@link Hyperlink#LINK_URL}, {@link Hyperlink#LINK_EMAIL}, {@link Hyperlink#LINK_FILE}
221222
* @return The native cell
223+
*
224+
* @deprecated Use {@link #link(Object, String, HyperlinkType}
222225
*/
226+
@Deprecated
223227
XSSFCell link(Object value, String address, int linkType) {
228+
link(value, address, HyperlinkType.forInt(linkType))
229+
}
230+
231+
/**
232+
* Creates a cell with a hyperlink
233+
*
234+
* @param value The cell value
235+
* @param address The link address
236+
* @param linkType The type of link. One of {@link HyperlinkType#URL}, {@link HyperlinkType#EMAIL}, {@link HyperlinkType#FILE}
237+
* @return The native cell
238+
*/
239+
XSSFCell link(Object value, String address, HyperlinkType linkType) {
224240
XSSFCell cell = cell(value, LINK_OPTIONS)
225241
handleLink(cell, address, linkType)
226242
}
@@ -236,7 +252,7 @@ abstract class CreatesCells {
236252
XSSFCell cell = cell(value, LINK_OPTIONS)
237253
callable.resolveStrategy = Closure.DELEGATE_FIRST
238254
callable.delegate = new CellFinder(cell, columnIndexes)
239-
handleLink(cell, callable.call().toString(), Hyperlink.LINK_DOCUMENT)
255+
handleLink(cell, callable.call().toString(), HyperlinkType.DOCUMENT)
240256
}
241257

242258
/**

src/test/groovy/com/jameskleeh/excel/ColumnSpec.groovy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jameskleeh.excel
22

33
import org.apache.poi.common.usermodel.Hyperlink
4+
import org.apache.poi.common.usermodel.HyperlinkType
45
import org.apache.poi.ss.usermodel.Cell
56
import org.apache.poi.ss.util.CellRangeAddress
67
import org.apache.poi.xssf.usermodel.XSSFRow
@@ -94,9 +95,9 @@ class ColumnSpec extends Specification {
9495
XSSFWorkbook workbook = ExcelBuilder.build {
9596
sheet("X") {
9697
row {
97-
link('Test URL', 'http://www.google.com', Hyperlink.LINK_URL)
98-
link('Test File', 'test.docx', Hyperlink.LINK_FILE)
99-
link('Test Email', 'mailto:[email protected]', Hyperlink.LINK_EMAIL)
98+
link('Test URL', 'http://www.google.com', HyperlinkType.URL)
99+
link('Test File', 'test.docx', HyperlinkType.FILE)
100+
link('Test Email', 'mailto:[email protected]', HyperlinkType.EMAIL)
100101
link('Test Document') {
101102
"'${getSheetName()}'!${exactCell(1,1)}"
102103
}
@@ -111,15 +112,15 @@ class ColumnSpec extends Specification {
111112
then:
112113
cells[0].stringCellValue == 'Test URL'
113114
cells[0].hyperlink.address == 'http://www.google.com'
114-
cells[0].hyperlink.type == Hyperlink.LINK_URL
115+
cells[0].hyperlink.typeEnum == HyperlinkType.URL
115116
cells[1].stringCellValue == 'Test File'
116117
cells[1].hyperlink.address == 'test.docx'
117-
cells[1].hyperlink.type == Hyperlink.LINK_FILE
118+
cells[1].hyperlink.typeEnum == HyperlinkType.FILE
118119
cells[2].stringCellValue == 'Test Email'
119120
cells[2].hyperlink.address == 'mailto:[email protected]'
120-
cells[2].hyperlink.type == Hyperlink.LINK_EMAIL
121+
cells[2].hyperlink.typeEnum == HyperlinkType.EMAIL
121122
cells[3].stringCellValue == 'Test Document'
122123
cells[3].hyperlink.address == "'X'!B2"
123-
cells[3].hyperlink.type == Hyperlink.LINK_DOCUMENT
124+
cells[3].hyperlink.typeEnum == HyperlinkType.DOCUMENT
124125
}
125126
}

0 commit comments

Comments
 (0)