Nitan Alin
Nitan Alin

Reputation: 83

Apps for Office 365 - Return selected text with styling and formatted

I created an Add-In App for Office365 (Word, Excel and Powerpoint) and i want to get the selected text inside my app, with new lines and styling.

I am using the following code to do that

function getSelectedData(hasSelectionCallback, noSelectionCallback) {

var type = Office.CoercionType.Text;

Office.context.document.getSelectedDataAsync(type,
{
valueFormat: "formatted", filterType: "all"
},
function (asyncResult) {
var error = asyncResult.error;
if (asyncResult.status === Office.AsyncResultStatus.Failed)
{
console.log(error.name + ": " + error.message);
}
else {
// Get selected data.

}
});
}

I tried with all types of Coercion but none does what i actually need.

For example, i want to parse the selected data, so i can know where the new lines are.

For the moment it only returns plain text, with no formatting or styling.

I would appreciate a response for my problem.

Thank you!

Upvotes: 1

Views: 261

Answers (2)

Juan Balmori
Juan Balmori

Reputation: 5036

this is an interesting question. The quick answer to it is that the valueFormat optional parameter of the getSelectedDataAsync method is

  1. Only supported in Excel (Word and PPT ignore this parameter)
  2. Its not intended to get the actual style of the selection (and by that I mean for instance font color, bold, italics, etc). The purpose of this parameter is, more for Value formatting. So for instance if un excel you have a currency column with a "$1,000.00" value the non-formatted value will be 1000.00, and the formatted "$1,000.00". A similar behavior you will see with dates and other formatted values.

I would encourage you to try the new Word And Excel API to get formatting information.

check out a sample for Word here on how you can get the selection formatting details as well of a specific paragraph.

also check this out for range formatting in Excel.

Hope this helps!

Upvotes: 1

Michael Zlatkovsky
Michael Zlatkovsky

Reputation: 8670

In Word, there is the OpenXML (Office.CoercionType.Ooxml) coercion, which will give you everything there is to know about the selection. OOXML is somewhat verbose, but as detailed as you can possibly wish for.

For Excel and PowerPoint, I believe that all you can get is plain text, not any formatting info (though I'm a little surprised you don't get newline info, that seems like it's part of text content rather than formatting...)

Hope this helps!

Upvotes: 0

Related Questions