Skip to content

Commit

Permalink
Support POI 3.16. Add/update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskleeh committed May 3, 2017
1 parent 8756205 commit fbab0ec
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: "jacoco"


group 'com.jameskleeh'
version '0.3.0'
version '0.4.0'

targetCompatibility = 1.7

Expand All @@ -26,7 +26,7 @@ repositories {

dependencies {
provided 'org.codehaus.groovy:groovy-all:2.4.7'
compile "org.apache.poi:poi-ooxml:3.14"
compile "org.apache.poi:poi-ooxml:3.16"
codenarc 'org.codenarc:CodeNarc:0.25.2'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testRuntime "org.slf4j:slf4j-api:1.7.10"
Expand Down
14 changes: 13 additions & 1 deletion docs/formulas.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,16 @@ formula {

The resulting formula will be:

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

=== Formula Styling

Formulas support a Map parameter that is the styling options

[source,groovy]
----
formula("CONCATENATE(A1,A2)", [font: Font.BOLD])
formula([font: Font.BOLD]) {
...
}
----
2 changes: 2 additions & 0 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ include::columns.adoc[]

include::cells.adoc[]

include::merging.adoc[]

include::links.adoc[]

include::formulas.adoc[]
Expand Down
6 changes: 5 additions & 1 deletion docs/introduction.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

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.

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]!
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]!

=== Common Problems

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.
47 changes: 47 additions & 0 deletions docs/merging.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[[merging]]
== Merging Cells

This module supports merging cells with an easy to use API.

[source,groovy]
----
row {
merge {
//Anything you can do inside row directly
cell("Foo")
cell("Bar")
}
}
----

You can also apply default styling to the merged cells

[source,groovy]
----
row {
merge(font: Font.BOLD) {
//Anything you can do inside row directly
cell("Foo")
cell("Bar")
}
}
----

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.

[source,groovy]
----
row {
//The value "Foo" will span 3 columns
merge("Foo", 3)
}
----

You can also apply default styling to the merged cells created in that way

[source,groovy]
----
row {
merge("Foo", 3, [font: Font.BOLD])
}
----
17 changes: 15 additions & 2 deletions docs/styles.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
[[styles]]
== Styling Cells

There are many options available for styling cells to meet your requirements.
There are many options available for styling cells to meet your requirements. Any method that creates a cell can have styling applied to it.

Examples:

* column(value, id, style)
* cell(value, style)
* formula(value, style)
* formula(style, closure)

In addition, you can apply default styling to other elements.

* row { defaultStyle(style) ... }
* sheet { defaultStyle(style) ... }
* merge(style) { }

=== Font

Expand Down Expand Up @@ -52,7 +65,7 @@ The font name can be set as well:
[source,groovy]
----
...
cell("A", [font: [name: "Arial"])
cell("A", [font: [name: "Arial"]])
...
----

Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/com/jameskleeh/excel/CellStyleBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class CellStyleBuilder {
}

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

if (vAlign != null) {
cellStyle.setVerticalAlignment((short)vAlign.ordinal())
cellStyle.setVerticalAlignment(vAlign)
} else {
throw new IllegalArgumentException("The vertical alignment must be an instance of ${VerticalAlignment.getCanonicalName()}")
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/groovy/com/jameskleeh/excel/internal/CreatesCells.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.jameskleeh.excel.Excel
import com.jameskleeh.excel.Font
import com.jameskleeh.excel.CellFinder
import org.apache.poi.common.usermodel.Hyperlink
import org.apache.poi.common.usermodel.HyperlinkType
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFHyperlink
import org.apache.poi.xssf.usermodel.XSSFSheet
Expand Down Expand Up @@ -205,7 +206,7 @@ abstract class CreatesCells {
cell
}

protected XSSFCell handleLink(XSSFCell cell, String address, int linkType) {
protected XSSFCell handleLink(XSSFCell cell, String address, HyperlinkType linkType) {
XSSFHyperlink link = workbook.creationHelper.createHyperlink(linkType)
link.address = address
cell.hyperlink = link
Expand All @@ -219,8 +220,23 @@ abstract class CreatesCells {
* @param address The link address
* @param linkType The type of link. One of {@link Hyperlink#LINK_URL}, {@link Hyperlink#LINK_EMAIL}, {@link Hyperlink#LINK_FILE}
* @return The native cell
*
* @deprecated Use {@link #link(Object, String, HyperlinkType}
*/
@Deprecated
XSSFCell link(Object value, String address, int linkType) {
link(value, address, HyperlinkType.forInt(linkType))
}

/**
* Creates a cell with a hyperlink
*
* @param value The cell value
* @param address The link address
* @param linkType The type of link. One of {@link HyperlinkType#URL}, {@link HyperlinkType#EMAIL}, {@link HyperlinkType#FILE}
* @return The native cell
*/
XSSFCell link(Object value, String address, HyperlinkType linkType) {
XSSFCell cell = cell(value, LINK_OPTIONS)
handleLink(cell, address, linkType)
}
Expand All @@ -236,7 +252,7 @@ abstract class CreatesCells {
XSSFCell cell = cell(value, LINK_OPTIONS)
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new CellFinder(cell, columnIndexes)
handleLink(cell, callable.call().toString(), Hyperlink.LINK_DOCUMENT)
handleLink(cell, callable.call().toString(), HyperlinkType.DOCUMENT)
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/test/groovy/com/jameskleeh/excel/ColumnSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jameskleeh.excel

import org.apache.poi.common.usermodel.Hyperlink
import org.apache.poi.common.usermodel.HyperlinkType
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.util.CellRangeAddress
import org.apache.poi.xssf.usermodel.XSSFRow
Expand Down Expand Up @@ -94,9 +95,9 @@ class ColumnSpec extends Specification {
XSSFWorkbook workbook = ExcelBuilder.build {
sheet("X") {
row {
link('Test URL', 'http://www.google.com', Hyperlink.LINK_URL)
link('Test File', 'test.docx', Hyperlink.LINK_FILE)
link('Test Email', 'mailto:foo@bar.com', Hyperlink.LINK_EMAIL)
link('Test URL', 'http://www.google.com', HyperlinkType.URL)
link('Test File', 'test.docx', HyperlinkType.FILE)
link('Test Email', 'mailto:foo@bar.com', HyperlinkType.EMAIL)
link('Test Document') {
"'${getSheetName()}'!${exactCell(1,1)}"
}
Expand All @@ -111,15 +112,15 @@ class ColumnSpec extends Specification {
then:
cells[0].stringCellValue == 'Test URL'
cells[0].hyperlink.address == 'http://www.google.com'
cells[0].hyperlink.type == Hyperlink.LINK_URL
cells[0].hyperlink.typeEnum == HyperlinkType.URL
cells[1].stringCellValue == 'Test File'
cells[1].hyperlink.address == 'test.docx'
cells[1].hyperlink.type == Hyperlink.LINK_FILE
cells[1].hyperlink.typeEnum == HyperlinkType.FILE
cells[2].stringCellValue == 'Test Email'
cells[2].hyperlink.address == 'mailto:foo@bar.com'
cells[2].hyperlink.type == Hyperlink.LINK_EMAIL
cells[2].hyperlink.typeEnum == HyperlinkType.EMAIL
cells[3].stringCellValue == 'Test Document'
cells[3].hyperlink.address == "'X'!B2"
cells[3].hyperlink.type == Hyperlink.LINK_DOCUMENT
cells[3].hyperlink.typeEnum == HyperlinkType.DOCUMENT
}
}

0 comments on commit fbab0ec

Please sign in to comment.