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>



















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