DocumentオブジェクトのsetSelectedDataAsyncメソッドを使えばOfficeドキュメントに文字列を書き込むことができる、というのはこれまで書いた記事「[JavaScript API for Office]「あいさつ文の挿入」を作業ウィンドウアプリに移植してみる。」や「新しいOffice.jsでPowerPointでもOffice用アプリが使えるようになりました。」等でも紹介している通りですが、実はsetSelectedDataAsyncメソッドのオプションパラメーターでcoercionTypeを指定すれば、Officeドキュメントに画像を挿入することもできます。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.2.min.js"></script>
<script src="https://appsforoffice.microsoft.com/lib/1.0/hosted/office.js"></script>
<script>
Office.initialize = function(reason){}
$(function(){
$("#btnOK").click(function(){
Office.context.document.setSelectedDataAsync(
"<img src='http://localhost/apps/img.jpg' alt='cherry blossom' img/>",
{coercionType:"html"}
);
});
});
</script>
</head>
<body>
<input id="btnOK" type="button" value="画像を挿入">
</body>
</html>
ただし、CoercionType 列挙型 (Office 用アプリ)にもあるように、coercionTypeに”html“を指定できるのはWordのみになります。
ExcelやPowerPointで同じ方法が使えないのは残念ですね。

















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