How to add indexes to a selection in ONLYOFFICE spreadsheets using a macro
Spreadsheets are all about numbers, right? Well, sometimes the default indexing just won’t do. That’s where our time-saving macro comes in. It’ll neatly index all the rows in your selection, making your spreadsheet workflow a whole lot simpler.
Building the macro
Let’s start crafting our indexing macro:
const oWorksheet = Api.GetActiveSheet();
const oRange = oWorksheet.GetSelection();
First, we get the active sheet in the oWorksheet variable, and get the selection in the oRange variable.
let index = 0;
Next, we declare a variable index which will help us iterate through all the cells in the selection.
oRange.ForEach(function (range) {
index++;
oWorksheet.GetRange(range).Insert("right");
oFormat = `${index})`;
oWorksheet.GetRange(range).SetValue(oFormat);
});
Then, we use a for loop, which pushes all values in the selection one column right, and adds the indexing in the now empty column. We increment the index variable on each iteration of the for loop.
The full macro code
Here is the full code for the macro:
(function () {
const oWorksheet = Api.GetActiveSheet();
const oRange = oWorksheet.GetSelection();
let index = 0;
oRange.ForEach(function (range) {
index++;
oWorksheet.GetRange(range).Insert("right");
oFormat = `${index})`;
oWorksheet.GetRange(range).SetValue(oFormat);
});
})();
Now, let’s run our macro and see it in action!
We hope this macro will help you index your data where ever there is a need, and save you some time by avoiding indexing the data manually.
Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is your key to transforming your ideas into reality. If you have any questions or innovative concepts, we encourage you to share them with us. Your input is highly valued, and we are excited about the possibility of collaborating with you. Best of luck in your exploratory endeavors!
Create your free ONLYOFFICE account
View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.