GoogleAppsScript(GAS)備忘録(基本文法編)

参考文献


[詳解！ Google Apps Script完全入門［第3版］ ～GoogleアプリケーションとGoogle Workspaceの最新プログラミングガイド～](https://www.shuwasystem.co.jp/book/9784798064741.html)

# コメント
```javascript:comment.gs
// 1行コメント

/*
複数行コメント
*/
```
- 「ctrl + /」 でコメント、非コメントにできる

# 変数・定数
## 記法 
|記法|例|対象|
|---|---|---|
|スネークケース|USER_ID|(グローバル)定数名<br>プロパティストアのキー|
|ロワーキャメルケース|userId|関数名<br>(ローカル)変数名<br>(ローカル)定数名|
|アッパーキャメルケース<br>(パスカルケース)|UserId|クラス名|



## 変数宣言
```javascript:declaration.gs
function declarationVariable() {
  //変数の宣言はlet
  let num;
  num = 10;

  console.log(num);

  //1行で複数の変数を宣言できる
  let numA, numB;
  numA = 1;
  numB = 100;

  console.log(numA, numB);

  //変数の宣言と同時に値を代入できる
  let str = 'Hello World!'
  console.log(str);
}
```
```text:Execution log
3:12:11 PM	Notice	Execution started
3:12:12 PM	Info	10
3:12:12 PM	Info	1 100
3:12:12 PM	Info	Hello World!
3:12:12 PM	Notice	Execution completed
```
- varは非推奨

## 定数宣言
```javascript:declarationConstant.gs
function declarationConstant() {
  //定数の宣言はconst
  //宣言と同時に値を代入する
  const myName = 'Towamz';

  console.log(myName);
  //定数は値を変えることができない
  myName = 'Jo'
}
```

```text:Execution log
3:10:53 PM	Notice	Execution started
3:10:53 PM	Info	Towamz
3:10:53 PM	Error	
TypeError: Assignment to constant variable.
declarationConstant	@ Code.gs:24
```
```javascript:declarationConstantError.gs
function declarationConstantError() {
  //宣言と同時に値を代入しないとエラーになる
  const constString;

  console.log(constString);
}
```
```text:Execution log
Syntax error: SyntaxError: Missing initializer in const declaration line: 21 file: Code.gs
```


## 配列宣言
```javascript:declarationArray.gs
function declarationArray() {
  let numArray;
  const strArrayConst= ['a', 'b', 'c', 'd', 'e'];

  numArray = [10, 20, 30, 40, 50];
  console.log(numArray);
  console.log(strArrayConst);

  // 定数でも配列は要素の書き換えができる
  numArray[1] = 100;
  strArrayConst[1] = 'z';
  console.log(numArray);
  console.log(strArrayConst);

  // 定数は配列自体の再代入は不可能
  numArray = [60, 70, 80, 90, 100]
  strArrayConst = ['f', 'g', 'h', 'i', 'j', 'k'];

}
```
```text:Execution log
3:43:07 PM	Notice	Execution started
3:43:07 PM	Info	[ 10, 20, 30, 40, 50 ]
3:43:07 PM	Info	[ 'a', 'b', 'c', 'd', 'e' ]
3:43:07 PM	Info	[ 10, 100, 30, 40, 50 ]
3:43:07 PM	Info	[ 'a', 'z', 'c', 'd', 'e' ]
3:43:07 PM	Error	
TypeError: Assignment to constant variable.
declarationArray	@ Code.gs:50
```


# 配列
## 参照
```javascript:referenceArray.gs
function referenceArray() {
  let numArray;
  numArray = [10, 20, 30, 40, 50];

  //配列はインデックス番号を指定して参照する
  console.log(numArray[0]);
  console.log(numArray[1]);
  console.log(numArray[2]);
  console.log(numArray[3]);
  console.log(numArray[4]);
  //定義していないインデックスでもエラーとならない
  console.log(numArray[5]);

  //配列名のみ指定して配列全体を表示することもできる
  console.log(numArray);

}
```
```text:Execution log
4:15:40 PM	Notice	Execution started
4:15:40 PM	Info	10
4:15:40 PM	Info	20
4:15:40 PM	Info	30
4:15:40 PM	Info	40
4:15:40 PM	Info	50
4:15:40 PM	Info	undefined
4:15:40 PM	Info	[ 10, 20, 30, 40, 50 ]
4:15:40 PM	Notice	Execution completed
```



## 代入
```javascript:assignArray.gs
function assignArray() {
  let numArray;
  numArray = [10, 20, 30, 40, 50];
  console.log(numArray);

  //インデックス番号を指定して値を代入する
  numArray[0] = 100;
  console.log(numArray);

  //存在しないインデックス番号を指定して要素を増やすことができる
  numArray[5] = 60;
  console.log(numArray);

  //連続しないインデックス番号に値を代入すると
  //その間の要素はundifinedで作成される
  numArray[10] = 110;
  console.log(numArray);
  console.log(numArray[6]);
  console.log(numArray[10]);
}
```
```text:Execution log
4:24:11 PM	Notice	Execution started
4:24:12 PM	Info	[ 10, 20, 30, 40, 50 ]
4:24:12 PM	Info	[ 100, 20, 30, 40, 50 ]
4:24:12 PM	Info	[ 100, 20, 30, 40, 50, 60 ]
4:24:12 PM	Info	[ 100, 20, 30, 40, 50, 60, , , , , 110 ]
4:24:12 PM	Info	undefined
4:24:12 PM	Info	110
4:24:12 PM	Notice	Execution completed
```

## 分割代入
```javascript:destructuringAssignmentArray.gs
function destructuringAssignmentArray(){
  let numArray = [10, 20, 30, 40, 50];
  let num1, num2, num3, num4, num5;

  //配列要素をひとつずつ変数へ代入する
  [num1, num2, num3, num4, num5] = numArray;
  console.log(numArray);
  console.log(num1, num2, num3, num4, num5);


  let strArray = ['mouse', 'cow', 'tiger', 'rabbit', 'dragon'];
  let str1, str2, str3, str4, str5;

  [str1, str2, str3, str4, str5] = strArray;
  console.log(strArray);
  console.log(str1, str2, str3, str4, str5);
}
```
```text:Execution log
5:42:20 PM	Notice	Execution started
5:42:20 PM	Info	[ 10, 20, 30, 40, 50 ]
5:42:20 PM	Info	10 20 30 40 50
5:42:20 PM	Info	[ 'mouse', 'cow', 'tiger', 'rabbit', 'dragon' ]
5:42:20 PM	Info	mouse cow tiger rabbit dragon
5:42:20 PM	Notice	Execution completed
```

## スプレッド構文
```javascript:.gs
function spreadSyntaxArray(){
  let numArray = [10, 20, 30, 40, 50];
  console.log(numArray);
  // 配列要素を展開する
  console.log(...numArray);

  // 配列の一部に組み込んで新しい配列を作る
  let numArray2 = [0, ...numArray, 60]
  console.log(numArray2);
  console.log(...numArray2);

  // 2つの配列を結合して新しい配列を作る
  let numArray3 = [...numArray, ...numArray2]
  console.log(numArray3);
  console.log(...numArray3);
}
```
```text:Execution log
5:48:49 PM	Notice	Execution started
5:48:49 PM	Info	[ 10, 20, 30, 40, 50 ]
5:48:49 PM	Info	10 20 30 40 50
5:48:49 PM	Info	[ 0, 10, 20, 30, 40, 50, 60 ]
5:48:49 PM	Info	0 10 20 30 40 50 60
5:48:49 PM	Info	[ 10, 20, 30, 40, 50, 0, 10, 20, 30, 40, 50, 60 ]
5:48:49 PM	Info	10 20 30 40 50 0 10 20 30 40 50 60
5:48:50 PM	Notice	Execution completed
```

# 演算子
## 比較演算子
|演算子|説明|
|---|---|
|==|左辺と右辺が等しい(値)|
|!=|左辺と右辺等しくない(値)|
|<|左辺が右辺未満|
|<=|左辺が右辺以下|
|>|左辺が右辺を超える|
|>=|左辺が右辺以上|
|===|左辺と右辺が等しい(値・データ型)|
|!==|左辺と右辺が等しくない(値・データ型)|

## 論理演算子
|演算子|説明|
|---|---|
|&&|and|
|\|\||or|
|!|true/falseを反転|

# if
## if
```javascript:if.gs
function ifStatement() {
  const x = 5;

  if (x < 10){
    console.log('xは10未満です')
  }
}
```
```text:Execution log
6:01:28 PM	Notice	Execution started
6:01:27 PM	Info	xは10未満です
6:01:28 PM	Notice	Execution completed
```

## if-else
```javascript:if-else.gs
function ifElseStatement() {
  const x = 5;

  if (x < 10){
    console.log('xは10未満');
  }else{
    console.log('xは10以上');
  }
}
```
```text:Execution log
6:01:41 PM	Notice	Execution started
6:01:41 PM	Info	xは10未満
6:01:41 PM	Notice	Execution completed
```


## if-else if-else
```javascript:if-else if-else.gs
function ifElseIfElseStatement() {
  const x = 5;

  if (x < 5){
    console.log('xは5未満');
  }else if(x < 10){
    console.log('xは5以上10未満');
  }else{
    console.log('xは10以上');
  }
}
```
```text:Execution log
6:01:56 PM	Notice	Execution started
6:01:56 PM	Info	xは5以上10未満
6:01:57 PM	Notice	Execution completed
```
## コピペ用
```javascript:ifForCopyAndPaste.gs
if (){

}else if(){

}else if(){

}else if(){

}else{

}
```



# switch
## switch
```javascript:switch.gs
function switchStatement() {
  const month = 5

  switch (month){
    case 3:
    case 4:
    case 5:
      console.log('春です');
      break;
    case 6:
    case 7:
    case 8:
      console.log('夏です');
      break;
    case 9:
    case 10:
    case 11:
      console.log('秋です');
      break;
    case 1:
    case 2:
    case 12:
      console.log('冬です');
      break;
    default:
      console.log('月ではありません');
  }
}
```
```text:Execution log
6:10:27 PM	Notice	Execution started
6:10:27 PM	Info	春です
6:10:28 PM	Notice	Execution completed
```
## コピペ用
```javascript:switchForCopyAndPaste.gs
switch (){
  case :

    break;
  case :

    break;
  case :
  case :
  case :

    break;
  default:

}
```

# while
## while
```javascript:while.gs
function whileStatement() {
  let x = 0;

  while (x < 10){
    x += 2;
    console.log(`x:${x}`);
  }
}
```
```text:Execution log
6:21:50 PM	Notice	Execution started
6:21:50 PM	Info	x:2
6:21:50 PM	Info	x:4
6:21:50 PM	Info	x:6
6:21:50 PM	Info	x:8
6:21:50 PM	Info	x:10
6:21:51 PM	Notice	Execution completed
```
## do-while
```javascript:while.gs
function doWhileStatement(){
  let i = 0
  do{
    console.log(i);
    i++;
  }while(i<5);
}
```
```text:Execution log
8:38:38 PM	Notice	Execution started
8:38:38 PM	Info	0
8:38:38 PM	Info	1
8:38:38 PM	Info	2
8:38:38 PM	Info	3
8:38:38 PM	Info	4
8:38:38 PM	Notice	Execution completed
```



## コピペ用
```javascript:whileForCopyAndPaste.gs
while(){

}
```
```javascript:doWhileForCopyAndPaste.gs
do{

}while();
```

# for
## for
```javascript:for.gs
function forStatement() {
  for(i=0; i<5; i++){
    console.log(i);
  }
}
```
```text:Execution log
9:00:33 PM	Notice	Execution started
9:00:33 PM	Info	0
9:00:33 PM	Info	1
9:00:33 PM	Info	2
9:00:33 PM	Info	3
9:00:33 PM	Info	4
9:00:34 PM	Notice	Execution completed
```

## for-Of
```javascript:forOf.gs
function forOfStatement() {
  let strArray = ['a', 'b', 'c', 'd', 'e'];

  for(let str of strArray){
    console.log(str);
  }
}
```
```text:Execution log
9:22:46 PM	Notice	Execution started
9:22:47 PM	Info	a
9:22:47 PM	Info	b
9:22:47 PM	Info	c
9:22:47 PM	Info	d
9:22:47 PM	Info	e
9:22:47 PM	Notice	Execution completed
```


## for-In
```javascript:forIn.gs
function forInStatement() {
  let strArray = ['a', 'b', 'c', 'd', 'e'];
  let strNumDictionay = {
    a: 1,
    b: 2,
    c: 4,
    d: 8,
    e: 16 
  };

  console.log('array');
  for(let i in strArray){
    console.log(i, strArray[i]);
  }

  console.log('dictionary');
  for(let str in strNumDictionay){
    console.log(str, strNumDictionay[str]);
  }
}
```
```text:Execution log
9:24:32 PM	Notice	Execution started
9:24:33 PM	Info	array
9:24:33 PM	Info	0 a
9:24:33 PM	Info	1 b
9:24:33 PM	Info	2 c
9:24:33 PM	Info	3 d
9:24:33 PM	Info	4 e
9:24:33 PM	Info	dictionary
9:24:33 PM	Info	a 1
9:24:33 PM	Info	b 2
9:24:33 PM	Info	c 4
9:24:33 PM	Info	d 8
9:24:33 PM	Info	e 16
9:24:34 PM	Notice	Execution completed
```
- 順番が保証されない

## コピペ用
```javascript:forCopyAndPaste.gs
for(i=0; i<5; i++){

}
```
```javascript:forOfCopyAndPaste.gs
for(let i of Array){

}
```
```javascript:forInCopyAndPaste.gs
for(let i in Dictinary){

}
```

<!--

## タイトル
```javascript:.gs

```
```text:Execution log

```


### タイトル
```javascript:.gs

```
```text:Execution log

```

-->