ONLYOFFICEマクロを使って、単語のすべてのインスタンスにコメントを追加する方法

2024年01月05日著者:Denis

文書を作成する際、文章中に繰り返し出てくる特定の単語にコメントを付けることが必要になる場合があります。この記事では、特定の単語のすべてのインスタンスに効率的にコメントを追加し、編集プロセスを合理化し、文書をより明確にする方法をご紹介します。

Build an ONLYOFFICE macro that adds comments to every instance of a word in the Text Editor

マクロの構築

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メソッドには、作者名を変更するためのオプションのパラメータがあります。それを使いたくない場合は、オプションのパラメータを除外して、コメント変数だけを渡すことができます。

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メソッドの豊富なライブラリは、あなたのアイデアを現実に変える鍵です。ご質問や革新的なコンセプトがありましたら、ぜひ私たちと共有してください