Skip to content

Commit 7af583d

Browse files
committed
Add support for merging cells
1 parent e65e986 commit 7af583d

File tree

5 files changed

+104
-7
lines changed

5 files changed

+104
-7
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.1.1'
17+
version '0.1.2.BUILD-SNAPSHOT'
1818

1919
targetCompatibility = 1.7
2020

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import groovy.transform.TypeCheckingMode
55
import org.apache.poi.ss.usermodel.BorderStyle
66
import org.apache.poi.ss.usermodel.FillPatternType
77
import org.apache.poi.ss.usermodel.Font as FontType
8-
import org.apache.poi.ss.usermodel.IndexedColors
98
import org.apache.poi.ss.usermodel.HorizontalAlignment
109
import org.apache.poi.ss.usermodel.VerticalAlignment
1110
import org.apache.poi.xssf.usermodel.XSSFCell
@@ -424,7 +423,7 @@ class CellStyleBuilder {
424423
}
425424

426425
@CompileStatic(TypeCheckingMode.SKIP)
427-
private Map merge(Map[] sources) {
426+
Map merge(Map[] sources) {
428427
if (sources.length == 0) {
429428
return [:]
430429
}

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

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

33
import groovy.transform.CompileStatic
4+
import org.apache.poi.ss.util.CellRangeAddress
45
import org.apache.poi.xssf.usermodel.XSSFCell
56
import org.apache.poi.xssf.usermodel.XSSFRow
67
import org.apache.poi.xssf.usermodel.XSSFSheet
@@ -53,7 +54,13 @@ class Row {
5354
}
5455

5556
void defaultStyle(Map options) {
56-
this.defaultOptions = options
57+
options = new LinkedHashMap(options)
58+
styleBuilder.convertSimpleOptions(options)
59+
if (defaultOptions == null) {
60+
defaultOptions = options
61+
} else {
62+
defaultOptions = styleBuilder.merge(defaultOptions, options)
63+
}
5764
}
5865

5966
XSSFCell column(String value, Object id, final Map options = [:]) {
@@ -101,9 +108,7 @@ class Row {
101108
}
102109

103110
XSSFCell cell() {
104-
XSSFCell cell = nextCell()
105-
cell.setCellValue('')
106-
cell
111+
cell('')
107112
}
108113
XSSFCell cell(Object value) {
109114
cell(value, null)
@@ -133,4 +138,38 @@ class Row {
133138
cell
134139
}
135140

141+
void merge(final Map style, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Row) Closure callable) {
142+
Map existingDefaultOptions = defaultOptions
143+
144+
if (style != null && !style.isEmpty()) {
145+
Map newDefaultOptions = new LinkedHashMap(style)
146+
styleBuilder.convertSimpleOptions(newDefaultOptions)
147+
newDefaultOptions = styleBuilder.merge(defaultOptions, newDefaultOptions)
148+
defaultOptions = newDefaultOptions
149+
}
150+
151+
callable.resolveStrategy = Closure.DELEGATE_FIRST
152+
callable.delegate = this
153+
int startingCellIndex = cellIdx
154+
callable.call()
155+
int endingCellIndex = cellIdx - 1
156+
if (endingCellIndex > startingCellIndex) {
157+
CellRangeAddress range = new CellRangeAddress(row.rowNum, row.rowNum, startingCellIndex, endingCellIndex)
158+
sheet.addMergedRegion(range)
159+
}
160+
161+
defaultOptions = existingDefaultOptions
162+
}
163+
164+
void merge(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Row) Closure callable) {
165+
merge(null, callable)
166+
}
167+
168+
void merge(Object value, Integer count, final Map style = null) {
169+
merge(style) {
170+
cell(value)
171+
skipCells(count)
172+
}
173+
}
174+
136175
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Sheet {
2828
}
2929

3030
void defaultStyle(Map options) {
31+
options = new LinkedHashMap(options)
32+
styleBuilder.convertSimpleOptions(options)
3133
defaultOptions = options
3234
}
3335

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,5 +595,62 @@ class CellStyleBuilderSpec extends Specification {
595595
style.borderBottomEnum == BorderStyle.DASH_DOT
596596
}
597597

598+
void "test merging of options"() {
599+
given:
600+
XSSFCell testCell1
601+
XSSFCell testCell2
602+
XSSFCell testCell3
603+
XSSFCell testCell4
604+
ExcelBuilder.output(new FileOutputStream('/Users/jameskleeh/test.xlsx')) {
605+
sheet {
606+
defaultStyle([border: BorderStyle.MEDIUM])
607+
row {
608+
defaultStyle([border: [left: BorderStyle.HAIR]])
609+
merge([border: [right: BorderStyle.THICK], alignment: 'center']) {
610+
testCell1 = cell('Foo')
611+
cell('')
612+
cell('')
613+
cell('')
614+
cell('')
615+
cell('')
616+
}
617+
testCell2 = cell('')
618+
}
619+
row {
620+
testCell3 = cell('')
621+
}
622+
}
623+
sheet {
624+
row {
625+
testCell4 = cell('')
626+
}
627+
}
628+
}
629+
630+
when:
631+
XSSFCellStyle style1 = testCell1.cellStyle //sheet, row, merge
632+
XSSFCellStyle style2 = testCell2.cellStyle //sheet, row
633+
XSSFCellStyle style3 = testCell3.cellStyle //sheet
634+
XSSFCellStyle style4 = testCell4.cellStyle //none
635+
636+
then:
637+
style1.borderLeftEnum == BorderStyle.HAIR
638+
style1.borderTopEnum == BorderStyle.MEDIUM
639+
style1.borderRightEnum == BorderStyle.THICK
640+
style1.borderBottomEnum == BorderStyle.MEDIUM
641+
style2.borderLeftEnum == BorderStyle.HAIR
642+
style2.borderTopEnum == BorderStyle.MEDIUM
643+
style2.borderRightEnum == BorderStyle.MEDIUM
644+
style2.borderBottomEnum == BorderStyle.MEDIUM
645+
style3.borderLeftEnum == BorderStyle.MEDIUM
646+
style3.borderTopEnum == BorderStyle.MEDIUM
647+
style3.borderRightEnum == BorderStyle.MEDIUM
648+
style3.borderBottomEnum == BorderStyle.MEDIUM
649+
style4.borderLeftEnum == BorderStyle.NONE
650+
style4.borderTopEnum == BorderStyle.NONE
651+
style4.borderRightEnum == BorderStyle.NONE
652+
style4.borderBottomEnum == BorderStyle.NONE
653+
}
654+
598655

599656
}

0 commit comments

Comments
 (0)