使用脚本实验室结合两个段落

发布于 2025-02-10 03:02:25 字数 683 浏览 3 评论 0原文

我是脚本实验室的新手。我昨天刚刚发现了这个工具,试图找到并删除段落的新线路或段落的结尾,但到目前为止没有运气。在正则上下文中,我试图查找\ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2025-02-17 03:02:25

我想在单词中找到所有具有此正则表达式\ w \ s \ n的段落,但是正常的言论在单词API中的工作方式与正常的正常言论中的工作方式不如@Rick提及的,所以我最终使用了捕获我需要的段落的其他方法。我看到每个段落都有-18的第一线。因此,我做了一个,如果修剪段trimmed = CurrentPara.text.trim();这样,我将摆脱后面检查的空格,如果最后一个char不是。或,然后如果所有这些都是正确的,则将下一个段落添加到当前的段落中,例如carde> carde> carde> code> code> end”);然后简单地删除下一个段落,因为我不再需要它 - nextpara.delete();

,这是我的工作解决方案

async function run() {
  await Word.run(async (context) => {
    const paragraphs = context.document.body.paragraphs;
    paragraphs.load("items");
    await context.sync();

    let currentPara;
    let nextPara;
    let trimmed;
    let lastChar;
    for (let i = 0; i < paragraphs.items.length; i++) {
      currentPara = paragraphs.items[i];
      nextPara = paragraphs.items[i+1];
      trimmed = currentPara.text.trim();
      lastChar = trimmed[trimmed.length - 1];
      if (currentPara.firstLineIndent == -18 && lastChar != "." && lastChar != "!" && lastChar != "?") {
        currentPara.insertText(nextPara.text, "End");
        nextPara.delete();
        // here I change the current paragraph style
        currentPara.firstLineIndent = 18;
        currentPara.isListItem = false;
        currentPara.leftIndent = 0;
      }
    }
  });
}

希望对他人有帮助。从使用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 paragraph trimmed = 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 this currentPara.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

async function run() {
  await Word.run(async (context) => {
    const paragraphs = context.document.body.paragraphs;
    paragraphs.load("items");
    await context.sync();

    let currentPara;
    let nextPara;
    let trimmed;
    let lastChar;
    for (let i = 0; i < paragraphs.items.length; i++) {
      currentPara = paragraphs.items[i];
      nextPara = paragraphs.items[i+1];
      trimmed = currentPara.text.trim();
      lastChar = trimmed[trimmed.length - 1];
      if (currentPara.firstLineIndent == -18 && lastChar != "." && lastChar != "!" && lastChar != "?") {
        currentPara.insertText(nextPara.text, "End");
        nextPara.delete();
        // here I change the current paragraph style
        currentPara.firstLineIndent = 18;
        currentPara.isListItem = false;
        currentPara.leftIndent = 0;
      }
    }
  });
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文