ONLYOFFICEマクロを使用して、マスターフォームテンプレートにテキストフォームを追加する方法

2024年01月10日著者:Denis

フォームテンプレート文書を扱うとき、ダミーのプレースホルダーを使い、後でフォーム要素に置き換えることがよくあります。このブログ記事では、特定の単語が出現するたびにTextFormを挿入するテキスト文書エディタマクロを使って、このプロセスを自動化します。

Use ONLYOFFICE macros to easily add TextForms to master form templates

マクロの構築

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

ここでは、現在の文書を oDocument 変数に格納しています。次に、GetElementsCount() メソッドを使って文書内の要素の総数を取得します。

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

このセクションでは、各要素を循環させるために for ループを使用しています。こうすることで、すべての要素(段落)に指定された単語の TextForm が挿入されるようにします。

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

forループの内部では、GetElement()メソッドを使って各文書要素をelementIndexで取得します。そして、プレーンテキストを抽出して rawText 変数に格納します。

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

ここでは、コメントの対象となる単語を定義します。続いて、forループを使って単語の長さを決定し、変数wordLengthに格納します。

   const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");

次に、文書中に出現するすべての単語を検索するために、ターゲット単語を使用して正規表現(wordRegex)が作成されます。

    let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index; 
      let nextMatchPosition = matchPosition + wordLength;

ここでは、while ループの中で、wordRegex に格納されているすべてのインスタンスを繰り返し処理します。

各マッチについて、単語の開始位置 (matchPosition) と単語の次の文字の位置 (nextMatchPosition) が計算されます。

const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
insertRange.Select();

次に、操作する範囲を insertRange に格納します。次に、Select() メソッドを使用して範囲を選択します。

oDocument.InsertTextForm({
        key: "Personal information",
        tip: "Enter your first name",
        required: true,
        placeholder: "Name",
        comb: true,
        cellWidth: 3,
        multiLine: false,
        autoFit: false,
        placeholderFromSelection: true,
        keepSelectedTextInForm: false,
      });

最後に、選択した範囲に TextForm を挿入します。InsertTextForm() メソッドにはさまざまなパラメータがあります。必要に応じて変更してください。

マクロ全体のコード

マクロ全体のコードです。

(function () {
    function addTextForm() {
      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";
        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.
          console.log(matchPosition);
          console.log(nextMatchPosition);
          const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
          insertRange.Select();
          //insert a text form at the selected range
          oDocument.InsertTextForm({
            key: "Personal information",
            tip: "Enter your first name",
            required: true,
            placeholder: "Name",
            comb: true,
            cellWidth: 3,
            multiLine: false,
            autoFit: false,
            placeholderFromSelection: true,
            keepSelectedTextInForm: false,
          });
        }
      }
    }
  
    addTextForm();
  })();

では、このマクロがどのように動作するか見てみましょう。

このマクロをあなたのoFormワークフローに統合することで、フォームテンプレートとoFormsの使用経験がより充実したものになると確信しています。

ONLYOFFICE APIのパワーを活用するチャンスをお見逃しなく。私たちのAPIメソッドの豊富なライブラリは、あなたのアイデアを現実に変える鍵です。ご質問や革新的なコンセプトがあれば、ぜひ私たちと共有してください