diff --git a/ChangeLog.md b/ChangeLog.md index d006390d1..2c00775b5 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. -## [1.12.34] - 2024-09-xx +## [1.12.35] - 2024-09-xx ### Added @@ -16,10 +16,18 @@ 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 (...) + + + +## [1.12.34] - 2024-09-10 + +### Added + - 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 +- function `cell-lock` to :excel module diff --git a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleExcelSection.java b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleExcelSection.java index 75e56e51b..b18e6fd3b 100644 --- a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleExcelSection.java +++ b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleExcelSection.java @@ -89,6 +89,7 @@ public DocSection section() { final DocSection cells = new DocSection("Cells", id()); all.addSection(cells); cells.addItem(diBuilder.getDocItem("excel/cell-empty?", false)); + cells.addItem(diBuilder.getDocItem("excel/cell-lock", false)); cells.addItem(diBuilder.getDocItem("excel/cell-locked?", false)); cells.addItem(diBuilder.getDocItem("excel/cell-hidden?", false)); cells.addItem(diBuilder.getDocItem("excel/cell-type", false)); diff --git a/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelCellStyles.java b/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelCellStyles.java index 2cbb0298f..019593538 100644 --- a/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelCellStyles.java +++ b/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelCellStyles.java @@ -193,6 +193,10 @@ public CellStyle getCellStyle(final String name) { return (name != null) ? cellStyles.get(name) : null; } + public CellStyle createCellStyle() { + return workbook.createCellStyle(); + } + public Font getFont(final CellStyle style) { return style == null ? null : workbook.getFontAt(style.getFontIndex()); } diff --git a/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelSheet.java b/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelSheet.java index 8533870c8..00a51c0eb 100644 --- a/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelSheet.java +++ b/src/main/java/com/github/jlangch/venice/impl/util/excel/ExcelSheet.java @@ -522,14 +522,32 @@ public String getDataFormatString(final int row, final int col) { return getDataFormatString(cell); } + public void lock(final int row, final int col, final boolean locked) { + final Cell cell = getCell(row, col); + if (cell != null) { + final CellStyle lockedCellStyle = cellStyles.createCellStyle(); + lockedCellStyle.cloneStyleFrom(cell.getCellStyle()); + lockedCellStyle.setLocked(locked); + cell.setCellStyle(lockedCellStyle); + } + } + public boolean isLocked(final int row, final int col) { final Cell cell = getCell(row, col); - return isLocked(cell); + if (cell == null) { + return false; + } + final CellStyle style = cell.getCellStyle(); + return style == null ? false : style.getLocked(); } public boolean isHidden(final int row, final int col) { final Cell cell = getCell(row, col); - return isHidden(cell); + if (cell == null) { + return false; + } + final CellStyle style = cell.getCellStyle(); + return style == null ? false : style.getHidden(); } public boolean isColumnHidden(final int col) { @@ -1317,24 +1335,6 @@ private String getDataFormatString(final Cell cell) { return style.getDataFormatString(); } - private boolean isLocked(final Cell cell) { - if (cell == null) { - return false; - } - - final CellStyle style = cell.getCellStyle(); - return style == null ? false : style.getLocked(); - } - - private boolean isHidden(final Cell cell) { - if (cell == null) { - return false; - } - - final CellStyle style = cell.getCellStyle(); - return style == null ? false : style.getHidden(); - } - public String getCellType(final CellType type) { if (type == CellType.BLANK) { return "blank"; diff --git a/src/main/java/com/github/jlangch/venice/util/excel/ExcelSheetFacade.java b/src/main/java/com/github/jlangch/venice/util/excel/ExcelSheetFacade.java index 75a35521f..b188bed0c 100644 --- a/src/main/java/com/github/jlangch/venice/util/excel/ExcelSheetFacade.java +++ b/src/main/java/com/github/jlangch/venice/util/excel/ExcelSheetFacade.java @@ -94,6 +94,10 @@ public String getCellType(final int row1, final int col1) { return sheet.getCellType(row1-1, col1-1); } + public void lock(final int row1, final int col1, final boolean locked) { + sheet.lock(row1-1, col1-1, locked); + } + public boolean isLocked(final int row1, final int col1) { return sheet.isLocked(row1-1, col1-1); } diff --git a/src/main/resources/com/github/jlangch/venice/excel.venice b/src/main/resources/com/github/jlangch/venice/excel.venice index 007575e59..ed8101384 100644 --- a/src/main/resources/com/github/jlangch/venice/excel.venice +++ b/src/main/resources/com/github/jlangch/venice/excel.venice @@ -3674,11 +3674,57 @@ (. sheet :getErrorCode row col)) +(defn + ^{ :arglists '("(cell-lock sheet row col locked?)") + :doc + """ + Locks/unlocks a cell. + + Note: Excel locks new cells by default. + """ + :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/cell-lock sheet 1 1 false) + (excel/cell-lock sheet 1 2 false) + (excel/cell-lock sheet 1 3 true) + (excel/auto-size-columns sheet) + (excel/write->file wbook "sample.xlsx")) + + (let [wbook (excel/open "sample.xlsx") + sheet (excel/sheet wbook "Sheet 1")] + [(excel/cell-locked? sheet 1 1) + (excel/cell-locked? sheet 1 2) + (excel/cell-locked? sheet 1 3)])) + """ ) + :see-also '( + "excel/cell-locked?", + "excel/cell-hidden?", + "excel/cell-empty?", + "excel/cell-type" ) } + + cell-lock [sheet row col locked?] + + { :pre [(instance-of? :ExcelSheetFacade sheet) + (long? row) (pos? row) + (long? col) (pos? col) + (boolean? locked?)] } + + (. sheet :lock row col locked?)) + + (defn ^{ :arglists '("(cell-locked? sheet row col)") :doc """ Returns true if the sheet cell is locked else false. + + Note: Excel locks new cells by default. """ :examples '( """ @@ -3688,20 +3734,28 @@ (let [wbook (excel/create :xlsx) sheet (excel/add-sheet wbook "Sheet 1")] (excel/write-values sheet 1 1 "John" "Doe" 28) + (excel/cell-lock sheet 1 1 false) + (excel/cell-lock sheet 1 2 false) + (excel/cell-lock sheet 1 3 true) (excel/auto-size-columns sheet) (excel/write->file wbook "sample.xlsx")) (let [wbook (excel/open "sample.xlsx") sheet (excel/sheet wbook "Sheet 1")] - (excel/cell-locked? sheet 1 1))) + [(excel/cell-locked? sheet 1 1) + (excel/cell-locked? sheet 1 2) + (excel/cell-locked? sheet 1 3)])) """ ) :see-also '( - "excel/cell-hidden?", "excel/cell-empty?", "excel/cell-type" ) } + "excel/cell-lock", + "excel/cell-empty?", + "excel/cell-type" ) } cell-locked? [sheet row col] { :pre [(instance-of? :ExcelSheetFacade sheet) - (long? row) (long? col) (pos? row) (pos? col)] } + (long? row) (pos? row) + (long? col) (pos? col)] } (. sheet :isLocked row col)) @@ -3733,7 +3787,8 @@ cell-hidden? [sheet row col] { :pre [(instance-of? :ExcelSheetFacade sheet) - (long? row) (long? col) (pos? row) (pos? col)] } + (long? row) (pos? row) + (long? col) (pos? col)] } (. sheet :isHidden row col))