Skip to content

Commit 8756205

Browse files
committed
Version 0.3.0. Fix for format not applied when cell had no options. Fix to use cell renderers first.
1 parent 1f7c11e commit 8756205

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ apply plugin: "jacoco"
1414

1515

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

1919
targetCompatibility = 1.7
2020

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,6 @@ class CellStyleBuilder {
356356
XSSFCellStyle cellStyle = workbook.createCellStyle()
357357
if (options.containsKey(FORMAT)) {
358358
setFormat(cellStyle, options[FORMAT])
359-
} else {
360-
Object format = Excel.getFormat(value.class)
361-
if (format) {
362-
setFormat(cellStyle, format)
363-
}
364359
}
365360
if (options.containsKey(FONT)) {
366361
setFont(cellStyle, options[FONT])
@@ -405,6 +400,12 @@ class CellStyleBuilder {
405400
convertSimpleOptions(options)
406401
convertSimpleOptions(defaultOptions)
407402
options = merge(defaultOptions, options)
403+
if (!options.containsKey(FORMAT) && value != null) {
404+
def format = Excel.getFormat(value.class)
405+
if (format != null) {
406+
options.put(FORMAT, format)
407+
}
408+
}
408409
if (options) {
409410
WorkbookCache workbookCache = WORKBOOK_CACHE.get(workbook)
410411
if (workbookCache.containsStyle(options)) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import groovy.transform.CompileStatic
2323
import org.apache.poi.ss.util.CellRangeAddress
2424
import org.apache.poi.xssf.usermodel.XSSFCell
2525
import org.apache.poi.xssf.usermodel.XSSFRow
26-
import org.apache.poi.xssf.usermodel.XSSFSheet
27-
import org.apache.poi.xssf.usermodel.XSSFWorkbook
2826

2927
/**
3028
* A class used to create a row in an excel document

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ abstract class CreatesCells {
186186

187187
XSSFCell cell = nextCell()
188188
setStyle(value, cell, style)
189-
if (value instanceof String) {
189+
Closure callable = Excel.getRenderer(value.class)
190+
if (callable != null) {
191+
cell.setCellValue((String)callable.call(value))
192+
} else if (value instanceof String) {
190193
cell.setCellValue(value)
191194
} else if (value instanceof Calendar) {
192195
cell.setCellValue(value)
@@ -197,12 +200,7 @@ abstract class CreatesCells {
197200
} else if (value instanceof Boolean) {
198201
cell.setCellValue(value)
199202
} else {
200-
Closure callable = Excel.getRenderer(value.class)
201-
if (callable != null) {
202-
cell.setCellValue((String)callable.call(value))
203-
} else {
204-
cell.setCellValue(value.toString())
205-
}
203+
cell.setCellValue(value.toString())
206204
}
207205
cell
208206
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import java.awt.Color
1616
*/
1717
class CellStyleBuilderSpec extends Specification {
1818

19+
void cleanup() {
20+
Excel.formatEntries.clear()
21+
Excel.rendererEntries.clear()
22+
}
23+
1924
void "test convertSimpleOptions"() {
2025
given:
2126
CellStyleBuilder cellStyleBuilder = new CellStyleBuilder(new XSSFWorkbook())
@@ -94,8 +99,8 @@ class CellStyleBuilderSpec extends Specification {
9499
then:
95100
thrown(IllegalArgumentException)
96101

97-
when:
98-
cellStyle = cellStyleBuilder.buildStyle("someString", [:])
102+
when: "A call to getStyle is essential here to bring in the formats registered in Excel"
103+
cellStyle = cellStyleBuilder.getStyle("someString", [:])
99104

100105
then:
101106
cellStyle.dataFormatString == "bar"

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

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

3-
import org.apache.poi.ss.usermodel.FillPatternType
3+
import spock.lang.Issue
44
import spock.lang.Specification
55

6-
import java.awt.Color
7-
8-
/**
9-
* Created by jameskleeh on 9/25/16.
10-
*/
116
class ExcelSpec extends Specification {
127

138
void cleanup() {
@@ -125,4 +120,24 @@ class ExcelSpec extends Specification {
125120
format == 18
126121
}
127122
123+
@Issue("https://github.com/jameskleeh/groovy-excel-builder/issues/7")
124+
void "test creating cells with registered cell formats"() {
125+
Excel.registerCellRenderer(String, 0) { it + 'extra' }
126+
Excel.registerCellFormat(BigDecimal, 0xa)
127+
128+
when:
129+
def wb = ExcelBuilder.build {
130+
sheet {
131+
row {
132+
cell(new BigDecimal("1.32"))
133+
cell("Foo")
134+
}
135+
}
136+
}
137+
138+
then:
139+
wb.getSheetAt(0).getRow(0).getCell(0).cellStyle.dataFormat == (short)0xa
140+
wb.getSheetAt(0).getRow(0).getCell(1).stringCellValue == "Fooextra"
141+
}
142+
128143
}

0 commit comments

Comments
 (0)