admin管理员组文章数量:1323323
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.
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.
3 Answers
Reset to default 5This 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
});
Here's the a plete 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.
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./office/Apps-for-Office-Get-51cc1aac
本文标签: Get Excel range using JavaScript API for OfficeStack Overflow
版权声明:本文标题:Get Excel range using JavaScript API for Office - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140924a2422575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论