使用脚本实验室结合两个段落
我是脚本实验室的新手。我昨天刚刚发现了这个工具,试图找到并删除段落的新线路或段落的结尾,但到目前为止没有运气。在正则上下文中,我试图查找\ w \ s \ n
的意思是 - 一个小写字母,然后是一个空间,然后是新行。 我尝试运行此代码
async function run() {
await Word.run(async (context) => {
const results = context.document.body.search("\w\s\n", { matchWildcards: true });
results.load("length");
await context.sync();
results.items.forEach((word) => {
// ideally i would end up something like this 'n[space][new line]' and then I want to remove the new line and end up only with 'n[space]'. How can I make this?
});
await context.sync();
});
}
如果有人可以向我展示查找和替换的工作演示, 也将帮助我理解该框架的工作原理。谢谢。
I am new to Script Lab. I just discovered this tool yesterday and was trying to find and remove a new line or the end of a paragraph, but with no luck so far. In a RegEx context, I am trying to find \w\s\n
which mean - one lowercase letter, then a space and then a new line. I tried to run this code
async function run() {
await Word.run(async (context) => {
const results = context.document.body.search("\w\s\n", { matchWildcards: true });
results.load("length");
await context.sync();
results.items.forEach((word) => {
// ideally i would end up something like this 'n[space][new line]' and then I want to remove the new line and end up only with 'n[space]'. How can I make this?
});
await context.sync();
});
}
If someone can show me a working demo of find and replace this will also help me understood how this framework is working. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想在单词中找到所有具有此正则表达式
\ w \ s \ n
的段落,但是正常的言论在单词API中的工作方式与正常的正常言论中的工作方式不如@Rick提及的,所以我最终使用了捕获我需要的段落的其他方法。我看到每个段落都有-18的第一线。因此,我做了一个,如果修剪段trimmed = CurrentPara.text.trim();
这样,我将摆脱后面检查的空格,如果最后一个char不是。或
!
或?
,然后如果所有这些都是正确的,则将下一个段落添加到当前的段落中,例如carde> carde> carde> code> code> end”);
然后简单地删除下一个段落,因为我不再需要它 -nextpara.delete();
,这是我的工作解决方案
希望对他人有帮助。从使用Word API的两天开始,我可以说有一些局限性,但是有了逻辑,您就可以实现自己的目标。
I wanted to find all paragraphs in Word which have this regex
\w\s\n
, but regex does not work the same way in Word API as normal regex as mention by @Rick so I end up using other method to catch the paragraphs I needed. I saw every paragraph has a firstLineIndent of -18. So I made one if, trim the paragraphtrimmed = currentPara.text.trim();
so I will get rid of the spaces at the back check if the last char is not.
or!
or?
and then if all of this is true add the next paragraph to the current one like thiscurrentPara.insertText(nextPara.text, "End");
then simply delete the next paragraph as I do not need it anymore -nextPara.delete();
And this is my working solution
Hope that helps someone else. From the 2 days working with Word API I can say there are some limitations, but with logic you can get to your goal.