如何使用 ONLYOFFICE 宏为特定单词添加注释

2024年01月05日作者: Mona

在处理文档时,有时需要对某些反复出现的单词添加注释。在这篇文章中,我们将向您展示如何用宏来执行这个操作,从而简化编辑过程并让文档更清晰易懂。

如何使用ONLYOFFICE宏为单词的每个实例添加注释

构建宏

function addCommentToWord() {

addCommentToWord() 函数可以为文档中出现的指定单词添加注释。

以下是其关键组成的拆分:

   const oDocument = Api.GetDocument();
   const numberOfElements = oDocument.GetElementsCount();

我们在 oDocument 变量中获取了当前文档。下面我们使用 GetElementsCount()方法获取文档中的所有元素

    for (
        let elementIndex = 0;
        elementIndex < numberOfElements;
        elementIndex++
      ) 

此处使用 for 循环来迭代所有元素。这样会为文档的每个元素(段落)中的所需单词添加注释。

{
   const oElement = oDocument.GetElement(elementIndex);
   const rawText = oElement.GetText();

在 for 循环中,我们通过使用 GetElement 方法并传入 elementIndex 来获取文档的每个元素。然后,我们在 rawText 变量中提取纯文本。

    const targetWord = "sample word";
    let count = 0;
    for (let i = 0; i < targetWord.length; i++) {
      count++;
    }
    const wordLength = count - 1;

将单词设置在我们希望评论出现的位置。接下来,使用 for 循环计算单词的长度,并将其存储在 wordLength 中。

// Find all occurrences of the target word in the document
    const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");

然后,使用目标单词创建正则表达式 (wordRegex) 以查找文档中的所有匹配项。

let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index; //returns the index where the word starts.
      let nextMatchPosition = matchPosition + wordLength;
      // Add a comment at the position of the match
      const comment = "YOUR COMMENT GOES HERE";
      const oParagraph = oDocument.GetElement(0);
      const range = oParagraph.GetRange(matchPosition, nextMatchPosition);
      range.AddComment(comment, "Comment's Author Name");
    } 
  }

在 while 循环中,我们在 wordRegex 中找到并存储的所有实例中迭代。

对于每个匹配项,将计算单词 (matchPosition) 的起始位置和单词 (nextMatchPosition) 之后下一个字符的位置。

comment 变量保存要插入的注释。AddComment 方法具有一个可选参数,可用于更改作者的姓名。如果不想使用它,可以排除可选参数,只需传入 comment 变量即可。

addCommentToWord();

最后,调用 addCommentToWord 函数。

完整的宏代码

这是宏的完整代码。

(function () {
  function addCommentToWord() {
    const oDocument = Api.GetDocument();
    const numberOfElements = oDocument.GetElementsCount();

    //looping through all the elements in the document.
    for (
        let elementIndex = 0;
        elementIndex < numberOfElements;
        elementIndex++
      ) {
        const oElement = oDocument.GetElement(elementIndex);
        const rawText = oElement.GetText();
  
      // Specify the target word and the length of the word
      const targetWord = "sample word";
      let count = 0;
      for (let i = 0; i < targetWord.length; i++) {
        count++;
      }
      const wordLength = count - 1;

      // Find all occurrences of the target word in the document
      const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
      let match;
      while ((match = wordRegex.exec(rawText)) !== null) {
        
        let matchPosition = match.index; // returns the index where the word starts.
        let nextMatchPosition = matchPosition + wordLength; //the index where the word ends.

        // Add a comment at the position of the match
        const comment = "YOUR COMMENT GOES HERE";
        const range = oElement.GetRange(matchPosition, nextMatchPosition);
        range.AddComment(comment, "Comment's Author Name");
      }
    }
  }

  addCommentToWord();
})();

现在,让我们运行宏看看效果。

这个宏可以使处理重复的单词注释变得轻而易举,为在 ONLYOFFICE 中处理文档编辑器的用户提供方便。

不要错过利用 ONLYOFFICE API 强大功能的机会。我们广泛的 API 方法库是您将想法变为现实的关键。如果您有任何问题或创新概念,欢迎与我们分享。也很高兴能够与您合作。祝探索之路好运!