【问题标题】:How do I get the formatting the Current cell of the table in Word using office.js如何使用 office.js 格式化 Word 中表格的当前单元格
【发布时间】:2016-06-28 12:35:42
【问题描述】:

我在 excel 中问过一个关于 How to get the formatting of a Cell using Office.js 的问题。我又遇到了同样的问题,但这次是关于 ms-word,我有可能获得在 word 应用程序中创建的表格单元格中的格式化文本。

虽然我能够将选定的文本作为 html 获取,这给了我所需的样式

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

我只想要当前单元格格式的文本 谢谢!

【问题讨论】:

    标签: javascript office-js office-app


    【解决方案1】:

    优秀的问题Pradeep。为了获得单元格格式,您需要使用当前预览的 Word 1.3 API。 您可以查看如何试用 1.3 API here。 (注意,您需要使用该页面上公开的预览版 Office.js CDN!) 检查所有新功能here

    现在,一旦您准备好尝试 1.3,以下代码将为您提供单元格格式信息。一般来说,代码在做什么

    1. 验证所选内容是否在表格的单元格内。
    2. 验证完成后,我们将获得单元的“主体”。 Body 对象包含 FONT 对象,它将包含您需要的所有格式 properties。(即颜色、字体名称、粗体、斜体等。您还可以使用 getHtml() 函数获取正文的 HTML。

    找到下面的代码来做到这一点。请注意,此代码将返回您在整个单元格中应用的格式。如果你想更上一层楼,也就是逐字逐句获取格式信息,那么你需要在 body 对象上应用split 方法,这样你就可以遍历并获取每个对象的格式信息返回的范围。 希望这有帮助!编码愉快!

    function getFormattedText() {
    
            Word.run(function (context) {
                //step 1: lets get the selection's range. and find out if its within a table cell.....
                var mySelection = context.document.getSelection().parentTableCell;
                context.load(mySelection);
                return context.sync()
                .then(function () {
                    if (mySelection.isNull == true) {
                        //selection is not within a cell..... 
                        console.log("selection not in a header");
    
    
                    }
                    else {
    
                        // the selection is inside a cell! lets get the content....
                        var body = mySelection.body;
                        context.load(body, { expand: 'font' });
                        return context.sync()
                        .then(function () {
                            console.log(body.font.name + " " + body.font.color);  // at this point all the font properties are available, bold, italic color.. etc...
    
                        })
                          .catch(function (e) {
    
                             console.log(e.message);
                          });
                       
                    }
                })
                .catch(function (e) {
    
                   console.log(e.message);
                });
    
            });
        }

    【讨论】:

    • 感谢您的回复@Juan 我正在浏览新的 API 文档,我发现它非常有用。 office.js 中的 Excel api 是否也有任何更新?
    • 嗨@Juan 这段代码在 Word 2013 上不起作用,当我在 Office 2016 上尝试上述代码时,它的代码也不能正常工作。我会给你发代码。
    • 抱歉回复晚了。是的,Word 的新 API 模型是 2016+(最新 API 的最新更新),目前没有计划将它们移植到 2013。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多