Highlight greater than values with ONLYOFFICE macro

14 July 2022 By Serge 0 156
Share

Quite often, working with spreadsheets might seem tedious and unrewarding. And that’s when macros come in handy. These little scripts help automate routine tasks and enhance our productivity.

In this blog post, we will talk about a small macro that highlights all the greater than or lower than values.

Highlight greater than values with ONLYOFFICE macro

Reference macro

For reference, we used a VBA macro. This macro invokes an input box where you put the greater than value, and then it searches the selection on the spreadsheet and highlights the targeted cells.

The macro code is the following:

However, ONLYOFFICE macros are JavaScript based, which means they are more versatile and we will use different approaches to tackle this task.

Highlight values with a single color

That would be a simpler option. But on the other hand, this macro can highlight all the lower than values as well. In this case, you can highlight all the greater than values with one color and all the lower than values with another.

So, let’s get started! First, we need to get the active sheet. We achieve that by using the Api.ActiveSheet method. It returns an object that represents an active sheet. And then we target the selection on the spreadsheet:

Now we need to get a value of each cell from the selection. This time we will use the ForEach method. It executes a provided function once for each cell:

Then we add an if statement that will check if the values in the selection are greater than a certain number. In our case, it’s 5. If they are greater than 5, we change the cells’ fill color by using the SetFillColor method. It creates a fill color from the RGB code:

Let’s run our macro.

Highlight greater than values with ONLYOFFICE macro
Alternatively, you can change the font color of the targeted values. To achieve that, we will replace the SetFillColor method with the SetFontColor:

Highlight greater than values with ONLYOFFICE macro
To highlight the lower than values, we change the if statement parameter. Now, if the value of a cell in the selection is lower than 5, the cell background color will change.

Highlight greater than values with ONLYOFFICE macro
Highlight greater than values with ONLYOFFICE macro

Highlight values with multiple colors

Now, let’s take it up a notch and create a macro that will highlight the targeted cells with multiple colors.

First, we need these unique colors. We use CreateColorFromRGB method to generate them and we store these colors in the uniqueColors array:

Then we declare the uniqueColorIndex variable and set it to 0. We will use this variable as an index to select unique colors from the array. To do that, we add an if statement that will go through the array, and increment the index:

Then, we need an if statement that will find the targeted values and apply the unique color to the cells. The concept is similar to the previous macro. We target the active sheet. Then, we get the values of the cells in the selection. And we add an if statement that will look for the greater than or lower than values and highlight them with the SetFillColor method. But this time, the color will be generated with the getColor function:

The full macro code is the following:

Now, let`s run our macro!

Highlight greater than values with ONLYOFFICE macro
Highlight greater than values with ONLYOFFICE macro
And again, alternatively, you can use the SetFontColor method:

Highlight greater than values with ONLYOFFICE macro

This little macro is just one of the many possible implementations of our API methods. ONLYOFFICE macros are extremely versatile, and we welcome you to experiment and create your own scripts. Feel free to share your ideas or macros with us. We are always open to discussion and cooperation. Best of luck in your exploratory endeavors!

Add a comment