【问题标题】:How to get/change the first paragraph in a selection via word-js?如何通过 word-js 获取/更改选择中的第一段?
【发布时间】:2017-07-12 20:57:04
【问题描述】:

我正在使用 javascript api 开发 Word 插件。我正在尝试在选择中显示第一段的文本。稍后我想更改这一段。

但是,如果段落包含在表格中,则已经显示文本会失败。如果选择了表格单元格,则单元格中包含的任何段落似乎都不包含在 context.document.getSelection().paragraphs 集合中。

为什么会这样?如果所选内容包括大表格中的表格单元格,我应该如何导航到所选内容的第一段?

这是我用来调试问题的代码:

(function () {
    "use strict";

    // The initialize function is run each time the page is loaded.
    Office.initialize = function (reason) {
        $(document).ready(function () {

            // Use this to check whether the API is supported in the Word client.
            if (Office.context.requirements.isSetSupported('WordApi', 1.1)) {
                // Do something that is only available via the new APIs
                $('#button').click(doSomething);
                $('#status').html('Everything is fine.');
            }
            else {
                // Just letting you know that this code will not work with your version of Word.
                $('#supportedVersion').html('This code requires WordApi 1.1 or greater.');
            }
        });
    };

    function doSomething() {
        Word.run(function (context) {
            // get selection
            var selection = context.document.getSelection();
            // load the text of all paragraphs in the selection
            var paragraphs = selection.paragraphs.load('text');

            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function() {
                $('#status').html(JSON.stringify(paragraphs.items));
            });
        })
        .catch(function (error) {
            $('#status').html('Error: ' + JSON.stringify(error));
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });
    }
})();

第一张图片中的选择返回[{"text":"Content21"}],正如预期的那样。

第二张图片中的选择根本不返回任何文本:[]。 paragraps-collection 似乎是空的。这对我来说似乎没有任何意义。选择中有段落。

我做错了什么?

【问题讨论】:

    标签: javascript ms-word ms-office office-js office-addins


    【解决方案1】:

    我不熟悉 Word API,但according the documentation 选择包含一个范围,并且该范围包含表格行选择案例中的段落列表。

    所以你可以试试这个:

    var range = context.document.getSelection();
    var paragraphs = range.paragraphs;
    var textFirstParagraph = paragraphs.items[0].getRange().load('text');
    

    进一步研究的文档链接:

    【讨论】:

    • 你的代码应该和我的一样。您只是取消了变量名“range”而不是“selection”和 context.load() 函数,它 100% 等效于 object.load() 函数:buildingofficeaddins.com/loading-properties
    • Michael Palm 你对load 函数的看法是正确的。无论如何,您是否尝试过访问段落集合 items 字段? dev.office.com/reference/add-ins/word/paragraphcollection
    • 是的,正如您在我的代码中看到的那样,我正在打印 items 数组:$('#status').html(JSON.stringify(paragraphs.items));。它适用于第一张图片中的选择。
    • 就我在 VBA 中看到的而言,它反映了 Word API。在 VBA 中,要访问单元格段落项目的文本,您需要遍历这些项目并访问它们的 Range 对象和此范围对象的 text 属性。我看到 API 这个词提供了一个可以使用的getRange 方法。
    • word-js 中的范围与 VBA 不同。一个 Range 可以包含多个段落。根据文档,getRange() 返回的 Range 也是如此。
    【解决方案2】:

    我注意到,word-js 中所有与表格相关的对象仅适用于需求集 1.3 而不适用于 1.1。此外,它们未列在文档左侧的菜单中

    https://dev.office.com/reference/add-ins/word/paragraph

    尽管如此,也可以通过单击来浏览它们,例如关于段落对象的“parentTable”属性的类型。

    由于我在我的 word-js 版本 (https://github.com/OfficeDev/office-js-docs/issues/961) 的另一个关键功能中遇到了一个明显的错误,而且 Requirement Set 1.1 似乎不适合与表格交互,我认为这是一个错误。我在这里举报了:https://github.com/OfficeDev/office-js-docs/issues/972

    顺便说一句:为了增加混淆,共享 api (https://dev.office.com/reference/add-ins/shared/tabledata) 中还有一个“TableData”对象,它似乎被 Requirement Set 1.3 的表格内容所取代。我不知道这是在做什么以及是否应该使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 2010-09-20
      相关资源
      最近更新 更多