How do I select a range and change font color of specific words in Outlook web add-ins

99封情书 提交于 2021-02-07 11:00:03

问题


In Outlook web add-in, I want to select text from email body, keep it as tracked object and then apply styling on it like we do in Word web add-in. In the Word, I do it the following way:

 //used to store current selected range 
var selection=null;

$scope.getSelectedText = function () {
     Word.run(function (context) {
            if (selection) {
                context.trackedObjects.remove(selection);
            }
            selection = context.document.getSelection();
            context.trackedObjects.add(selection);
            context.load(selection, 'text');
            return context.sync()
                .then(function () {
                    if (!selection.text) {
                        systemService.errorHandler("Please select any text from the document");
                        return false;
                    }
                }).then(context.sync);
        })
         .catch(systemService.errorHandler);
    };

//highlight Words in the selected range or body
   $scope.highlightWords = function (item) {

    var color = systemService.getColor(item.gradeText);
    var filteredWords = JSON.parse(localStorage.getItem("List")) || [];
    var promise = new OfficeExtension.Promise(function (resolve, reject) { resolve(null); });

    Word.run(function (context) {
        var selectedRange = (selection) ? selection : context.document.body;
        selectedRange.load('text');
        return context.sync().then(function () {

            if (selectedRange.text == "") {
                systemService.showNotification("Alert:", "No text found in the document!");
                return;
            }

            filteredWords.forEach(function (word) {
                promise = promise.then(function () {
                    return highlightRange(word, color);
                })
            });
        });

    }).catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

function highlightRange(word, color) {
    return Word.run(selection, function (context) {
        var range = (selection) ? selection : context.document.body;
        var searchResults = null;
        searchResults = range.search(word, { ignorePunct: true, matchCase: false, matchWholeWord: true });

        searchResults.load('font');
        return context.sync().then(function () {
            for (var i = 0; i < searchResults.items.length; i++) {
                searchResults.items[i].font.color = color;
                searchResults.items[i].font.bold = true;
            }
        }).then(context.sync());

    }).catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}
//highlight Words in the selected range or body

I have tried multiple ways to do but there is a problem while keeping track of the selected object. In Word, we can save the range and manipulate it overtime as needed, but in Outlook, the user have to keep the content selected in order to perform operation on it (like changing font color etc.). Also, do we have any font property available for outlook web add-ins like Word?

来源:https://stackoverflow.com/questions/61305382/how-do-i-select-a-range-and-change-font-color-of-specific-words-in-outlook-web-a

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