How to get the formatting of a Cell using Office.js

北战南征 提交于 2019-12-10 15:43:01

问题


I am developing an Excel Add-In that extracts the text of cell A1 INCLUDING its format and display the text in its own area. So the add in contains this (see screenshot below): • Area to display the formatted text • Button to start the extraction Please Click to view image

bellow is the code that i am using to get the text

function displaySelectedCells() {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
        function (result) {
            if (result.status === Office.AsyncResultStatus.Succeeded) {
                showNotification('The selected text is:', '"' + result.value + '"');
            } else {
                showNotification('Error', result.error.message);
            }
        });
}

The above code is using "CoercionType" as "Text" . I have tried a lot to find the solution to get the text as formatted written in a Sheet Cell. The Response i am getting is plane text only :( Please help me to solve the probelm


回答1:


To add to Michael Saunder's answer: while you can use myRange.format.font.load(['bold','color','italic','name','size','underline']), this will only get you the font, bold, italic, etc. if the formatting is consistent within the cell and in the entire range represented by the myRange object. Since you're only looking for a single cell's format, it doesn't sound like the latter applies to you (it's only a single cell), but the example image you have shows different words within the cell having different bold, italic, color, and so on. With the current APIs you will not be able to get this information.

It sounds like what you're asking is some sort of HTML or image-like representation of the cell. I encourage you to file this as a suggestion on our UserVoice (https://officespdev.uservoice.com/), along with a description of real-world the use-case that you need this for.

~ Michael Zlatkovsky, developer on Office Extensibility team, MSFT




回答2:


The object you should look at is called RangeFont, which is accessed as Range.format.font. Code:

Excel.run(function (ctx) { 
    var myRange = ctx.workbook.getSelectedRange();
    var myFont = myRange.format.font;
    myFont.load(['bold','color','italic','name','size','underline']);
    myRange.load('values');
    return ctx.sync().then(function() {
        log(myFont.color);
        log(myFont.bold);
        log(myRange.values);
    });
}).catch(function(error) {
    log("Error: " + error);
});


来源:https://stackoverflow.com/questions/37571636/how-to-get-the-formatting-of-a-cell-using-office-js

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