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

天大地大妈咪最大 提交于 2019-12-11 11:07:48

问题


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!


回答1:


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!




回答2:


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!



来源:https://stackoverflow.com/questions/36762654/apps-for-office-365-return-selected-text-with-styling-and-formatted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!