Office アドイン

Office用アプリではalertやconfirmが使えない?

JavaScriptでメッセージや確認ダイアログを表示する際には「alert」や「confirm」、「prompt」がよく使われますが、これらはOffice用アプリで使用できるのでしょうか?
簡単なサンプルコードでテストしてみます。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="https://az88874.vo.msecnd.net/api/1.0/office.js"></script>
        <script>
            Office.initialize = function(reason){
                $(document).ready(function(){
                    Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, document_SelectionChanged);
                });
            }
            function document_SelectionChanged(eventArgs) {
                $("#result").val(eventArgs.type);
                window.alert("aaa");
                window.confirm("bbb");
                window.prompt("ccc","ddd");
            }
        </script>
    </head>
    <body>
        <textarea id="result" rows="4" cols="30"></textarea>
    </body>
</html>

上記コードはSelectionChangedイベントを利用して選択が変更されたときに処理を行うものですが、動作を確認してみるとtextareaへの書き込みは行われるものの、それ以下のコードは実行されないことが分かります。
どうやらOffice用アプリではalertやconfirm、promptは使用できないようです。

といっても、下記コードのようにVBScriptを通してしまえばMsgBoxやInputBoxを呼び出すことができるので、ダイアログで簡単に動作を確認したい場合には下記のような方法がお薦めです。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="https://az88874.vo.msecnd.net/api/1.0/office.js"></script>
        <script>
            Office.initialize = function(reason){
                $(document).ready(function(){
                    Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, document_SelectionChanged);
                });
            }
            function document_SelectionChanged(eventArgs) {
                $("#result").val(eventArgs.type);
                msgBoxVbs("eee", 3 + 64, "fff");
                inputBoxVbs("ggg", "hhh", "iii");
            }
        </script>
        <script language="vbscript">
            Function msgBoxVbs(prompt, buttons, title)
                msgBoxVbs = MsgBox(prompt, buttons, title)
            End Function
            
            Function inputBoxVbs(prompt, title, default)
                inputBoxVbs = InputBox(prompt, title, default)
            End Function
        </script>
    </head>
    <body>
        <textarea id="result" rows="4" cols="30"></textarea>
    </body>
</html>

[Office用アプリ]Microsoft Translator APIを利用して選択文字列を翻訳する(クロスドメイン対応)。前のページ

PowerPoint用のOffice用アプリ次のページ

関連記事

  1. アイコン一覧

    Office 365アイコン(imageMso)一覧(P)

    Office 365のデスクトップ版Officeアプリケーション(Wo…

  2. Office関連

    [PowerPoint]図やスライドをSVGとして保存する機能が追加されました。

    ※ 下記情報はInsider版のOfficeを元にしています。バージョ…

  3. Office アドイン

    [Officeアドイン]シート見出しの色を変更する方法

    前回の記事で紹介したExcel JavaScript API バージョ…

  4. Office アドイン

    Office アドインの概要と開発方法を学ぶための自習書

    2018年10月27日(土)、品川の日本マイクロソフト本社で「2018…

  5. Office関連

    フッターにページ番号と総ページ数を挿入するWordマクロ

    以前書いた、フッターに「ページ番号 / 総ページ数」を挿入するWord…

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

Time limit is exhausted. Please reload CAPTCHA.

Translate

最近の記事

アーカイブ

PAGE TOP