1+ /*
2+ Licensed to the Apache Software Foundation (ASF) under one
3+ or more contributor license agreements. See the NOTICE file
4+ distributed with this work for additional information
5+ regarding copyright ownership. The ASF licenses this file
6+ to you under the Apache License, Version 2.0 (the
7+ "License"); you may not use this file except in compliance
8+ with the License. You may obtain a copy of the License at
9+
10+ http://www.apache.org/licenses/LICENSE-2.0
11+
12+ Unless required by applicable law or agreed to in writing,
13+ software distributed under the License is distributed on an
14+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ KIND, either express or implied. See the License for the
16+ specific language governing permissions and limitations
17+ under the License.
18+ */
119package com.jameskleeh.excel
220
21+ import com.jameskleeh.excel.internal.CreatesCells
322import groovy.transform.CompileStatic
423import org.apache.poi.ss.util.CellRangeAddress
524import org.apache.poi.xssf.usermodel.XSSFCell
@@ -11,36 +30,26 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook
1130 * A class used to create a row in an excel document
1231 */
1332@CompileStatic
14- class Row {
33+ class Row extends CreatesCells {
1534
1635 private final XSSFRow row
17- private final XSSFWorkbook workbook
18- private final XSSFSheet sheet
19- private Map defaultOptions
20- private final Map<Object , Integer > columnIndexes
21- private final CellStyleBuilder styleBuilder
36+
2237 private int cellIdx
2338
24- Row (XSSFWorkbook workbook , XSSFSheet sheet , XSSFRow row , Map defaultOptions , Map<Object , Integer > columnIndexes ) {
25- this . workbook = workbook
26- this . sheet = sheet
39+ Row (XSSFWorkbook workbook , XSSFSheet sheet , XSSFRow row , Map defaultOptions , Map<Object , Integer > columnIndexes , CellStyleBuilder styleBuilder ) {
40+ super (workbook, sheet, defaultOptions, columnIndexes, styleBuilder)
2741 this . row = row
2842 this . cellIdx = 0
29- this . defaultOptions = defaultOptions
30- this . columnIndexes = columnIndexes
31- this . styleBuilder = new CellStyleBuilder (workbook)
3243 }
3344
34- private XSSFCell nextCell () {
45+ @Override
46+ protected XSSFCell nextCell () {
3547 XSSFCell cell = row. createCell(cellIdx)
3648 cellIdx++
3749 cell
3850 }
3951
40- private void setStyle (Object value , XSSFCell cell , Map options ) {
41- styleBuilder. setStyle(value, cell, options, defaultOptions)
42- }
43-
52+ @Override
4453 void skipCells (int num ) {
4554 cellIdx + = num
4655 }
@@ -53,91 +62,7 @@ class Row {
5362 }
5463 }
5564
56- void defaultStyle (Map 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- }
64- }
65-
66- XSSFCell column (String value , Object id , final Map options = [:]) {
67- XSSFCell cell = nextCell()
68- cell. setCellValue(value)
69- setStyle(value, cell, options)
70- columnIndexes[id] = cell. columnIndex
71- cell
72- }
73-
74- XSSFCell formula (String formulaString , final Map style ) {
75- XSSFCell cell = nextCell()
76- if (formulaString. startsWith(' =' )) {
77- formulaString = formulaString[1 .. -1 ]
78- }
79- cell. setCellFormula(formulaString)
80- setStyle(null , cell, style)
81- cell
82- }
83-
84- XSSFCell formula (String formulaString ) {
85- formula(formulaString, null )
86- }
87-
88- XSSFCell formula (@DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Formula ) Closure callable ) {
89- formula(null , callable)
90- }
91-
92- XSSFCell formula (final Map style , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Formula ) Closure callable ) {
93- XSSFCell cell = nextCell()
94- callable. resolveStrategy = Closure . DELEGATE_FIRST
95- callable. delegate = new Formula (cell, columnIndexes)
96- String formula
97- if (callable. maximumNumberOfParameters == 1 ) {
98- formula = (String )callable. call(cell)
99- } else {
100- formula = (String )callable. call()
101- }
102- if (formula. startsWith(' =' )) {
103- formula = formula[1 .. -1 ]
104- }
105- cell. setCellFormula(formula)
106- setStyle(null , cell, style)
107- cell
108- }
109-
110- XSSFCell cell () {
111- cell(' ' )
112- }
113- XSSFCell cell (Object value ) {
114- cell(value, null )
115- }
116- XSSFCell cell (Object value , final Map style ) {
117-
118- XSSFCell cell = nextCell()
119- setStyle(value, cell, style)
120- if (value instanceof String ) {
121- cell. setCellValue(value)
122- } else if (value instanceof Calendar ) {
123- cell. setCellValue(value)
124- } else if (value instanceof Date ) {
125- cell. setCellValue(value)
126- } else if (value instanceof Number ) {
127- cell. setCellValue(value. doubleValue())
128- } else if (value instanceof Boolean ) {
129- cell. setCellValue(value)
130- } else {
131- Closure callable = Excel . getRenderer(value. class)
132- if (callable != null ) {
133- cell. setCellValue((String )callable. call(value))
134- } else {
135- cell. setCellValue(value. toString())
136- }
137- }
138- cell
139- }
140-
65+ @Override
14166 void merge (final Map style , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Row ) Closure callable ) {
14267 Map existingDefaultOptions = defaultOptions
14368
@@ -161,10 +86,12 @@ class Row {
16186 defaultOptions = existingDefaultOptions
16287 }
16388
89+ @Override
16490 void merge (@DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Row ) Closure callable ) {
16591 merge(null , callable)
16692 }
16793
94+ @Override
16895 void merge (Object value , Integer count , final Map style = null ) {
16996 merge(style) {
17097 cell(value)
0 commit comments