Search code examples
office-jsoffice-addinsexceljs

get selected cell's value and address in Office.js excel


I am working on a Excel Web Add-In using Office.js. I need to get selected cell's Address and Value. Please be clear that here user will select only one cell.

This should happen when user click on cell in worksheet.


Solution

  • I solved my problem by following code

    Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged,
                function (eventArgs) {
                    Excel.run(function (ctx) {
                        var range = ctx.workbook.getSelectedRange();
                        range.load(['address', 'values']);
                        return ctx.sync().then(function () {
                            showNotification("", range.values[0][0] + " Address:" + range.address);
                        });
                    });                    
                });