![見出し画像](https://assets.st-note.com/production/uploads/images/92012150/rectangle_large_type_2_00b8940211cee6649f2d07078f2d415d.png?width=800)
GAS講座:3日目
講座の感想
山場第一弾。ほどんどぽかーんと話を聞いているだけで終わってしまったので、少しずつ覚えていけたらと思います。配列についてはすこし理解ができた気がするぞ!宿題で振り返りなががんばるぞ
アジェンダ
・関数
・配列
関数
関数とは
一連の処理の集まり。
引数→関数→戻り値
引数 :関数にわたす値
戻り値:関数から受け取る値
関数の定義
function 関数名() {
//処理
}
定義した関数は呼び出し・実行ができる
※プロジェクト内で関数名はかぶらないように!
関数の呼び出し
![](https://assets.st-note.com/img/1668229576902-knmYtsRtWY.png?width=800)
function myFunction() {
sayHello();
console.log('Good Bye.');
}
function sayHello() {
console.log('Hello!');
}
演習-01
function myFunction3_01() {
sayHello();
console.log('Good Bye.');
}
function sayHello() {
console.log('Hello!');
}
引数と仮引数
![](https://assets.st-note.com/img/1668230089627-tE7H13F5Pe.png?width=800)
戻り値
![](https://assets.st-note.com/img/1668230218752-5dZVdo2V6k.png?width=800)
引数と戻り値
![](https://assets.st-note.com/img/1668230307693-NlZ1VtJlH2.png?width=800)
function myFunction() {
console.log(calcArea(3, 4));
}
function calcArea(x, y) {
return x * y;
}
演習-02
function myFunction3_02() {
console.log(calcArea(3, 4));
}
function calcArea(x, y) {
return x * y;
}
関数の便利な利用方法
![](https://assets.st-note.com/img/1668230559375-TObsjACgVS.png?width=800)
関数リテラル
![](https://assets.st-note.com/img/1668230646414-a7nGYEFf0s.png?width=800)
アロー関数
![](https://assets.st-note.com/img/1668230713079-Vtj2aNMEY1.png?width=800)
アロー関数のreturn
![](https://assets.st-note.com/img/1668230794499-95OxkNgEh4.png?width=800)
演習-06
function myFunction3_06() {
const calcArea = x => x ** 2;
console.log(calcArea(3));
}
宣言と関数リテラル
![](https://assets.st-note.com/img/1668230915849-hxk9IKirRa.png?width=800)
配列
GASのデータ型
![](https://assets.st-note.com/img/1668231030256-khlEfvDdXp.png?width=800)
配列とは
![](https://assets.st-note.com/img/1668231078760-payOllXIQh.png?width=800)
配列リテラル
![](https://assets.st-note.com/img/1668231115849-QRjJ5rz7Rl.png?width=800)
配列の代入・参照
![](https://assets.st-note.com/img/1668231184685-85Y6vWqQlB.png?width=800)
演習-08
function myFunction3_08() {
const members = ['Bob', 'Tom', 'Ivy'];
console.log(members[0]);
console.log(members[1]);
console.log(members[2]);
console.log(members);
members[1] = 'Tim';
console.log(members);
}
定数なのになぜ配列の要素の健康が可能か?
![](https://assets.st-note.com/img/1668231291584-jxLwrFfrdN.png?width=800)
lengthプロパティ
![](https://assets.st-note.com/img/1668231378071-stFd4eQxd5.png?width=800)
演習-09
function myFunction3_09() {
const members = ['Bob', 'Tom', 'Jay', 'Tom'];
console.log(members.length);
}
lengthプロパティとインデックス
![](https://assets.st-note.com/img/1668231502088-lHzGbExfaz.png?width=800)
for文による配列のループ
![](https://assets.st-note.com/img/1668231578765-WArByCfJMA.png?width=800)
for…of 文による配列のループ
![](https://assets.st-note.com/img/1668231669191-DvIioyd2fY.png?width=800)
演習-10
function myFunction3_10() {
const members = ['Bob', 'Tom', 'Jay', 'Tom'];
for(let i = 0; i < members.length; i++) {
console.log(members[i]);
}
for(const member of members) {
console.log(member);
}
}
二次元配列とは
![](https://assets.st-note.com/img/1668232021387-wgAydV8g0H.png?width=800)
GASで二次元配列はとってもよく使います
スプレッドシート: セル範囲(行×列)
Gmail: スレッド×メッセージ
二次元配列の要素の参照①
![](https://assets.st-note.com/img/1668232146588-ZBzkmF2Z67.png?width=800)
二次元配列の要素の参照②
![](https://assets.st-note.com/img/1668232234889-xn4eRigqPr.png?width=800)
演習-12
function myFunction3_12() {
const numbers = [[10, 30, 20, 40], [11, 31, 21], [12]];
console.log(numbers[0][0]);
console.log(numbers[0][1]);
console.log(numbers[0][2]);
console.log(numbers[0][3]);
console.log(numbers[0]);
console.log(numbers[1]);
console.log(numbers[2]);
console.log(numbers);
}
この記事が気に入ったらサポートをしてみませんか?