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:関数(Function)を「日本語アルゴリズム → コード」で身につける

Posted at

GAS(Google Apps Script)を学んでいく中で、
学習者が次につまずくポイントがこのあたりです。

  • 関数って何?
  • 引数(ひきすう)ってどう使うの?
  • return(戻り値)の意味がわからない
  • 関数を分ける理由がよく分からない
  • GAS は全部 function から始まるから余計に混乱する

この記事では、

  • 関数の意味を「日常的な例」で理解
  • 日本語コメントでアルゴリズムを作成
  • 最後に GAS コードに翻訳

という流れで 丁寧に解説します。


## 1. 関数(Function)とは?

🎯 「処理のまとまり」に名前をつけたもの

例えると:

  • 「ご飯を炊く」
  • 「ケーキを焼く」
  • 「メールを送る」

これらの一連の流れに名前をつけて、
必要なときに何度でも呼び出せる のが関数です。


🧠 GASにおける関数の基本形

function greet() {
  Logger.log("こんにちは!");
}

呼び出し:

greet();

## 2. 引数(Arguments)

📮 「関数に渡す材料」

関数を料理に例えると、

  • 引数 = 材料
  • 関数 = レシピ
  • return(戻り値)= 完成した料理

引数つきの関数:

function hello(name) {
  Logger.log("こんにちは " + name + " さん!");
}

hello("太郎");

## 3. 戻り値(return)

🎁 「関数の結果を外に渡す」

function add(a, b) {
  return a + b;
}

const total = add(3, 5);
Logger.log(total); // 8

## 4. コメントだけで書く「日本語アルゴリズム」

ここでは、あえて コードを書かず日本語だけ にします。

学習者がコードを書く練習に使えます。


👋 サンプル①:挨拶メッセージを返す(コメント版)

function makeGreeting(name)

// ① 渡された名前に対して挨拶文を作る
// ② 「こんにちは、○○さん!」の形にする
// ③ 完成した文字列を return で返す

➕ サンプル②:足し算を行う関数(コメント版)

function add(a, b)

// ① a と b を足す
// ② 合計を return で返す

🎬 サンプル③:映画の合計視聴時間(コメント版)

function totalMovieTime(times)

// ① times(配列)に入っている時間を合計する
// ② 合計時間を return で返す

🧾 サンプル④:商品合計金額の計算(コメント版)

function calcItemTotal(price, count)

// ① price(単価) × count(個数)で合計金額を計算
// ② 合計金額を return で返す

🔍 サンプル⑤:年齢判定(コメント版)

function isAdult(age)

// ① 年齢が 20 以上なら「true」
// ② 19 以下なら「false」
// ③ 結果を return で返す

## 5. 回答コード(コメントの翻訳結果)

ここからは、上記の日本語アルゴリズムを
すべて GASコードに翻訳した回答例 を掲載します。


👋 サンプル①:挨拶メッセージを返す

function makeGreeting(name) {
  const message = "こんにちは、" + name + "さん!";
  return message;
}

function testGreeting() {
  const result = makeGreeting("太郎");
  Logger.log(result);
}

➕ サンプル②:足し算を行う関数

function add(a, b) {
  return a + b;
}

function testAdd() {
  const result = add(5, 3);
  Logger.log("合計:" + result);
}

🎬 サンプル③:映画の合計視聴時間

function totalMovieTime(times) {
  let total = 0;
  for (let i = 0; i < times.length; i++) {
    total += times[i];
  }
  return total;
}

function testMovieTime() {
  const times = [120, 95, 135];
  const total = totalMovieTime(times);
  Logger.log("合計視聴時間:" + total + "");
}

🧾 サンプル④:商品合計金額の計算

function calcItemTotal(price, count) {
  return price * count;
}

function testCalcItemTotal() {
  const total = calcItemTotal(350, 3);
  Logger.log("合計金額:" + total + "");
}

🔍 サンプル⑤:年齢判定

function isAdult(age) {
  if (age >= 20) {
    return true;
  } else {
    return false;
  }
}

function testIsAdult() {
  const result = isAdult(18);
  Logger.log("成人か?:" + result);
}

## 6. 関数を使うと「分かりやすくなる」実例

「関数に分けたほうが読みやすい」例を見てみましょう。

❌ 分けないとこうなる

function order() {
  const price = 500;
  const count = 3;
  const total = price * count;

  if (total >= 1000) {
    Logger.log("合計 " + total + "円:高めです");
  } else {
    Logger.log("合計 " + total + "円:安いです");
  }
}

✅ 関数に分けると読みやすくなる

function calcTotal(price, count) {
  return price * count;
}

function judgePrice(total) {
  if (total >= 1000) {
    Logger.log("高めです");
  } else {
    Logger.log("安いです");
  }
}

function order() {
  const total = calcTotal(500, 3);
  Logger.log("合計:" + total + "");
  judgePrice(total);
}

## 7. まとめ

  • 関数は「処理のまとまり」
  • 引数は「材料」
  • return は「完成した結果」
  • 関数を分けると 再利用しやすく、読みやすく、ミスも減る
  • GASのスクリプトは function を中心に作るので必須知識

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?