基本上我想要的是,我有开始和结束索引,现在我想从这个开始和结束索引获取 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) {
});
请帮我解决这个问题。
提前致谢。
最佳答案
补充瑞奇的答案。是的,我们不支持特定范围索引/坐标,因为这非常容易出错。 但是,您可以获得任何对象的开始、结束或整个范围。例如,您可以执行类似 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);
}
关于office-js - Word js : how to get the Range object from start and end index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329258/