@@ -20,11 +20,16 @@ package com.jameskleeh.excel.internal
2020
2121import com.jameskleeh.excel.CellStyleBuilder
2222import com.jameskleeh.excel.Excel
23- import com.jameskleeh.excel.Formula
23+ import com.jameskleeh.excel.Font
24+ import com.jameskleeh.excel.CellFinder
25+ import org.apache.poi.common.usermodel.Hyperlink
2426import org.apache.poi.xssf.usermodel.XSSFCell
27+ import org.apache.poi.xssf.usermodel.XSSFHyperlink
2528import org.apache.poi.xssf.usermodel.XSSFSheet
2629import org.apache.poi.xssf.usermodel.XSSFWorkbook
2730
31+ import java.awt.Color
32+
2833/**
2934 * A base class used to create cells
3035 *
@@ -37,9 +42,10 @@ abstract class CreatesCells {
3742 protected Map defaultOptions
3843 protected final Map<Object , Integer > columnIndexes
3944 protected final CellStyleBuilder styleBuilder
45+ protected static final Map LINK_OPTIONS = [font : [style : Font . UNDERLINE , color : Color . BLUE ]]
4046
41- CreatesCells (XSSFWorkbook workbook , XSSFSheet sheet , Map defaultOptions , Map<Object , Integer > columnIndexes , CellStyleBuilder styleBuilder ) {
42- this . workbook = workbook
47+ CreatesCells (XSSFSheet sheet , Map defaultOptions , Map<Object , Integer > columnIndexes , CellStyleBuilder styleBuilder ) {
48+ this . workbook = sheet . workbook
4349 this . sheet = sheet
4450 this . defaultOptions = defaultOptions
4551 this . columnIndexes = columnIndexes
@@ -121,7 +127,7 @@ abstract class CreatesCells {
121127 * @param callable The return value will be assigned to the cell formula. The closure delegate contains helper methods to get references to other cells.
122128 * @return The native cell
123129 */
124- XSSFCell formula (@DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Formula ) Closure callable ) {
130+ XSSFCell formula (@DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = CellFinder ) Closure callable ) {
125131 formula(null , callable)
126132 }
127133
@@ -132,10 +138,10 @@ abstract class CreatesCells {
132138 * @param callable The return value will be assigned to the cell formula. The closure delegate contains helper methods to get references to other cells.
133139 * @return The native cell
134140 */
135- XSSFCell formula (final Map style , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = Formula ) Closure callable ) {
141+ XSSFCell formula (final Map style , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = CellFinder ) Closure callable ) {
136142 XSSFCell cell = nextCell()
137143 callable. resolveStrategy = Closure . DELEGATE_FIRST
138- callable. delegate = new Formula (cell, columnIndexes)
144+ callable. delegate = new CellFinder (cell, columnIndexes)
139145 String formula
140146 if (callable. maximumNumberOfParameters == 1 ) {
141147 formula = (String )callable. call(cell)
@@ -201,6 +207,40 @@ abstract class CreatesCells {
201207 cell
202208 }
203209
210+ protected XSSFCell handleLink (XSSFCell cell , String address , int linkType ) {
211+ XSSFHyperlink link = workbook. creationHelper. createHyperlink(linkType)
212+ link. address = address
213+ cell. hyperlink = link
214+ cell
215+ }
216+
217+ /**
218+ * Creates a cell with a hyperlink
219+ *
220+ * @param value The cell value
221+ * @param address The link address
222+ * @param linkType The type of link. One of {@link Hyperlink#LINK_URL}, {@link Hyperlink#LINK_EMAIL}, {@link Hyperlink#LINK_FILE}
223+ * @return The native cell
224+ */
225+ XSSFCell link (Object value , String address , int linkType ) {
226+ XSSFCell cell = cell(value, LINK_OPTIONS )
227+ handleLink(cell, address, linkType)
228+ }
229+
230+ /**
231+ * Creates a cell with a hyperlink to another cell in the document
232+ *
233+ * @param value The cell value
234+ * @param callable The closure to build the link
235+ * @return The native cell
236+ */
237+ XSSFCell link (Object value , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = CellFinder ) Closure callable ) {
238+ XSSFCell cell = cell(value, LINK_OPTIONS )
239+ callable. resolveStrategy = Closure . DELEGATE_FIRST
240+ callable. delegate = new CellFinder (cell, columnIndexes)
241+ handleLink(cell, callable. call(). toString(), Hyperlink . LINK_DOCUMENT )
242+ }
243+
204244 /**
205245 * Merges cells
206246 *
@@ -230,4 +270,4 @@ abstract class CreatesCells {
230270 }
231271 }
232272
233- }
273+ }
0 commit comments