【问题标题】:How to get and replace the word on the left of the cursor如何获取和替换光标左侧的单词
【发布时间】:2022-10-19 20:19:12
【问题描述】:
对于 javascript 中的单词插件,一个简单的用例是获取光标左侧的单词并将其替换为大写。
例如,如果 | 是光标:
-
Hello world| 将变为 Hello WORLD|
-
Hello| world 将变为 HELLO| world
是否可以使用Word.Range class 执行此示例?例如,要将范围扩大到像这样的虚构代码这样的空格:
Word.run(function (context) {
var selection = context.document.getSelection();
var cursor = selection.getRange('Start');
// Fictive: how to expand the range to the left until a space?
var range = cursor.expandToLeftUntil(' ');
range.load("text");
var html = range.getHtml();
await context.sync();
var textToReplace = html.value.toUpperCase();
// Replace the text
range.insertText(textToReplace, 'Replace');
await context.sync();
});
或者有没有其他解决方案?
【问题讨论】:
标签:
ms-word
office-js
office-addins
word-addins
word-web-addins
【解决方案2】:
我试图做类似的事情。 At least, when a selection is empty get the word nearby the cursor.我希望它们是一些 API 函数,但事实并非如此。
我从 Rick Kirkham 的答案/想法开始(谢谢!)。我无法使用搜索方法来获取单词列表。不过,在空间上使用 split 效果很好。
而不是像我一样选择你可以修改文本。
如果您不想靠近但只有在您应该更改功能以检查'InsideStart' 之后(在这种情况下您想转到前一个单词,所以i-1)。
Word.run(async (context) => {
let cursorOrSelection = context.document.getSelection();
cursorOrSelection.load();
await context.sync();
// if the cursor is empty we make a selection of the Word close-by
// this behaviour is done automatically when you insert a comment in Word
if (cursorOrSelection.isEmpty) {
console.log("Empty selection, cursor.");
// get the paragraph closest to the cursor.
const paragraph = cursorOrSelection.paragraphs.getFirst();
const allWordsInParagraph = paragraph.split([" "], true /* trimDelimiters*/, true /* trimSpaces */);
allWordsInParagraph.load();
await context.sync();
// compare the cursorRange with the ranges of individual words in the paragraph.
let compareRanges = [];
allWordsInParagraph.items.forEach( item => {
compareRanges.push({
compare: cursorOrSelection.compareLocationWith(item),
range: item
});
});
await context.sync();
// walk through all the words and compare the location relation with the cursor
// were the location relation changes, the word is near the cursor.
let previousLocationRelation = null;
let wordClosestToCursorRange = null;
for (let i = 0; i < compareRanges.length; i++) {
const locationRelation = compareRanges[i].compare.value;
console.log(locationRelation);
// if first entry is Before, we are at the beginning
if(i==0 && locationRelation === 'Before') {
wordClosestToCursorRange = compareRanges[i].range;
// jump out
break;
}
else {
if(previousLocationRelation && locationRelation != previousLocationRelation) {
// first "edge" we find.
// console.log('-- edge');
// if first edge we encounter is Before
// we need the previous one (could be after)
if(locationRelation === 'Before') {
wordClosestToCursorRange = compareRanges[i-1].range;
}
else {
// we are inside the word or end of the word
// Inside, InsideStart, InsideEnd
wordClosestToCursorRange = compareRanges[i].range;
}
// jump out we are only interested in the first edge
break;
}
}
previousLocationRelation = locationRelation;
}
wordClosestToCursorRange.select();
}
return context.sync();
})
.catch(function (error) {
console.log(error.message)
})