Ewa.NamedItem.activateAsync(callback, userContext)
Applies to: apps for SharePoint | SharePoint Server 2010
In this article
Return Value
Remarks
Example
Activates the named item.
Ewa.NamedItem.activateAsync(callback, userContext);
Parameters
callback
The function that is called when the request is executed.
userContext
An object provided as a way for callers to pass state through the asynchronous call.
Return Value
None.
Remarks
The activateAsync method activates the specified named item. Activation works only in named item view mode. In sheet view mode this method fails silently.
Example
The following code example shows how to add a button to the page. Each time the button is clicked, the next named item in the collection of named items for the workbook is activated and a message is displayed in the browser status bar specifying the type of the activated named item.
<script type="text/javascript">
var ewa = null;
var itemCount = 1;
// Add event handler for onload event.
if (window.attachEvent)
{
window.attachEvent("onload", ewaOmPageLoad);
}
else
{
window.addEventListener("DOMContentLoaded", ewaOmPageLoad, false);
}
// Add event handler for applicationReady event.
function ewaOmPageLoad()
{
Ewa.EwaControl.add_applicationReady(getEwa);
}
function getEwa()
{
// Get a reference to the Excel Services Web Part.
ewa = Ewa.EwaControl.getInstances().getItem(0);
}
function activateNamedItemsButton()
{
// Get a reference to the workbook.
var wkBook = ewa.getActiveWorkbook();
// Only run if in Named Item view
if (wkBook.getIsNamedItemView())
{
// Get the collection of named items in the workbook.
var items = wkBook.getNamedItems();
// Get the next named item.
var item = items.getItem(itemCount);
itemCount++;
if (itemCount >= items.getCount())
{
itemCount = 0;
}
// Activate the specified named item.
// Pass in named item as userContext.
item.activateAsync(activateNamedItemsCallBack, item);
}
else
{
alert("Not in NamedItem view.");
}
}
// Get NamedItemType as string.
function getNamedItemTypeAsString(type)
{
var myType = null;
// Determine the NamedItemType of the Named Item.
switch(namedItem.getNamedItemType())
{
case Ewa.NamedItemType.NamedRange:
myType = "NamedRange";
break;
case Ewa.NamedItemType.Parameter:
myType = "Parameter";
break;
case Ewa.NamedItemType.Table:
myType = "Table";
break;
case Ewa.NamedItemType.PivotTable:
myType = "PivotTable";
break;
case Ewa.NamedItemType.Chart:
myType = "Chart";
break;
}
return myType;
}
function activateNamedItemsCallBack(asyncResult)
{
// Get named item from userContext.
var item = asyncResult.getUserContext();
// Get NamedItemType as string.
var type = getNamedItemTypeAsString(item.getNamedItemType());
window.status = "Named item with NamedItemType of " + type + " was activated.";
}
</script>
<input type="button" id="ActivateNamedItems" value="Activate Named Items" onclick="activateNamedItemsButton()" />
In sheet view mode you can activate a particular named item by activating the underlying range that contains the named item as shown in the following code example.
// ...
var items = wkBook.getNamedItems();
// Get the named item named "ColorList".
var item = items.getItemByName('ColorList');
// Get the range of the named item asynchronously.
item.getRefersToRangeAsync(getRefersToRangeCallBack, null);
function getRefersToRangeCallBack(asyncResult)
{
// Activate the range of the named item asynchronously.
asyncResult.getReturnValue().activateAsync(null, null);
}