Google Apps Scriptでカレンダーの説明と場所を取得する方法

GASでガス料金の日程を管理したいんだけど、説明と場所情報をどうやって取得するんだろう?
猫男
猫男
catman
catman
ふっ、スクリプトの見過ぎでガスとGASを見間違えてるようだな。よし、catmanがGASでのカレンダー管理と、説明と場所を取得する方法を教えてやろう!準備はいいか?

GASでカレンダーの説明と場所を取得する方法

作成したイベントから説明と場所を取得するには、以下のスクリプトを書こう。

 function getEventDetails() {
  const calendar = CalendarApp.getDefaultCalendar();
  const events = calendar.getEvents(new Date('2023-11-01T00:00:00'), new Date('2023-11-01T23:59:59'));
  for (let event of events) {
    Logger.log('説明: ' + event.getDescription());
    Logger.log('場所: ' + event.getLocation());
  }
} 

getEvents() で日付範囲のイベントを取得。

ループでそれぞれのイベントから getDescription()getLocation() で情報を取得しているぞ。

 

練習問題

1. カレンダーに新たなイベントを追加するコードを書いてみよう。
2. 作成したイベントの説明と場所を変更するコードを書こう。

 

解答・解説

 // 1. 新たなイベントを追加
function addNewEvent() {
  const calendar = CalendarApp.getDefaultCalendar();
  const startTime = new Date('2023-12-15T09:00:00');
  const endTime = new Date('2023-12-15T10:00:00');
  const options = {
    description: 'Gas Maintenance',
    location: 'Utility Company'
  };
  calendar.createEvent('Gas Maintenance', startTime, endTime, options);
}

// 2. イベントの説明と場所を変更
function updateEventDetails() {
  const calendar = CalendarApp.getDefaultCalendar();
  const events = calendar.getEvents(new Date('2023-11-01T00:00:00'), new Date('2023-11-02T00:00:00'));
  for (let event of events) {
    if (event.getTitle() === 'Gas Maintenance') {
      event.setDescription('Updated Description');
      event.setLocation('Updated Location');
    }
  }
} 

createEvent() で新たなイベントを作成し、setDescription()setLocation() で既存イベントの情報を更新する方法を示しました。

これなら僕もガス料金のイベント管理ができそう!ありがとう、catman!
猫男
猫男
catman
catman
役に立てたようだな。(でも、君はまだガスとGASを間違えているよだな)