Skip to content

Commit

Permalink
enhanced excel functions (url/email hyperlinks)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Sep 8, 2024
1 parent 1766202 commit 315b987
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 35 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ All notable changes to this project will be documented in this file.
- :openai module support for assistant run api (...)
- :openai module support for assistant run steps api (...)

- function `add-url-hyperlink` to :excel module
- function `add-email-hyperlink` to :excel module
- function `remove-hyperlink` to :excel module
- function `remove-formula` to :excel module



## [1.12.33] - 2024-09-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public DocSection section() {

final DocSection hyperlink = new DocSection("Hyperlinks", id());
all.addSection(hyperlink);
hyperlink.addItem(diBuilder.getDocItem("excel/add-url-hyperlink", false));
hyperlink.addItem(diBuilder.getDocItem("excel/add-email-hyperlink", false));
hyperlink.addItem(diBuilder.getDocItem("excel/remove-hyperlink", false));

final DocSection charts = new DocSection("Charts", id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void registerFont(
final String fontName,
final int heightInPoints
) {
registerFont(id, fontName, heightInPoints, false, false, (Short)null);
registerFont(id, fontName, heightInPoints, false, false, false, (Short)null);
}

public void registerFont(
Expand All @@ -192,6 +192,7 @@ public void registerFont(
final Integer heightInPoints,
final boolean bold,
final boolean italic,
final boolean underline,
final Short colorIndex
) {
final Font font = workbook.createFont();
Expand All @@ -203,6 +204,7 @@ public void registerFont(
}
font.setBold(bold);
font.setItalic(italic);
font.setUnderline(underline ? Font.U_SINGLE : Font.U_NONE);
if (colorIndex != null) {
font.setColor(colorIndex);
}
Expand All @@ -215,6 +217,7 @@ public void registerFont(
final Integer heightInPoints,
final boolean bold,
final boolean italic,
final boolean underline,
final Color color
) {
final Font font = workbook.createFont();
Expand All @@ -226,6 +229,7 @@ public void registerFont(
}
font.setBold(bold);
font.setItalic(italic);
font.setUnderline(underline ? Font.U_SINGLE : Font.U_NONE);
if (color != null) {
if (font instanceof XSSFFont) {
((XSSFFont)font).setColor(new XSSFColor(color, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;

import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFDataValidationHelper;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
Expand All @@ -54,6 +55,7 @@
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FontFormatting;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.PatternFormatting;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
Expand Down Expand Up @@ -836,24 +838,42 @@ public void setFormula(final int row, final int col, final String formula, final
}

public void removeFormula(final int row, final int col) {
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeFormula();
}
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeFormula();
}
}

public void setUrlHyperlink(final int row, final int col, final String text, final String urlAddress) {
final Hyperlink link = sheet.getWorkbook().getCreationHelper().createHyperlink(HyperlinkType.URL);
link.setAddress(urlAddress);

final Cell cell = getCellOrCreate(row, col);
cell.setCellValue(text);
cell.setHyperlink(link);
}

public void setEmailHyperlink(final int row, final int col, final String text, final String emailAddress) {
final Hyperlink link = sheet.getWorkbook().getCreationHelper().createHyperlink(HyperlinkType.EMAIL);
link.setAddress("mailto:" + emailAddress);

final Cell cell = getCellOrCreate(row, col);
cell.setCellValue(text);
cell.setHyperlink(link);
}

public void removeHyperlink(final int row, final int col) {
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeHyperlink();
}
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeHyperlink();
}
}

public void removeComment(final int row, final int col) {
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeCellComment();
}
final Cell cell = getCell(row, col);
if (cell != null) {
cell.removeCellComment();
}
}

public Map<String,Object> getCellStyleInfo(final int row, final int col) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public ExcelFontBuilder italic() {
return this;
}

public ExcelFontBuilder underline() {
this.underline = true;
return this;
}

public ExcelFontBuilder color(final IndexedColors color) {
this.colorIndex = color.index;
return this;
Expand All @@ -85,13 +90,13 @@ public ExcelFontBuilder colorHtml(final String color) {

public ExcelFacade end() {
if (colorIndex != null) {
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, colorIndex);
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, underline, colorIndex);
}
else if (color != null) {
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, color);
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, underline, color);
}
else {
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, (Short)null);
managedExcel.registerFont(id, fontName, heightInPoints, bold, italic, underline, (Short)null);
}

return parentBuilder;
Expand All @@ -105,6 +110,7 @@ else if (color != null) {
private Integer heightInPoints;
private boolean bold;
private boolean italic;
private boolean underline;
private Short colorIndex;
private Color color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ public String sumFormula(final int rowFrom1, final int rowTo1, final int colFrom
sheet.getCellAddress_A1_style(rowTo1-1, colTo1-1));
}

public String cellAddress_A1_style(final int row1, final int col1) {
return sheet.getCellAddress_A1_style(row1-1, col1-1);
public void setUrlHyperlink(final int row1, final int col1, final String text, final String urlAddress) {
sheet.setUrlHyperlink(row1-1, col1-1, text, urlAddress);
}

public Map<String,Object> getCellStyleInfo(final int row1, final int col1) {
return sheet.getCellStyleInfo(row1-1, col1-1);
public void setEmailHyperlink(final int row1, final int col1, final String text, final String emailAddress) {
sheet.setEmailHyperlink(row1-1, col1-1, text, emailAddress);
}

public void removeFormula(final int row1, final int col1) {
Expand All @@ -602,6 +602,15 @@ public void removeComment(final int row1, final int col1) {
}


public Map<String,Object> getCellStyleInfo(final int row1, final int col1) {
return sheet.getCellStyleInfo(row1-1, col1-1);
}

public String cellAddress_A1_style(final int row1, final int col1) {
return sheet.getCellAddress_A1_style(row1-1, col1-1);
}


// ------------------------------------------------------------------------
// Reader functions
// ------------------------------------------------------------------------
Expand Down
107 changes: 92 additions & 15 deletions src/main/resources/com/github/jlangch/venice/excel.venice
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,78 @@
;; Hyperlinks
;; -----------------------------------------------------------------------------

(defn
^{ :arglists '("(add-url-hyperlink sheet row col text url)")
:doc "Adds an URL hyperlink to a cell"
:examples '(
"""
(do
(load-module :excel)
(let [wbook (excel/create :xlsx)
sheet (excel/add-sheet wbook "Sheet 1")]
(excel/add-font wbook :hyperlink { :underline true
:color :BLUE })
(excel/add-style wbook :hyperlink { :font :hyperlink })
(excel/write-values sheet 1 1 "John" "Doe")
(excel/write-values sheet 2 1 "Sue" "Ford")
(excel/add-url-hyperlink sheet 1 3 "https://john.doe.org/" "https://john.doe.org/")
(excel/add-url-hyperlink sheet 2 3 "https://sue.ford.org/" "https://sue.ford.org/")
(excel/cell-style sheet 1 3 :hyperlink)
(excel/cell-style sheet 2 3 :hyperlink)
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/remove-hyperlink"
"excel/add-email-hyperlink") }

add-url-hyperlink [sheet row col text url]

{ :pre [(instance-of? :ExcelSheetFacade sheet)
(long? row) (long? row)
(long? col) (long? col)
(string? text)
(string? url)] }

(. sheet :setUrlHyperlink row col text url))


(defn
^{ :arglists '("(add-email-hyperlink sheet row col text url)")
:doc "Adds an email hyperlink to a cell"
:examples '(
"""
(do
(load-module :excel)
(let [wbook (excel/create :xlsx)
sheet (excel/add-sheet wbook "Sheet 1")]
(excel/add-font wbook :hyperlink { :underline true
:color :BLUE })
(excel/add-style wbook :hyperlink { :font :hyperlink })
(excel/write-values sheet 1 1 "John" "Doe")
(excel/write-values sheet 2 1 "Sue" "Ford")
(excel/add-email-hyperlink sheet 1 3 "[email protected]" "[email protected]")
(excel/add-email-hyperlink sheet 2 3 "[email protected]" "[email protected]")
(excel/cell-style sheet 1 3 :hyperlink)
(excel/cell-style sheet 2 3 :hyperlink)
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/remove-hyperlink"
"excel/add-url-hyperlink") }

add-email-hyperlink [sheet row col text email]

{ :pre [(instance-of? :ExcelSheetFacade sheet)
(long? row) (long? row)
(long? col) (long? col)
(string? text)
(string? email)] }

(. sheet :setEmailHyperlink row col text email))


(defn
^{ :arglists '("(remove-hyperlink sheet row col)")
:doc "Remove a cell comment"
Expand All @@ -1816,6 +1888,8 @@
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/add-url-hyperlink"
"excel/add-email-hyperlink"
"excel/remove-comment"
"excel/remove-formula") }

Expand Down Expand Up @@ -2257,13 +2331,14 @@

Options:

| :name s | font name, e.g. 'Arial' |
| :height n | height in points, e.g. 12 |
| :bold b | bold, e.g. true, false |
| :italic b | italic, e.g. true, false |
| :color c | color, either an Excel indexed color or a HTML \
color, e.g. :BLUE, "#00FF00" \
note: only XLSX supports 24 bit colors |
| :name s | font name, e.g. 'Arial' |
| :height n | height in points, e.g. 12 |
| :bold b | bold, e.g. true, false |
| :italic b | italic, e.g. true, false |
| :underline b | underline, e.g. true, false |
| :color c | color, either an Excel indexed color or a HTML \
color, e.g. :BLUE, "#00FF00" \
note: only XLSX supports 24 bit colors |
"""
:examples '(
"""
Expand All @@ -2275,6 +2350,7 @@
(excel/add-font wbook :header { :height 12
:bold true
:italic false
:underline false
:color :BLUE })
(excel/add-style wbook :header { :font :header })

Expand All @@ -2299,14 +2375,15 @@
{ :pre [(instance-of? :ExcelFacade wbook)
(keyword? font-id)] }
(let [font-builder (. wbook :withFont (name font-id))]
(when-let [x (:height options)] (. font-builder :heightInPoints x))
(when-let [x (:bold options)] (. font-builder :bold))
(when-let [x (:italic options)] (. font-builder :italic))
(when-let [x (:color options)] (cond
(string? x) (. font-builder :colorHtml x)
(keyword? x) (. font-builder :color (x colors))
(long? x) (. font-builder :color x))
:else (ex :VncException "Invalid font color"))
(when-let [x (:height options)] (. font-builder :heightInPoints x))
(when-let [x (:bold options)] (. font-builder :bold))
(when-let [x (:italic options)] (. font-builder :italic))
(when-let [x (:underline options)] (. font-builder :underline))
(when-let [x (:color options)] (cond
(string? x) (. font-builder :colorHtml x)
(keyword? x) (. font-builder :color (x colors))
(long? x) (. font-builder :color x))
:else (ex :VncException "Invalid font color"))

(. font-builder :end)
font-builder)))
Expand Down

0 comments on commit 315b987

Please sign in to comment.