File tree Expand file tree Collapse file tree 6 files changed +40
-23
lines changed
main/groovy/com/jameskleeh/excel
test/groovy/com/jameskleeh/excel Expand file tree Collapse file tree 6 files changed +40
-23
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ apply plugin: "jacoco"
1414
1515
1616group ' com.jameskleeh'
17- version ' 0.2 .0'
17+ version ' 0.3 .0'
1818
1919targetCompatibility = 1.7
2020
Original file line number Diff line number Diff 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)) {
Original file line number Diff line number Diff line change @@ -23,8 +23,6 @@ import groovy.transform.CompileStatic
2323import org.apache.poi.ss.util.CellRangeAddress
2424import org.apache.poi.xssf.usermodel.XSSFCell
2525import 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
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ import java.awt.Color
1616 */
1717class 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"
Original file line number Diff line number Diff line change 11package com.jameskleeh.excel
22
3- import org.apache.poi.ss.usermodel.FillPatternType
3+ import spock.lang.Issue
44import spock.lang.Specification
55
6- import java.awt.Color
7-
8- /**
9- * Created by jameskleeh on 9/25/16.
10- */
116class 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}
You can’t perform that action at this time.
0 commit comments