Reputation: 269
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.
Upvotes: 2
Views: 8890
Reputation: 269
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);
});
});
});
Upvotes: 1
Reputation: 13500
The following code example shows how you can get the range of cells that the user has selected, then get the value of the first cell in that range, then display both the address of the selected range and the value of the first cell in that range:
await Excel.run(async (context) => {
var range = context.workbook.getSelectedRange();
range.load(['address', 'values']);
await context.sync();
var firstSelectedCellValue = range.values[0][0];
OfficeHelpers.UI.notify('Selected range is: ' + range.address + '. Value of first cell in that range is: ' + firstSelectedCellValue);
});
You can quickly and easily try this snippet yourself by using Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/kbrandl/39a15d790bf9653dda35b52966faed1e.
Upvotes: 5