0
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?

【GAS】部署ごとの平均総支給額を出す集計システムをつくってみた

Last updated at Posted at 2025-09-18
  • この記事では Google Apps Script (GAS) を使って「部署ごとの平均総支給額」を求める方法をまとめてます。
  • 学習のアウトプットやけど、同じような処理をしたい人にも役立つはずやと思います。

学習内容

コース:業務効率化「Biz HACK」

単元:計算(2)チャレンジ課題

内容:部署ごとの平均を出す仕組みをつくる

学んだポイント

  • getValue()getValues() の違い

  • getValue() → 単一セルだけ取得

  • getValues() → 複数セルまとめて取得(結果は2次元配列になる)

2次元配列と row の扱い

  • 配列は「横(列)」と「縦(行)」を意識して扱う必要あり。行ごとにデータを処理する時に必須の考え方。

  • toLocaleString()
    数字を「,」入りの日本円フォーマットに変換できる。

  • number.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });

  • lastRow - 1
    データの最終行を動的に取得する時に便利。

deptMap の使い方

  • 部署ごとに「合計」と「人数」を保持するオブジェクト(箱みたいなもん)を用意する。

  • 論理OR演算子「||
    値が存在しない場合に初期化するときなどで便利。
    (例:deptMap[dept] = deptMap[dept] || { sum: 0, count: 0 };

実装コード例

// 範囲取得(例: A2~C列まで)
const values = sheet.getRange(2, 1, lastRow - 1, 3).getValues();

const deptMap = {};

values.forEach(row => {
  const dept = row[1];       // 部署名
  const totalComp = row[2];  // 総支給額

  // 部署がまだ登録されてなければ初期化
  if (!deptMap[dept]) {
    deptMap[dept] = { sum: 0, count: 0 };
 }

  deptMap[dept].sum += totalComp; 
  deptMap[dept].count++;
});

// 部署ごとに平均を計算
for (const dept in deptMap) {
  const avg = deptMap[dept].sum / deptMap[dept].count;
  console.log(`${dept} 平均: ${avg.toLocaleString("ja-JP")}円`);
}

まとめ(気づき)

  • deptMap みたいな「箱」を用意して、そこに値をためていくのは考え方としてめっちゃ分かりやすい。

  • 書いて説明すると、「なんとなく理解」から「ちゃんと説明できる」に変わるのを実感した。

  • GASでの集計処理は、Excelのピボット感覚に似てるから業務効率化にも応用できそう。

0
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
0
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?