Document


bindingsP  
customXmlPartsP  
modeP  
settingsP  
urlP  
addHandlerAsyncM  
getActiveViewAsyncM  
getFileAsyncMUpdated in 1.1 to get the document as PDF with add-ins for PowerPoint and Word.
(fileType [,options] [,callback])
 
getFilePropertiesAsyncM(Added 2013, SP1) 
getSelectedDataAsyncM  
goToByIdAsyncM(Added 2013, SP1) Named Ranges 
removeHandlerAsyncM(eventType [,options] [,callback]) 
setSelectedDataAsyncM(data [,options] [,callback(asyncResult)]) 
ActiveViewChangedE  
SelectionChangedE  

Current Selection

The document object exposes two methods that lets you read and write to the current selection in a spreadsheet or document.
getSelectedDataAsync and setSelectedDataAsync


Read from Current Selection

Read a matrix from the current selection in the document and display its contents

Office.context.document.getSelectedDataAsync("matrix", 
                                             function (asyncResult) {
  if (asyncResult.status == "failed") {
    showMessage("Action failed with error: " + asyncResult.error.message);
  }
  else {
    showMessage("Selected data: " + asyncResult.value);
  }
});

The following example uses the getSelectedDataAsync method to read the currently selected range of cells. To specify that the selection is read as a range of cells, specify the coerctionType parameter as CoercionType.Matrix. The callback function returns an object containing the data currently selected by the user, which can use the asyncResult.value property to access the data.


Write to Current Selection

var myMatrix = [["1","2","3"],["4","5","6"],["7","8","9"]]; 

// Set myMatrix in the document.
Office.context.document.setSelectedDataAsync(myMatrix,
                                             function (asyncResult) {
  if (asyncResult.status == "failed") {
    showMessage("Action failed with error: " + asyncResult.error.message);
  }
});

Be aware that a matrix can only be written into the spreadsheet if no other data in the surrounding cells will be overwritten.


Matrix or Table type ?
If your data has a heading row then you must use Table
If you need your data to grow dynamically when rows and columns are added then you must use Table
Adding rows and columns in supported in Matrix but appending is only supported in Table


Detecting Changes in the Current Selection

You can detect changes in the selection by adding an event handler for the SelectionChanged event
You can either pass the enumeration or a text string "documentSelectionChanged"


Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, 
Office.context.document.addHandlerAsync("documentSelectionChanged",
                                             myHandlerName,
                                             function (asyncResult) {}
);

// Event Handler Function
function myHandlerName(eventArgs){
   write('Document Selection Changed');
}

// Function that writes to a div with id='message' on the page
function write(message){
   document.getElementById('message').innerText += message;
}

The argument passed to the event handler contains a reference to a DocumentSelectionChangedEventArgs object which can be used to access the document that raised the event


Stop Detection Changes in Selection

You can stop listening to events by removing the handler

Office.context.document.removeHandlerAsync(Office.EventType.DocumentSelectionChanged, {handler:MyHandlerName}, function(result){}); 

© 2018 Better Solutions Limited. All Rights Reserved. © 2018 Better Solutions Limited TopPrevNext