Reputation: 4954
I am building an App for Office (for desktop Excel) and I am looking for a function in JavaScript API Office version 1.1 that will return the addresses of the column(s) and the row(s) of a user selection. A result like "A1:C3".
I tried with Office.context.document.getSelectedDataAsync()
but it only gets me the values. I need to know their address so I can display it in my app. My code is like this:
Office.context.document.getSelectedDataAsync(Office.CoercionType.Matrix, function (asyncResult) {
console.log(asyncResult.value);
});
The asyncResult
only gets me an array values. I cannot find any useful help on MSDN or Google. Any help is appreciated.
Upvotes: 4
Views: 5972
Reputation: 11525
This is very late but I hope this alternative can be useful to people working with Excel 2016. You can use the getSelectedRange function on the workbook to get the currently selected range and then load the address property like below.
Excel.run(function (ctx) {
var selectedRange = ctx.workbook.getSelectedRange();
selectedRange.load('address');
return ctx.sync().then(function () {
//selectedRange.address is now available to use
}).catch(function (error) {
//handle
});
}).catch(function (error) {
//handle
});
Upvotes: 5
Reputation: 206
Here's the a complete working example function: Just add a test button to this function. You also need a Div to write your results using the writeToPage function (or amend to your own output area.)
function get_rangecoords() {
Office.context.document.bindings.addFromPromptAsync(Office.BindingType.Matrix,
{ id: "MyMatrixBinding" },
function (asyncResult) {
//NOW DO OUTPUT OR ERROR
if (asyncResult.status === "failed") {
writeToPage("Error get_rangecoords. " + asyncResult.error.message, 3);
}
else {
writeToPage("Added new binding with type: " + asyncResult.value.type + " and id: " + asyncResult.value.id, 1);
}
});
Office.select("bindings#MyMatrixBinding", onBindingNotFound).
addHandlerAsync(Office.EventType.BindingSelectionChanged,
onBindingSelectionChanged,
function (AsyncResult) {
writeToPage("Event handler was added successfully! Change the matrix current selection to trigger the event", 1);
});
//Trigger on selection change, get partial data from the matrix
function onBindingSelectionChanged(eventArgs) {
eventArgs.binding.getDataAsync({
CoercionType: "matrix",
startRow: eventArgs.startRow,
startColumn: eventArgs.startColumn,
rowCount: 1, columnCount: 1
},
function (asyncResult) {
//NOW DO OUTPUT OR ERROR
if (asyncResult.status === "failed") {
writeToPage("Error asyncResult: " + asyncResult.error.message, 3);
}
else {
writeToPage('Start Row:' + eventArgs.startRow + ' Start Col:' + eventArgs.startColumn + '\nSelected Row count:' + eventArgs.rowCount + ', Col Count:' + eventArgs.columnCount + '\nFirst Cell Value:' + asyncResult.value[0].toString(), 1);
}
});
}
//Show error message in case the binding object wasn"t found
function onBindingNotFound() {
writeToPage("The binding object was not found. Please return to previous step to create the binding", 3);
}
}
function writeToPage(text, varimportance) {
if (varimportance == "") {
document.getElementById('Notificationarea').style.color = "black";
}
if (varimportance == 1) {
document.getElementById('Notificationarea').style.color = "darkgreen";
}
if (varimportance == 2) {
document.getElementById('Notificationarea').style.color = "darkorange";
}
if (varimportance == 3) {
document.getElementById('Notificationarea').style.color = "red";
}
document.getElementById('Notificationarea').innerText = text;
}
For more information, see http://microsoft-office-add-ins.com
Upvotes: 1
Reputation: 54
The trick is to create a named item to the whole sheet first and then attach a SelectionChanged handler to it. In its arguments you will get the column, row, height, and width of the selection inside that named item. There is an example by the Microsoft Dev team here:
https://code.msdn.microsoft.com/office/Apps-for-Office-Get-51cc1aac
Upvotes: 0