Skip to content

Commit

Permalink
refactored :excel module
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Sep 1, 2024
1 parent 5dfeda7 commit 6f70918
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.PatternFormatting;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
Expand All @@ -68,6 +71,7 @@
import com.github.jlangch.venice.util.excel.chart.LineDataSeries;
import com.github.jlangch.venice.util.excel.chart.PieDataSeries;
import com.github.jlangch.venice.util.excel.chart.Position;
import com.github.jlangch.venice.util.pdf.HtmlColor;


/**
Expand Down Expand Up @@ -298,6 +302,34 @@ public void copyCellStyle(
}
}

public void conditionalBackgroundColor(
final String condRule, // "ISBLANK(A1)"
final String condRegion, // "A1:B1"
final String bgColorHtml // "#CC636A"
) {
// Create a conditional formatting rule for blank cells
final SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

// Define a conditional formatting rule using a formula (ISBLANK(A1))
final ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(condRule);

// Create a pattern formatting object
final PatternFormatting fill = rule.createPatternFormatting();

final Color bg = HtmlColor.getColor(bgColorHtml);
byte[] rgb = new byte[]{(byte)bg.getRed(), (byte)bg.getGreen(), (byte)bg.getBlue()};

fill.setFillBackgroundColor(new XSSFColor(rgb, null));
fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);


// Define the range of cells to apply the rule
final CellRangeAddress[] regions = { CellRangeAddress.valueOf(condRegion) };

// Apply the conditional formatting rule to the sheet
sheetCF.addConditionalFormatting(regions, rule);
}

public Object getValue(final int row, final int col) {
final Cell cell = getCell(row, col);
return getValue(cell);
Expand Down Expand Up @@ -666,7 +698,7 @@ public Map<String,Object> getCellStyleInfo(final int row, final int col) {
if (cell != null) {
final CellStyle style = cell.getCellStyle();
if (style != null) {
// Cell type
// Cell type
info.put("cell.type", getCellType(cell.getCellType()));
info.put("cell.col", col);
info.put("cell.row", row);
Expand Down Expand Up @@ -881,7 +913,7 @@ private Object getValue(final Cell cell) {

final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
return null;
}

switch (cellValue.getCellType()) {
Expand Down Expand Up @@ -912,7 +944,7 @@ private String getString(final Cell cell) {

final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
return null;
}

switch (cellValue.getCellType()) {
Expand Down Expand Up @@ -941,7 +973,7 @@ private Boolean getBoolean(final Cell cell) {

final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
return null;
}

if (cellValue.getCellType() == CellType.BLANK) {
Expand All @@ -965,7 +997,7 @@ private Long getInteger(final Cell cell) {

final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
return null;
}

if (cellValue.getCellType() == CellType.BLANK) {
Expand All @@ -989,7 +1021,7 @@ private Double getFloat(final Cell cell) {

final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return null;
return null;
}

if (cellValue.getCellType() == CellType.BLANK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public void copyCellStyle(
sheet.copyCellStyle(cellRowFrom1-1, cellColFrom1-1, cellRowTo1-1, cellColTo1-1);
}

public void conditionalBackgroundColor(
final String condRule, // "ISBLANK(A1)"
final String condRegion, // "A1:B1"
final String bgColorHtml // "#CC636A"
) {
sheet.conditionalBackgroundColor(condRule, condRegion, bgColorHtml);
}

public ExcelSheetFacade<T> noHeader() {
this.noHeader = true;
return this;
Expand Down
32 changes: 31 additions & 1 deletion src/main/resources/com/github/jlangch/venice/excel.venice
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
(defn
^{ :arglists '(
"(copy-row sheet row-from row-to)"
"(copy-row sheet row copy-value copy-style)")
"(copy-row sheet row-from row-to copy-value copy-style)")
:doc
"Copies a specific row in a sheet."
:examples '(
Expand Down Expand Up @@ -817,6 +817,36 @@
(. sheet :copyCellStyle cell-from-row cell-from-col cell-to-row cell-to-col))


(defn
^{ :arglists '(
"(conditional-bg-color sheet rule region bg-color-html)")
:doc "Conditional background color"
:examples '(
"""
(do
(load-module :excel)
(let [wbook (excel/create :xlsx)
sheet (excel/add-sheet wbook "Sheet 1")]
(excel/write-values sheet 1 1 "John" "Doe" 28)
(excel/write-values sheet 2 1 "Sue" "Ford" 26)
(excel/conditional-bg-color sheet "ISBLANK(B1)" "B1:B99" "#CC636A")
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/clear-row", "excel/delete-row",
"excel/copy-row", "excel/copy-row-to-end",
"excel/write-items", "excel/write-item", "excel/cell-formula",
"excel/auto-size-columns", "excel/auto-size-column",
"excel/row-height") }

conditional-bg-color [sheet rule region bg-color-html]

{ :pre [(instance-of? :ExcelSheetFacade sheet)] }

(. sheet :conditionalBackgroundColor rule region bg-color-html))



;; -----------------------------------------------------------------------------
;; Images
Expand Down

0 comments on commit 6f70918

Please sign in to comment.