Apps for Office: Get selection-change coordinates in an Excel table

This sample app shows how to detect when the selection changes in a table (named range) in Excel 2013, and then display the coordinates, relative to the table, of the new selection. It also displays the number of table columns and rows included in the selection.

C# (1.8 MB)
 
 
 
 
 
(0)
1,346 times
Add To Favorites
4/23/2013
E-mail Twitter del.icio.us Digg Facebook
Sign in to Ask a Question


  • JavaScript runtime error: Unable to get property 'type' of undefined or null reference
    4 Posts | Last Post March 18, 2013
    • Hi,
      
      I am using VS2012 Ultimate edition to run the solution and I am getting the following error.
      
      Unhandled exception at line 10, column 13 in http://localhost:16624/Scripts/NewSelectionCoordinates.js
      
      0x800a138f - JavaScript runtime error: Unable to get property 'type' of undefined or null reference.
      
      I am just running the app and hitting the Set Binding button. Any help would be appreciated.
      var message = ('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id); is the line where it breaks.
      
      Regards,
      Mayur.
    • Hi Mayur,
      When running the script the error on line 10 is created by the missing MyRange.
      
      The addFromNamedItemAsync("myRange", "matrix", { id: 'myMatrix' }, function (asyncResult) 
      
      If you select a few cells and create them as MyRange you will see the results of the message.
      
      Alternativly you can also add error handeling by adding a function when myRange is missing.
      
      I hope this helps.
      Regards,
      Menno
    • Thanks Menno. Now I understand how it works. I have one more question on that though. Is it possible for me to lock/format/give a background color to a current selected cell with the help of the coordinated?
      
      Regards,
      Mayur.
    • Dear MayurParmar, If you add the following to the first few lines you will not get an error:
              if (asyncResult.status == 'succeeded'){
      var message = ('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
      write(message);
      addEventHandler();
                  }
      else
      write('Error: ' + asyncResult.error.message);
          });
      }
      
      It is possible to write back values to the current selected cell. Maybe you can start with the following and instead of just values add other properties like formats:
      
      function addTextToDocument() {
      
          var e = document.getElementById("input");
          var text = e.value;
      
          Office.context.document.setSelectedDataAsync(text,
              function (asyncResult) {});
      }
      
      Maybe you can change the e.value to e.format. I only used the setSelectedeDataAsync recently in a example. After running the following tutorial the created functions became clear to me. http://msdn.microsoft.com/en-us/library/office/apps/jj220065(v=office.15)
      
      Hope these help.
      Regards,
      Menno
  • Error when opening Solution 'NewSelectenCoordinates'
    2 Posts | Last Post August 20, 2012
    • I am using VS 2012 RC Ultimate and get the following error message when attmpting to open the solution.
       'AgaveApp' is not a valid value for the project property 'ProjectMode'
      Please advise. 
      ~ Bruce
    • Hi Bruce--
      
      Thanks for your comment, and sorry about the trouble you were having. This issue was brought to our attention only recently, and just today we were able to publish a revised version of this sample. The new version is here:
      
      http://code.msdn.microsoft.com/officeapps/Apps-for-Office-Get-51cc1aac
      
      Please let us know if you continue to have any problems running this or any other of our samples!
      
      Thanks!
      
      Saul