diff --git a/src/groovy/pl/touk/excel/export/Formatters.groovy b/src/groovy/pl/touk/excel/export/Formatters.groovy index deb3587..14e899c 100644 --- a/src/groovy/pl/touk/excel/export/Formatters.groovy +++ b/src/groovy/pl/touk/excel/export/Formatters.groovy @@ -3,6 +3,7 @@ package pl.touk.excel.export import pl.touk.excel.export.getters.Getter import pl.touk.excel.export.getters.PropertyGetter import pl.touk.excel.export.getters.AsIsPropertyGetter +import pl.touk.excel.export.getters.AsClosurePropertyGetter import pl.touk.excel.export.getters.LongToDatePropertyGetter class Formatters { @@ -14,14 +15,20 @@ class Formatters { return new AsIsPropertyGetter(propertyName) } + static PropertyGetter asClosure(Closure closure) { + return new AsClosurePropertyGetter(closure) + } + static List convertSafelyToGetters(List properties) { properties.collect { if (it instanceof Getter) { return it } else if(it instanceof String) { return asIs(it) + } else if(it instanceof Closure) { + return asClosure(it) } else { - throw IllegalArgumentException('List of properties, which should be either String, a Getter. Found: ' + + throw IllegalArgumentException('List of properties, which should be either String, a Getter or a Groovy Closure. Found: ' + it?.toString() + ' of class ' + it?.getClass()) } } diff --git a/src/groovy/pl/touk/excel/export/getters/AsClosurePropertyGetter.groovy b/src/groovy/pl/touk/excel/export/getters/AsClosurePropertyGetter.groovy new file mode 100644 index 0000000..f9eb866 --- /dev/null +++ b/src/groovy/pl/touk/excel/export/getters/AsClosurePropertyGetter.groovy @@ -0,0 +1,19 @@ +package pl.touk.excel.export.getters + +class AsClosurePropertyGetter extends PropertyGetter { + + def closure + + AsClosurePropertyGetter(Closure closure) { + super(null) + this.closure = closure + } + + def getFormattedValue(Object object) { + closure.call(object) + } + + def format(Object value) { + null + } +} diff --git a/test/unit/pl/touk/excel/export/XlsxExporterRowTest.groovy b/test/unit/pl/touk/excel/export/XlsxExporterRowTest.groovy index b2c75e4..220953c 100644 --- a/test/unit/pl/touk/excel/export/XlsxExporterRowTest.groovy +++ b/test/unit/pl/touk/excel/export/XlsxExporterRowTest.groovy @@ -73,6 +73,31 @@ class XlsxExporterRowTest extends XlsxExporterTest { testObject.verifyRowHasSelectedProperties(xlsxReporter.withDefaultSheet(), 3) } + @Test + void shouldFillRowEvaluatingPropertyList() { + //given + SampleObject testObject = new SampleObject() + def propertyExpressions = [ + 'stringValue', + { it.stringValue }, + { it.child.stringValue.toUpperCase() }, + { it.booleanValue?" it is true ":"it is false " }, + { it.shortValue + it.integerValue }, + { Math.sqrt(it.doubleValue) } + ] + + //when + xlsxReporter.add(testObject, propertyExpressions, 0) + + //then + assert xlsxReporter.getCellAt(0, 0).getStringCellValue() == testObject.stringValue + assert xlsxReporter.getCellAt(0, 1).getStringCellValue() == testObject.stringValue + assert xlsxReporter.getCellAt(0, 2).getStringCellValue() == testObject.child.stringValue.toUpperCase() + assert xlsxReporter.getCellAt(0, 3).getStringCellValue() == testObject.booleanValue?" it is true ":"it is false " + assert xlsxReporter.getCellAt(0, 4).getNumericCellValue() == testObject.integerValue + testObject.shortValue + assert xlsxReporter.getCellAt(0, 5).getNumericCellValue() == Math.sqrt(testObject.doubleValue) + } + @Test void shouldFillRows() { //given