I have just posted a sample of how to hook the DocumentSelectionChanged event in the new Apps for Office programming model.
To test our my new-found knowledge I slapped together a quick little demonstration which runs inside Excel 2013 records the various cell selections that users make. You can pull down the source from the ExcelSelectionChangedDemo repository on GitHub.
To get up and running with this on your local machine you are going to need a few things:
Given I am using a Git source code repository hosted up on GitHub you might also like to consider downloading the Git extensions for Visual Studio.
In the screenshot above you can see the previously referenced code running in Excel 2013. The code that drives this is in the Gist below.
The SelectionChanged method handles the callback (line 27) and then uses JSOM to fetch the data that has been selected. This is a two step process. First you get notified that a data selection changed, then you ask the object model to give you the selected data via getSelectedDataAsync. As the name suggests, this is method asynchronously fetches the data so a callback is provided (in this case, DataSelected).
The rest of the code in the ExcelSelectionChangedDemo.js file is just code which drives Knockout.js to deliver data-binding. Each time the selection changes inside the Excel application a new element is added with a JSON representation of the AsyncResult object. Selecting any cell, or multiple cells in a rectangle on the screen should result in a successful output. If you use CTRL-click and select multiple random cells around the screen you should be able to trigger an failure.
I’m going to keep looking into the new application story around Office and SharePoint. I think it is a really positive step forward for Microsoft which might help win back some mind-share from web-developers. I hope the same that I created helps you get started too.
Thanks to Michael Stokesbary, Yina Arenas, and Russell Palmer for running the event. Also I just noticed that one of my colleagues at Readify, Jake Ginnivan is doing a presentation titled “Removing the shackles of the past – Office and SharePoint development in the Cloud” in Perth on the 12th of February. Check it out if you are interested and in the area.