【问题标题】:Word js: how to get the Range object from start and end indexWord js:如何从开始和结束索引获取范围对象
【发布时间】:2017-11-03 21:13:44
【问题描述】:

基本上我想要的是,我有开始和结束索引现在我想从这个开始和结束索引中获取 Range 对象。

或者我如何从现有的范围对象中获取开始和结束索引。

Word.run(function (context) {
    var range = context.document.getSelection();
    range.select();

    return context.sync().then(function () {
       console.log('Selected the range.');
    });  
})
.catch(function (error) {

});

请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 请澄清您所说的“开始和结束索引”是什么意思。 Word.Range 对象 (dev.office.com/reference/add-ins/word/range) 似乎没有这些属性。
  • 我有一个段落,我需要从开始和结束索引中选择特定的单词。
  • 对不起。我还是不明白你所说的“开始和结束索引”是什么意思。
  • 假设我需要从下面的段落中选择“欢迎”这个词。我有开始和结束的位置。开始 = 3 和结束 = 10 您好,欢迎使用 office js API。这是创建插件的新方法。祝你好运。 使用 Word 互操作,我们可以这样做。 https://msdn.microsoft.com/en-us/library/2a9dt54a.aspx Word.Range rng = this.Range(ref start, ref end); rng.Select();那么如何使用 Office js API 从头到尾获取 Range 对象,
  • 请查看我的答案以获取更多详细信息和示例。你的第二个问题可以完成。谢谢。

标签: office-js office365api office365-apps


【解决方案1】:

补充瑞奇的答案。是的,我们不支持特定的范围索引/坐标,因为这很容易出错。 但是,您可以获取任何对象的开始、结束或整个范围。例如,您可以执行诸如 document.getSelection("start") 之类的操作,这将为您提供选择开始的零长度范围(您也可以执行“end”,如果您将其留空,它将为您提供整个选择范围)。

基本上所有对象都有这种能力。然后,您可以使用其他范围操作,例如扩展。看看这个例子,谁得到了从当前插入点到段落末尾的句子,只是为了让你知道你可以完成什么。

希望这会有所帮助。谢谢。

function getSentences() {
    Word.run(function (context) {
        // gets the complete sentence  (as range) associated with the insertion point.
        var sentences = context.document
            .getSelection().getTextRanges(["."] /* Using the "." as delimiter */, false /*means without trimming spaces*/);
        context.load(sentences);
        return context.sync()
            .then(function () {
                //  expands the range to the end of the paragraph to get all the complete sentences.
                var sentecesToTheEndOfParagraph = sentences.items[0].getRange()
                    .expandTo(context.document.getSelection().paragraphs
                        .getFirst().getRange("end") /* Expanding the range all the way to the end of the paragraph */).getTextRanges(["."], false);
                context.load(sentecesToTheEndOfParagraph);
                return context.sync()
                    .then(function () {
                        for (var i = 0; i < sentecesToTheEndOfParagraph.items.length; i++) {
                            console.log("Sentence " + (i + 1) + ":"
                                + sentecesToTheEndOfParagraph.items[i].text);
                        }
                    });
            });
    })
        .catch(OfficeHelpers.Utilities.log);
}

【讨论】:

  • 嘿@juan-balmori-感谢示例代码-为我清除了一些东西。不过有一件事-也许您可以将其添加到文档中? expandTo 并没有真正解释,像这样的例子真的可以帮助用户概念化 Ranges 和 expandTo。
  • 这是一个很好的建议,我们很快就会添加它。
【解决方案2】:

office.js Word.Range 对象不像 VSTO Word.Range 对象那样具有数字起点和终点。在 office.js 中,要获取文本中特定单词的范围,您可能需要执行以下操作之一,具体取决于您的场景。

  • 使用Range.getRange(rangeLocation) 方法。
  • 获取父段落的范围。然后使用Range.getTextRanges(...)方法获取段落中的所有单词范围,然后从返回的集合中挑选出你需要的范围。
  • 使用Range.search 或Paragraph.search 搜索字词。

请参阅Word.Range 的参考帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多