GoogleAppsScript(GAS)備忘録(GoogleCalendar編)

GoogleAppsScriptでカレンダーに予定を登録してみます


# 単一の予定を登録
```javascript:createEvent.gs
function createEvent() {
  const calendarId = 'yourAccount@gmail.com';
  const title = 'gasTest';
  const dateStart = new Date('2025/1/1 09:00');
  const dateEnd =   new Date('2025/1/1 12:00');
  const options = {
    description: 'メモ書き',
    location: '東京都千代田区千代田１−１'
  }

  const calendar = CalendarApp.getCalendarById(calendarId);
  var event = calendar.createEvent(title, dateStart, dateEnd, options);

}
```
<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/106311/39ea0e36-fbc8-1dc0-cf40-0a866fa6bfa0.png" width=50%>



# 複数の予定を登録
## 複数の予定を登録(すべての日)
```javascript:createEvents.gs
// const calendarId = 'yourAccount@gmail.com';
function createEvents() {
  const title = 'gasTest';
  let dateStart = new Date('2025/2/10 09:00');
  let dateEnd =   new Date('2025/2/10 18:00');
  const options = {
    description: 'メモ書き',
    location: '東京都千代田区千代田１−１'
  }

  const calendar = CalendarApp.getCalendarById(calendarId);

  for(i=0; i<5; i++){
    var event = calendar.createEvent(title, dateStart, dateEnd, options);
    event.setColor(CalendarApp.EventColor.BLUE);

    dateStart.setDate(dateStart.getDate()+1);
    dateEnd.setDate(dateEnd.getDate()+1);
  }
}
```
<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/106311/50d89855-2114-f4b5-50e1-c867be5759a8.png" width=70%>

## 複数の予定を登録(休日の場合は登録しない)
日本の祝日カレンダーから該当日のイベントを取得してあれば祝日と判定する
```javascript:isHoliday.gs
function isHoliday(date){
  const holidayCalendarId = "ja.japanese#holiday@group.v.calendar.google.com"
  const calendar = CalendarApp.getCalendarById(holidayCalendarId);

  const events = calendar.getEventsForDay(date);

  if(events.length==0){
    return false
  }else{
    return true
  }
}
```
```javascript:createEventsExceptHolydays.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsExceptHolydays() {
  const title = 'gasTest';
  let dateStart = new Date('2025/2/10 09:00');
  let dateEnd =   new Date('2025/2/10 18:00');
  const options = {
    description: 'メモ書き',
    location: '東京都千代田区千代田１−１'
  }
  
  const calendar = CalendarApp.getCalendarById(calendarId);

  for(i=0; i<5; i++){
    if(!isHoliday(dateStart)){
      var event = calendar.createEvent(title, dateStart, dateEnd, options);
      event.setColor(CalendarApp.EventColor.GREEN);
    }
    dateStart.setDate(dateStart.getDate()+1);
    dateEnd.setDate(dateEnd.getDate()+1);
  }
}
```
<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/106311/b6b89280-a72a-41b4-86dd-1e3e6557e775.png" width=70%>

## 複数の予定を登録(指定曜日の場合は登録しない)
getDayメソッドで曜日を取得して判定する
|戻り値|説明|
|---|---|
|0|日曜日|
|1|月曜日|
|2|火曜日|
|3|水曜日|
|4|木曜日|
|5|金曜日|
|6|土曜日|

- 終了日の指定は終了日の翌日に設定する(今回の例だと3/31にすると3/31 0:00という意味になる)

```javascript:createEventsExceptSpecifiedDays.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsExceptSpecifiedDays() {
  const title = 'gasTest';
  const dateFrom = new Date('2025/3/1');
  const dateUntil = new Date('2025/4/1');
  const options = {
    description: 'メモ書き',
    location: '東京都千代田区千代田１−１'
  }

  const calendar = CalendarApp.getCalendarById(calendarId)
  let dateStart = new Date(dateFrom.toDateString() + ' 09:00:00 GMT+0900 (Japan Standard Time)');  
  let dateEnd =   new Date(dateFrom.toDateString() + ' 18:00:00 GMT+0900 (Japan Standard Time)');

  while(dateStart<dateUntil){
    switch (dateStart.getDay()) {
      // case 0: 
      case 1: 
      case 2: 
      // case 3: 
      case 4: 
      case 5: 
      case 6: 
        var event = calendar.createEvent(title, dateStart, dateEnd, options);
        event.setColor(CalendarApp.EventColor.YELLOW);
    }
    dateStart.setDate(dateStart.getDate()+1);
    dateEnd.setDate(dateEnd.getDate()+1);
  }
}
```
<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/106311/a2adf6c1-4a82-28a2-a59b-c53d46140485.png" width=70%>


## 複数の予定を登録(getterで取得)
- getterで配列の予定情報を取得しカレンダーに登録する
- spreadsheetから情報を取得するようにも変更可能

```javascript:getCalendarArray.gs
function getCalendarArray(){
  let calendarArray = [];

  let title = 'gasTest';
  let dateStart = new Date('2025/2/17 09:00');
  let dateEnd =   new Date('2025/2/17 18:00');
  let options = {
    description: 'メモ書き',
    location: '東京都千代田区千代田１−１'
  }
  let color = CalendarApp.EventColor.ORANGE;
  calendarArray.push([title,dateStart,dateEnd,options,color]);


  title = 'gasTest2';
  dateStart = new Date('2025/2/20 11:00');
  dateEnd =   new Date('2025/2/20 20:00');
  options = {
    description: 'らくがき',
    location: ' 京都府京都市上京区京都御苑'
  }
  color = CalendarApp.EventColor.RED;
  calendarArray.push([title,dateStart,dateEnd,options,color]);

  return calendarArray;
}
```

```javascript:createEventsFromGetter.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsFromGetter() {
  const calendar = CalendarApp.getCalendarById(calendarId)
  const calendarArray = getCalendarArray();

  for (let calendarInfo of calendarArray){
      var event = calendar.createEvent(calendarInfo[0], calendarInfo[1], calendarInfo[2], calendarInfo[3]);
      event.setColor(calendarInfo[4]);
  } 
}
```
<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/106311/332deeb0-0fd7-3413-df0f-c6a3c65df600.png" width=70%>