13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Spread Sheet で独自関数を定義して使う

Posted at

Spread Sheet で以下のようなレイアウトの出納帳を作った.

2014-11-29_14_07_05.png

その日の支出は in 小計out 小計 にまとめられ, 財布 は前日の繰越から収支と銀行残高を引いたものとなっている.
任意の日の財布状況を予測できるので便利.

収支の記述ルールは以下のようになっている.

  • 収入の場合は正の数で表す
  • 支出の場合は負の数で表す
  • 銀行への入金/出金は頭に B と付ける
  • どんなものを買ったかなどはメモでセルに付記する

ここで, その日ごとの in 小計, out 小計 を出力するためには,

  • in 小計: 支出記入欄の正の数のみ合計する. 銀行への入出金は除く
  • out 小計: 支出記入欄の負の数のみ合計する. 銀行への入出金は除く

という操作が必要であり, Spread Sheet で提供されている関数を使うとだるい気がした (範囲すべてのセルに対して IF で調べたり).
そのため, 独自関数 (Custom Function) を定義して, 使うこととした.

まずツール > スクリプト エディタ... からスクリプトエディタを立ち上げる.

2014-11-29_14_16_35.png

以下の関数を追加して保存.

function 入金(target_list){
  sum = 0;
  for (var i = 0; i < target_list[0].length; i++) {
    target_cell = target_list[0][i];
    if (target_cell > 0) sum += target_cell;
  }
  return sum;
}

function 出金(target_list){
  sum = 0
  for (var i = 0; i < target_list[0].length; i++) {
    target_cell = target_list[0][i];
    if (target_cell < 0) sum += target_cell;
  }
  return sum;
}

範囲の場合, 1 行でも 2 次元配列で渡されるので注意.

ここで定義した関数はそのまま Spread Sheet から呼び出せる.

 2014-11-29 14.20.22.png

異様に簡単にできて便利.

References

13
14
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
13
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?