0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

プレゼンテーションの単語を置き換えるONLYOFFICEマクロ

Posted at

プレゼンテーションの作成中に、特定の単語の出現回数をすべて変更する必要がある場合がいくつかあります。特にスライドがたくさんある場合、手作業での修正は時間がかかります。このプロセスを簡単にするために、プレゼンテーションエディタで特定の単語を置き換えるマクロを作成しましょう。

An ONLYOFFICE Macro that replaces a word in the Presentation Editor

マクロの構築

const oPresentation = Api.GetPresentation(); //get the current presentation
まず、oPresentation変数で現在のプレゼンテーションを取得します。
for (
    var slideIndex = 0;
    slideIndex < oPresentation.GetSlidesCount();
    slideIndex++
  ) {
このマクロはネストされたforループを含みます。この最初のforループでは、プレゼンテーションのすべてのスライドを繰り返し処理します。
var oSlide = oPresentation.GetSlideByIndex(slideIndex);
var aShape = oSlide.GetAllShapes(); //get all the shapes from different slides
これらの行は、特定のスライドに存在する可能性のあるすべての図形を取得し、その詳細を aShape 配列にプッシュします。
for (var shapeIndex = 0; shapeIndex < aShape.length; shapeIndex++) {
      var content = aShape[shapeIndex].GetDocContent();
この2番目のforループでは、特定のスプレッドシート上の各図形の内容を取得します。
if (content) {
        var count = content.GetElementsCount();
        for (var elementIndex = 0; elementIndex < count; elementIndex++) {
          var element = content.GetElement(elementIndex);
content変数が存在することを確認し、contentに存在するすべての要素のカウントを取得します。
if (element) {
            const rawText = element.GetText(); //gets the text from a particular element
            element.Delete(); //delete the content
            const wordToFind = "apple"; // Replace "apple" with the word you want to find
            const replacementWord = "banana"; // Replace "banana" with the word you want to replace it with
            const cleanedText = rawText.replace(
              new RegExp(wordToFind, "g"),
              replacementWord
            );
まず、変数要素が有効かどうかを確認します。要素が存在すれば、元のテキストをコピーし、element.delete()メソッドで内容を削除します。

続いて、置換する単語と置換後の単語をそれぞれの変数に指定し、置換を実行して新しいテキストをcleanedText変数に格納します。

 var oParagraph = Api.CreateParagraph();
 var oRun = Api.CreateRun();
 oRun.AddText(cleanedText);
 oParagraph.AddElement(oRun);
 content.Push(oParagraph);

最後に、新しい段落を作成し、そこにcleanedTextを追加します。段落要素のセットアップが完了したら、上記のコードを使用して、上記で削除した段落要素の代わりにスライドに追加します。

マクロの全コード

マクロのコード全体は下記のようになります:
(function () {
  const oPresentation = Api.GetPresentation(); //get the current presentation
  for (
    var slideIndex = 0;
    slideIndex < oPresentation.GetSlidesCount();
    slideIndex++
  ) {
    var oSlide = oPresentation.GetSlideByIndex(slideIndex);
    var aShape = oSlide.GetAllShapes(); //get all the shapes from different slides
for (var shapeIndex = 0; shapeIndex &lt; aShape.length; shapeIndex++) {
  var content = aShape[shapeIndex].GetDocContent();

  // Check if content exists before proceeding
  if (content) {
    var count = content.GetElementsCount();
    for (var elementIndex = 0; elementIndex &lt; count; elementIndex++) {
      var element = content.GetElement(elementIndex);

      // Check if element is valid before using it
      if (element) {
        const rawText = element.GetText(); //gets the text from a particular element

        element.Delete(); //delete the content

        const wordToFind = "apple"; // Replace "apple" with the word you want to find
        const replacementWord = "banana"; // Replace "banana" with the word you want to replace it with
        const cleanedText = rawText.replace(
          new RegExp(wordToFind, "g"),
          replacementWord
        );

        //creates a new paragragh, and restores the content which was deleted, with the cleaned text.
        var oParagraph = Api.CreateParagraph();
        var oRun = Api.CreateRun();
        oRun.AddText(cleanedText);
        oParagraph.AddElement(oRun);
        content.Push(oParagraph);
      }
    }
  }
}

}
})();

マクロを実行して結果を見ましょう!
このマクロが皆さまのお役に立ち、プレゼンテーションのワークフローを効率化できたなら幸いです。

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?