Skip to content

Excel Add-in: How to create a range?

I am writing an Excel add-in in JavaScript.

What I need to do is find the currently selected cell, then create a range object that is 4 columns wide and X rows high, with the selected cell at the top left of the selection.

I know how to find the currently selected cell, but I don’t know how to either expand that selection or create a new range that is the size I need.

Any assistance would be appreciated!



The beta Excel.js has a method that does exactly what you want:

rangeObject.getAbsoluteResizedRange(numRows, numColumns);

If rangeObject is the currently selected cell, then rangeObject.getAbsoluteResizedRange(3, 4);

will return a 3 by 4 range with the currently selected cell in the upper left.

In latest production Excel.js, you could use range.getCell() method to get a reference to the desired lower right cell. Then call range.getBoundingRect() and pass the lower right range to it. For info about these, see Range.

UPDATE 1/1/22: getAbsoluteResizedRange is no longer in preview. You can use it in a production add-in.