Document

An abstract class that represents the document the add-in is interacting with.
Event - ActiveViewChanged


addHandlerAsyncMAdds an event handler for a Document object event.
bindingsPGets an object that provides access to the bindings defined in the document.
customXmlPartsPGets an object that represents the custom XML parts in the document.
getActiveViewAsyncM(PowerPoint only) Returns the state of the current view of the presentation (edit or read).
getFileAsyncMUpdated in 1.1 to get the document as PDF with add-ins for PowerPoint and Word.
(fileType [,options] [,callback])
Supports PDF file type.
Returns the entire document file in slices of up to 4194304 bytes (4 MB). For add-ins on iPad, file slice is supported up to 65536 (64 KB). Note that specifying file slice size of above permitted limit will result in an "Internal Error" failure.
getFilePropertiesAsyncM(Added 2013, SP1) Gets file properties of the current document.
getMaxResourceIndexAsyncM(Project only) Get the maximum index of the collection of resources in the current project.
getMaxTaskIndexAsyncM(Project only)
getProjectFieldAsyncM(Project only)
getResourceByIndexAsyncM(Project only)
getResourceFieldAsyncM(Project only)
getSelectedDataAsyncMReads the data contained in the current selection in the document.
getSelectedResourceAsyncM(Project only)
getSelectedTaskAsyncM(Project only)
getSelectedViewAsyncM(Project only)
getTaskAsyncM(Project only)
getTaskByIndexAsyncM(Project only)
getTaskFieldAsyncM(Project only)
getWSSUrlM(Project only)
goToByIdAsyncM(Added 2013, SP1) Named Ranges
modePGets the mode the document is in.
removeHandlerAsyncM(eventType [,options] [,callback])
SelectionChangedE 
setResourceFieldAsyncM(Project only)
setSelectedDataAsyncM(data [,options] [,callback(asyncResult)])
setTaskFieldAsyncM(Project only)
settingsPGets an object that represents the saved custom settings of the content or task pane add-in for the current document.
urlPGets the URL of the document that the host application currently has open. Returns null if the URL is unavailable.

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){});

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