none
Excel add-in JavaScript - Get last selected cell RRS feed

  • Question

  • I'm trying to be able to get the details of the last selected cell - value, address, etc.
    So far, I couldn't achieve this - the closest I got to is getting the currently selected cell, which apparently doesn't serve my needs.

    Thanks!

    Monday, August 7, 2017 7:31 AM

Answers

  • Hello,

    If you want to get previous selected cell, you could test the following code.

     var lastRangeAddress;
     function addEventHandlerToDocument() {
            Excel.run(function (ctx) {
                var PreviousCell = ctx.workbook.getSelectedRange().load("address");
                return ctx.sync().then(function () {
                    lastRangeAddress = PreviousCell.address;
                });
            }).catch(errorHandler);
            Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, MyHandler);
        }
    
        function MyHandler(eventArgs) {  
            Excel.run(function (ctx) {
                var sourceRange = ctx.workbook.getSelectedRange().load("address");
                return ctx.sync().then(function () {
                    showNotification("Previous cell",lastRangeAddress);
                    lastRangeAddress = sourceRange.address;              
                });       
            })
            .catch(errorHandler);
        }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by avi12641 Tuesday, August 8, 2017 4:26 AM
    Tuesday, August 8, 2017 3:11 AM
    Moderator

All replies

  • Hello,

    If you want to get previous selected cell, you could test the following code.

     var lastRangeAddress;
     function addEventHandlerToDocument() {
            Excel.run(function (ctx) {
                var PreviousCell = ctx.workbook.getSelectedRange().load("address");
                return ctx.sync().then(function () {
                    lastRangeAddress = PreviousCell.address;
                });
            }).catch(errorHandler);
            Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, MyHandler);
        }
    
        function MyHandler(eventArgs) {  
            Excel.run(function (ctx) {
                var sourceRange = ctx.workbook.getSelectedRange().load("address");
                return ctx.sync().then(function () {
                    showNotification("Previous cell",lastRangeAddress);
                    lastRangeAddress = sourceRange.address;              
                });       
            })
            .catch(errorHandler);
        }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by avi12641 Tuesday, August 8, 2017 4:26 AM
    Tuesday, August 8, 2017 3:11 AM
    Moderator
  • Thank you so much! It solved my problem :)
    Tuesday, August 8, 2017 4:26 AM