1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スプレッドシートにオリジナルの関数を追加する

Posted at

はじめに

Googleスプレッドシートには SUM関数 や AVERAGE関数 などの組み込み関数以外にも Google Apps Script (GAS) を使って独自に関数を追加することができます。

カスタム関数の追加方法

GASを使ってユーザ定義のオリジナル関数を追加する方法は以下の通りです。

  1. スプレッドシートを開く
    新規または既存のGoogleスプレッドシートを開いてください。

  2. GASのエディタを開く
    メニュー欄から「拡張機能」→「Apps Script」と選択してGASの開発環境(Apps Script Editor)を開きます。
    image.png

  3. カスタム関数を作成する
    エディタで次のような関数を定義して「Ctrl + S」で保存します。

    /**
     * 引数inputで与えられた数値を2倍にして返す関数。
     * @param {number} input 数値
     * @return 引数inputの2倍の数値
     * @customfunction
    */
    function DOUBLE(input) {
      return input * 2;
    }
    

    image.png

  4. カスタム関数を使う
    Apps Script Editor上で定義・保存したカスタム関数はスプレッドシートですぐに利用可能になります。

    スプレッドシートのタブに戻って「=DOUBLE(」と入力するとユーザ定義の関数が使い方のヒントも含めて表示されるはずです。

    image.png

    カスタム関数の命名規則

    ユーザ定義のカスタム関数を作成する際、関数はJavaScriptの標準的な命名規則に加えて以下のルールを守るようにしてください。
    • 既に存在する SUM() などの組み込み関数とは異なる名前にする
    • Apps Scriptのプライベート関数を意味するアンダースコア(_)で終わらせない
    • let func = () => {...}等ではなくfunction func() {...}の形式にする
    • 必須ではないが、伝統的に大文字で命名する
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?