Excelのカスタム関数を作成する – Officeアドイン

カスタム関数は、開発者がJavaScriptを用いてExcelのアドインとして新しい関数を追加する手段です。Excelユーザーは、SUM()などの標準関数のようにカスタム関数にアクセスできます。Excelのカスタム関数は、Office 365、Windows、Macで利用可能ですが、iPadや古いバージョンのOfficeではサポートされていません。カスタム関数の定義には、JavaScriptまたはTypeScriptでのコード記述が必要で、マニフェストファイルが関数の識別情報を定義します。

Excelのカスタム関数: 開発者が知っておくべきこと

カスタム関数は、開発者がExcelに新しい関数を追加するための強力な手段です。JavaScriptを使用して追加機能を定義することで、ユーザーはExcel内部でネイティブ関数(例えば、SUM())と同様にカスタム関数にアクセスできるようになります。本記事では、カスタム関数の定義方法と開発過程について詳しく説明します。

カスタム関数とは?

カスタム関数とは、ユーザーが自分で定義できる関数のことで、一般的には「ユーザー定義関数」という用語と同義です。VBA、COM、Office.jsアドインにも適用可能です。Microsoftの公式ドキュメントでは、Office JavaScript APIを使用したカスタム関数に関して「カスタム関数」という用語を使用しています。

対応プラットフォーム

Excelのカスタム関数は以下のプラットフォームで利用可能です:

  • Office on the web
  • Windows版Office
    • Microsoft 365のサブスクリプション
    • Office 2016以降の永久ライセンス版
    • Office 2021以降のボリュームライセンス版
  • Mac版Office

現在、以下のプラットフォームではカスタム関数はサポートされていません:

  • iPad版Office
  • WindowsでのOffice 2019以前のボリュームライセンス版

カスタム関数の定義方法

コード内でのカスタム関数の定義

Excelカスタム関数アドインプロジェクトを作成するために、Yeomanジェネレーターを使用すると、関数やタスクペインを制御するファイルが生成されます。重要なファイルは以下の通りです:

ファイル ファイル形式 説明
./src/functions/functions.jsまたはfunctions.ts JavaScript/TypeScript カスタム関数を定義するコードが含まれる
./src/functions/functions.html HTML カスタム関数を定義するJavaScriptファイルへの参照
./manifest.xml XML カスタム関数で使用するファイルの場所を指定する

スクリプトファイル

スクリプトファイルにはカスタム関数を定義するコードと、その関数の説明コメントが含まれています。以下は、球の体積を計算するカスタム関数を定義する例です:

/**
 * 球の体積を返します。
 * @customfunction
 * @param {number} radius
 */
function sphereVolume(radius) {
  return Math.pow(radius, 3) * 4 * Math.PI / 3;
}

マニフェストファイル

カスタム関数を定義するアドインのマニフェストファイル(./manifest.xml)は以下の要素を含みます:

  • カスタム関数の名前空間を定義
  • JavaScript、JSON、HTMLファイルの場所を指定
  • カスタム関数のランタイムを指定

共同編集機能

Microsoft 365サブスクリプションに接続されたExcel on the webおよびWindowsでは、ユーザーが共同編集することができます。カスタム関数を使用しているワークブックでは、共同編集を行う同僚が必要なカスタム関数アドインを読み込むよう求められます。

次のステップ

カスタム関数をぜひ試してみてください。シンプルなカスタム関数のクイックスタートや、より詳細なカスタム関数のチュートリアルを参照してください。

また、カスタム関数を試すための便利な方法として、Excel内でカスタム関数を実験できるScript Labを使用することもおすすめします。これにより、自分だけのカスタム関数を作成したり、提供されているサンプルを試したりすることができます。

Excelのカスタム関数を活用して、より豊かなデータ分析を実現しましょう。

————-

Create custom functions in Excel – Office Add-ins

Source link

The article discusses the functionality of custom functions in Excel, which allow developers to create new functions using JavaScript as part of an add-in. These custom functions can be used by Excel users just like built-in functions such as SUM(). The term "custom function" is synonymous with "user-defined function" and applies to various add-in technologies, including VBA, COM, and Office.js.

Custom functions are supported on several platforms, including:

  • Office on the web
  • Office on Windows (Microsoft 365 subscription, retail perpetual Office 2016 and later, volume-licensed Office 2021 and later)
  • Office on Mac

However, they are not available on Office for iPad and older versions like Office 2019 or earlier.

The article includes an example of a custom function, MYFUNCTION.SPHEREVOLUME, which calculates the volume of a sphere based on the given radius. Key files involved in creating custom functions include the JavaScript or TypeScript file containing the function definitions, an HTML file, and a manifest XML file that enables Excel to recognize and use the custom functions.

The article emphasizes the importance of proper comments in the code to generate metadata for Excel and recommends using the Yeoman generator for Office Add-ins to streamline project creation. It also provides guidance for testing custom functions across multiple environments by maintaining different manifest files for each.

Coauthoring is supported on Microsoft 365, allowing users to work collaboratively in Excel, and if a workbook includes a custom function, collaborators are prompted to load the associated add-in.

For those interested in experimenting with custom functions, the article suggests starting with quick start resources or using the Script Lab add-in for hands-on practice.

関連記事

コメント

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