Reputation: 131
Basically what I want is that, I have start and end index now I want to get the Range object from this start and end index.
OR how do I get the start and end index from the existing range object.
Word.run(function (context) {
var range = context.document.getSelection();
range.select();
return context.sync().then(function () {
console.log('Selected the range.');
});
})
.catch(function (error) {
});
Please help me how I can solve this.
Thanks in advance.
Upvotes: 2
Views: 3313
Reputation: 5046
To augment Ricky's answer. Yes we don't support specific range indexes/coordinates as this is super error prone. You can however get the start, end or the entire range of any object. For example you can do something like document.getSelection("start") that will give you a zero-length range of the beginning of the selection (you can also do "end" and if you leave it empty it gets you the whole selection range).
Basically all objects have that capability. You can then use other range operations like expand. Check out this example who gets the sentences from the current insertion point to the end of the paragraph, just to give you an idea on what you can accomplish.
hope this helps. thanks.
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);
}
Upvotes: 2
Reputation: 9784
The office.js Word.Range
object doesn't have numeric start and end points like the VSTO Word.Range object. In office.js, to get the range of a particular word in text, you probably need to do one of the following, depending on exactly what your scenario is.
Range.getRange(rangeLocation)
method.Range.getTextRanges(...)
method to get all the word ranges in the paragraph and then pick out the range you need from the collection that is returned.Range.search
or Paragraph.search
to search for the word.See the reference help for Word.Range.
Upvotes: 1