Search code examples
javascriptoffice-jsoffice-addinsexcel-addins

Office Add-ins Javascript - How to simulate tab or enter key to choose cell inside a range


I am developing an Excel Office Add-ins, I need to select a range, and read the data from each cell of that range when I press Tab key or Enter key (to highlight cell from left to right, top to button). How do I read the highlight cells? The Excel.Range class https://learn.microsoft.com/en-us/javascript/api/excel/excel.range?view=excel-js-preview doesn't have anything like that. Thank you!


Solution

  • Unfortunately, Office JS does not provide a method to change an active cell in a range object, based on your description of the scenario, we have a workaround solution for your scenario.

    You could get the range of the cell after the end-user clicks the button by range.getOffsetRange(rowOffset, columnOffset) and then you could use that range object of this cell to set it active, therefore it would be able to TTS

      await Excel.run(async (context) => {
        const range = context.workbook.getActiveCell();
        var activeCell = range.getOffsetRange(0,1);
        activeCell.select();
    
        await context.sync();
      });