Skip to content

Commit

Permalink
added excel functions to remove formulas, comments, and hyperlinks from
Browse files Browse the repository at this point in the history
cells
  • Loading branch information
jlangch committed Sep 8, 2024
1 parent 5348e3f commit 1766202
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public DocSection section() {
formulas.addItem(diBuilder.getDocItem("excel/cell-formula", false));
formulas.addItem(diBuilder.getDocItem("excel/sum-formula", false));
formulas.addItem(diBuilder.getDocItem("excel/evaluate-formulas", false));
formulas.addItem(diBuilder.getDocItem("excel/remove-formula", false));

final DocSection style = new DocSection("Styles", id());
all.addSection(style);
Expand All @@ -157,6 +158,14 @@ public DocSection section() {
all.addSection(image);
image.addItem(diBuilder.getDocItem("excel/add-image", false));

final DocSection comments = new DocSection("Comments", id());
all.addSection(comments);
comments.addItem(diBuilder.getDocItem("excel/remove-comment", false));

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

final DocSection charts = new DocSection("Charts", id());
all.addSection(charts);
charts.addItem(diBuilder.getDocItem("excel/add-line-chart", false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,27 @@ 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();
}
}

public void removeHyperlink(final int row, final int col) {
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();
}
}

public Map<String,Object> getCellStyleInfo(final int row, final int col) {
final Map<String,Object> info = new LinkedHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ public Map<String,Object> getCellStyleInfo(final int row1, final int col1) {
return sheet.getCellStyleInfo(row1-1, col1-1);
}

public void removeFormula(final int row1, final int col1) {
sheet.removeFormula(row1-1, col1-1);
}

public void removeHyperlink(final int row1, final int col1) {
sheet.removeHyperlink(row1-1, col1-1);
}

public void removeComment(final int row1, final int col1) {
sheet.removeComment(row1-1, col1-1);
}


// ------------------------------------------------------------------------
// Reader functions
Expand Down
109 changes: 108 additions & 1 deletion src/main/resources/com/github/jlangch/venice/excel.venice
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,6 @@
(. :PieDataSeries :new "" data-address-range))



;; -----------------------------------------------------------------------------
;; Formulas
;; -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1726,6 +1725,114 @@
(. sheet :sumFormula row-from row-to col-from col-to))


(defn
^{ :arglists '("(remove-formula sheet row col)")
:doc "Remove a cell formula"
:examples '(
"""
(do
(load-module :excel)
(let [data [ {:a 100 :b 200 }
{:a 101 :b 201 }
{:a 102 :b 202 } ]
wbook (excel/create :xlsx)
sheet (excel/add-sheet wbook "Sheet 1" { :no-header-row true })]
(excel/add-column sheet "A" { :field :a })
(excel/add-column sheet "B" { :field :b })
(excel/add-column sheet "C" { :field :c })
(excel/write-items sheet data)
(excel/cell-formula sheet 1 3 (excel/sum-formula sheet 1 1 1 2))
(excel/cell-formula sheet 2 3 (excel/sum-formula sheet 2 2 1 2))
(excel/cell-formula sheet 3 3 (excel/sum-formula sheet 3 3 1 2))
(excel/remove-formula sheet 1 3)
(excel/evaluate-formulas wbook)
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/remove-comment"
"excel/remove-hyperlink") }

remove-formula [sheet row col]

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

(. sheet :removeFormula row col))



;; -----------------------------------------------------------------------------
;; Comments
;; -----------------------------------------------------------------------------

(defn
^{ :arglists '("(remove-comment sheet row col)")
:doc "Remove a cell comment"
: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" 45)
(excel/write-values sheet 2 1 "Sue" "Ford" 26)
(excel/remove-comment sheet 1 1)
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/remove-formula"
"excel/remove-hyperlink") }

remove-comment [sheet row col]

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

(. sheet :removeComment row col))



;; -----------------------------------------------------------------------------
;; Hyperlinks
;; -----------------------------------------------------------------------------

(defn
^{ :arglists '("(remove-hyperlink sheet row col)")
:doc "Remove a cell comment"
: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" 45)
(excel/write-values sheet 2 1 "Sue" "Ford" 26)
(excel/remove-hyperlink sheet 1 1)
(excel/auto-size-columns sheet)
(excel/write->file wbook "sample.xlsx")))
""" )
:see-also '(
"excel/remove-comment"
"excel/remove-formula") }

remove-hyperlink [sheet row col]

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

(. sheet :removeHyperlink row col))



;; -----------------------------------------------------------------------------
;; Utils
;; -----------------------------------------------------------------------------

(defn
^{ :arglists '("(cell-address sheet row col)")
:doc "Returns the cell address in A1 style for a cell at row/col in a sheet"
Expand Down

0 comments on commit 1766202

Please sign in to comment.