Skip to content

Commit

Permalink
Merge pull request 'feature/Sum_Of_Highlighted_Cells' from feature/Su…
Browse files Browse the repository at this point in the history
…m_Of_Highlighted_Cells into develop

Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/api.onlyoffice.com/pulls/85
  • Loading branch information
LinneyS committed Dec 12, 2024
2 parents 67ef694 + 3372b26 commit c111072
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- docs api: added the Checking PDF forms page
- docspace backend: added the Removed user page
- macros: added remove extra spaces in document macro sample
- macros: added the Sum of highlighted cells macro sample

## 6.3.0
- docspace oauth api: added a new section
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -13
order: -14
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -1
order: -2
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -20
order: -21
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -19
order: -20
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -3
order: -4
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -9
order: -10
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -14
order: -15
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -12
order: -13
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -8
order: -9
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -10
order: -11
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -4
order: -5
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -18
order: -19
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -17
order: -18
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -11
order: -12
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -7
order: -8
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -5
order: -6
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -2
order: -3
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -15
order: -16
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
order: -1
---

## Description

Sums the answer of all highlighted cells in a spreadsheet.

<!-- This code snippet is shown in the screenshot. -->

<!-- eslint-skip -->

``` ts
(function () {
const oWorksheet = Api.GetActiveSheet();
const range1 = oWorksheet.GetRange("B1"); // Set your range for the color reference
const colorReference = range1.SetFillColor(Api.CreateColorFromRGB(91, 155, 213)); // Set targeted background color. To use fill color from the existing range, comment this line out
const targetedColor = range1.GetFillColor()
const range2 = oWorksheet.GetRange("A1:A16"); // Set the targeted range in the spreadsheet
const result = oWorksheet.GetRange("A17"); // Set the cell where the result will be displayed
let sum = 0;
let cellColorCode;

range2.ForEach(function (range) {
const cellColor = range.GetFillColor();

if (cellColor!== "No Fill"){
cellColorCode = cellColor.GetRGB()
} else {
cellColorCode = null;
}

if (cellColorCode && cellColorCode === targetedColor.GetRGB()) {
const value = range.GetValue();
if (!isNaN(parseFloat(value))) {
sum += parseFloat(value);
}
}
});
result.SetValue(`The sum: ${sum}`)
})();
```

Methods used: GetActiveSheet, GetRange, SetFillColor, CreateColorFromRGB, GetFillColor, ForEach, GetRGB, GetValue

## Reference Microsoft VBA macro code

``` vb
Sub SumHighlightedCells()
Dim cell As Range
Dim total As Double

' Loop through each cell in the selection
For Each cell In Selection
' Check if the cell contains a numeric value
If IsNumeric(cell.Value) Then
total = total + cell.Value
End If
Next cell

' Display the sum in a message box
MsgBox "The sum of the highlighted cells is: " & total
End Sub
```

## Result

![Sum of highlighted cells](/assets/images/plugins/sum-of-highlighted-cells.png)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -6
order: -7
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -16
order: -17
---

## Description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: -21
order: -22
---

## Description
Expand Down
6 changes: 6 additions & 0 deletions site/pages/Docs/Plugin and Macros/Macros/Samples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ In this example we are removing extra spaces in the text document.

[More](Remove%20extra%20spaces)

## Sum of highlighted cells

In this example we are summing the answer of all highlighted cells in a spreadsheet.

[More](Sum%20of%20highlighted%20cells/index.md)

## Support

If you want to request a feature or report a bug regarding macros, use the issues section [on GitHub.](https://github.com/ONLYOFFICE/plugin-macros/issues)
Expand Down

0 comments on commit c111072

Please sign in to comment.