Office 365 Advent Calendar
2017年12月、今年もAdvent Calendarの季節がやってきました。
昨年は「Office 365 Advent Calendar 2016」に参加させていただきましたが(下記記事参照)、今年も同じく「Office 365 Advent Calendar 2017」に参加させていただきます。
Excel Custom functions
今回紹介するのはExcelの新機能、Custom functionsです。
Dev Preview – Custom Functions in Excel. Get all the details and try custom functions for yourself at https://t.co/7cl81IA8jc #Office365 pic.twitter.com/jMO5GO9Ig7
— Office Developer (@OfficeDev) 2017年11月6日
上記ツイートの動画を見ると何となく何をやっているかが分かると思いますが、Excel Custom functionsは、早い話が“ユーザー定義関数のようにワークシート上から使える独自関数を、JavaScriptで定義できる機能”です。
(「Office 365 Advent CalendarなのにOffice 365関係ないじゃん!?」と思われる方もいらっしゃるかもしれませんが、動作検証に使っているのはOffice 365 Enterprise E3のInsider版Excelを使っているので、一応関わりがあるということで・・・。)
Office 2016 Insiderビルドのインストール
現時点(2017年12月)では、Custom functions機能はExcel 2016 バージョン 16.8711 以降でないと使えないため、InsiderビルドのOfficeをインストールする必要があります。
インストール方法は「Office 365 の商用ユーザーが Office 2016 の新機能にいち早くアクセスする方法」に書いてある通りで、「Office Deployment Tool」を使って、設定ファイル指定でOfficeをインストールします。
設定ファイル例(configuration.xml)
1 2 3 4 5 6 7 | < Configuration > < Add OfficeClientEdition = "32" Channel = "InsiderFast" > < Product ID = "O365ProPlusRetail" > < Language ID = "ja-jp" /> </ Product > </ Add > </ Configuration > |
Custom functions用Office アドインの作成
Custom functionsの開発方法はOffice アドインと全く同じで、マニフェストファイル、アドイン本体となるWebページ、JavaScriptファイル、この3つを用意する必要があります。
Office アドインの開発方法についてはここでは深く解説しませんので、下記記事等をご参照ください。
マニフェストファイル(manifest.xml)
SourceLocation要素やbt:Url要素で、アドイン本体となるWebページとJavaScriptファイルを指定しています。
また、Id要素の値は、Generate GUIDs online等で作成した、新しいGUIDに置き換える必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <? xml version = "1.0" encoding = "utf-8" ?> < OfficeApp xmlns = "http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:bt = "http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:ov = "http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type = "TaskPaneApp" > < Id >ed75112a-f94f-43d9-bb73-0889225ae851</ Id > < Version >1.0.0.0</ Version > < ProviderName >kinuasa</ ProviderName > < DefaultLocale >ja-JP</ DefaultLocale > < DisplayName DefaultValue = "Custom functions sample" /> < Description DefaultValue = "Custom functionsのサンプルアドインです。" /> < Hosts > < Host Name = "Workbook" /> </ Hosts > < DefaultSettings > </ DefaultSettings > < Permissions >ReadWriteDocument</ Permissions > < VersionOverrides xmlns = "http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type = "VersionOverridesV1_0" > < Hosts > < Host xsi:type = "Workbook" > < AllFormFactors > < ExtensionPoint xsi:type = "CustomFunctions" > < Script > < SourceLocation resid = "functionsjs" /> </ Script > < Page > < SourceLocation resid = "functionshtml" /> </ Page > </ ExtensionPoint > </ AllFormFactors > </ Host > </ Hosts > < Resources > < bt:Urls > </ bt:Urls > </ Resources > </ VersionOverrides > </ OfficeApp > |
アドイン本体(customfunctions.html)
Webページの内容は非常にシンプルで、単にJavaScriptを読み込んでいるだけです。
(Custom functionsがまだPreview段階であるため、使用するライブラリもbeta版のものを読み込んでいます。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <! DOCTYPE html> < html > < head > < meta charset = "UTF-8" /> < meta http-equiv = "X-UA-Compatible" content = "IE=Edge" /> < meta http-equiv = "Expires" content = "0" /> < title >Custom functions sample</ title > <!-- <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> --> <!-- <script src="https://appsforoffice.microsoft.com/lib/beta/hosted/office.js"></script> --> < script src = "customfunctions.js" ></ script > </ head > < body ></ body > </ html > |
JavaScriptファイル
関数本体となるJavaScriptファイルで、ここで独自関数を定義します。
下記コードでは、2つの数値に100を足した値を返す独自関数「ADD100」を定義しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Office.initialize = function (reason){ Excel.Script.CustomFunctions = {}; Excel.Script.CustomFunctions[ "CONTOSO" ] = {}; function add100 (a, b) { return a + b + 100; } Excel.Script.CustomFunctions[ "CONTOSO" ][ "ADD100" ] = { call: add100, description: "2つの数値に100を足した値を返します。" , result: { resultType: Excel.CustomFunctionValueType.number, resultDimensionality: Excel.CustomFunctionDimensionality.scalar, }, parameters: [ { name: "num 1" , description: "最初の数値" , valueType: Excel.CustomFunctionValueType.number, valueDimensionality: Excel.CustomFunctionDimensionality.scalar, }, { name: "num 2" , description: "2番目の数値" , valueType: Excel.CustomFunctionValueType.number, valueDimensionality: Excel.CustomFunctionDimensionality.scalar, } ], options:{ batch: false , stream: false } }; Excel.run( function (context) { context.workbook.customFunctions.addAll(); return context.sync().then( function (){}); }). catch ( function (error){}); }; |
Script.CustomFunctionオブジェクト(独自関数)を定義して、CustomFunctionCollectionオブジェクトのaddAllメソッドによって登録をおこなっているわけですが、詳細な説明はそれぞれのリファレンスをご参照ください。
なお、登録した独自関数は、同じアドインからCustomFunctionCollectionオブジェクトのdeleteAllメソッドを実行することによって登録を解除できますが、個人的には、下記フォルダにあるファイルを削除してしまった方が早いと思います。
1 | %LOCALAPPDATA%\Microsoft\Office\16.0\Wef\CustomFunctions |
作成したOffice アドインの読み込み
作成したOffice アドインは共有フォルダ方式で読み込みます(サイドローディング)。
やり方の詳しい説明は下記記事をご参照ください。
挿入タブの「個人用アドイン」から、上記アドインを追加すると、問題が無ければ下図のようにワークシート上から定義した関数を呼び出せるようになります。
上手くいかない場合は、マニフェストファイルで指定したWebページをInternet Explorerで開いてみて、証明書エラーが表示されないかどうか、まずは確認してください。
ブラウザーの警告が表示される場合は、「自己証明書のhttpsサイトで証明書エラーを非表示にする手順」にあるような方法で、エラーを非表示にする必要があります。
おわりに
「JavaScriptでExcelの関数を定義できるようになった」、そう聞いてもピンと来ない方も多いと思いますが、これは画期的な機能追加です。
Office アドイン(Webページ)を介してCognitive Servicesなどの強力なAPIを利用でき、ユーザー側ではSUM関数やAVERAGE関数等の標準関数と同様の操作性で簡単に扱うことができます。
しかも、Office ストア(Microsoft AppSource)を通して世界中にアドインを提供することができるのです(その予定)。
夢が広がるExcel Custom functions、今後の動向に要注目です。
2017/12/12 追記:
Custom functionsのデバッグ方法について記事を書きました。
参考Webページ
- Create custom functions in Excel (Preview)
- https://docs.microsoft.com/ja-jp/office/dev/add-ins/excel/custom-functions-overview
- Custom functions in Excel (Preview)
- https://github.com/OfficeDev/Excel-Custom-Functions
- Deliver Your Own Excel Custom Functions (JavaScript Add-in) for Users
- https://tsmatz.wordpress.com/2017/12/01/excel-addin-custom-function/
この記事へのコメントはありません。