カレンダーの取得
Google Apps Script には、Google カレンダーを操作するためのライブラリであるCalendarAppクラスが標準で組み込まれています。
ここではCalendarAppクラスを使用し、様々な操作の起点となるCalendarApp.Calendarオブジェクトを取得する方法を解説します。
GAS から Google カレンダーにアクセスすることになるため、初回実行時に権限の設定が必要になります。
このページで紹介する関数の概要
Section titled “このページで紹介する関数の概要”取得するための関数だけ知りたいという方は、タブリストから目的のタブを選択していただくことで、関数のサンプルをご覧いただけます。
/** @type { CalendarApp.Calendar[] } */const calendars = CalendarApp.getAllCalendars();/** @type { CalendarApp.Calendar[] } */const calendars = CalendarApp.getAllOwnedCalendars();const CALENDAR_ID = "___取得するカレンダーのID___"
/** @type { CalendarApp.Calendar | null } */const calendar = CalendarApp.getCalendarById(CALENDAR_ID);const CALENDAR_NAME = "ToDo リスト"
/** @type { CalendarApp.Calendar[] } */const calendars = CalendarApp.getCalendarsByName(CALENDAR_NAME);作成者を問わず、全てのカレンダーを取得する
Section titled “作成者を問わず、全てのカレンダーを取得する”Google カレンダーには、自分が所有するカレンダーと、他のユーザーが作成し自分が登録(購読)したカレンダーのいずれも表示されます。
この全てを取得したい場合は、CalendarApp.getAllCalendars関数を使用します。
利用する関数
Section titled “利用する関数”/** * @return { CalendarApp.Calendar[] } 対象ユーザーが所有または登録しているすべてのカレンダー */CalendarApp.getAllCalendars;サンプルコード
Section titled “サンプルコード”/** 全てのカレンダー */const calendars = CalendarApp.getAllCalendars();
console.log(`${calendars.length}件のカレンダーを取得しました`);
for (const calendar of calendars) { console.log(`カレンダー名は「${calendar.getName()}」です`);}自分が所有するカレンダーを全て取得する
Section titled “自分が所有するカレンダーを全て取得する”他のユーザーが作成し自分が登録したカレンダーを除き、自分が所有するカレンダーだけを取得したい場合は、CalendarApp.getAllOwnedCalendars関数を使用します。
利用する関数
Section titled “利用する関数”/** * @return { CalendarApp.Calendar[] } 自分が所有するすべてのカレンダー */CalendarApp.getAllOwnedCalendars;サンプルコード
Section titled “サンプルコード”/** 自分が所有する全てのカレンダー */const calendars = CalendarApp.getAllOwnedCalendars();
console.log(`${calendars.length}件のカレンダーを取得しました`);
for (const calendar of calendars) { console.log(`カレンダー名は「${calendar.getName()}」です`);}ID を指定して取得する
Section titled “ID を指定して取得する”特定の 1 カレンダーを、ID を指定して取得することが可能です。
CalendarApp.getCalendarById関数を使用します。
利用する関数
Section titled “利用する関数”/** * @return { CalendarApp.Calendar | null } 対象カレンダー */CalendarApp.getCalendarById;対象 ID のカレンダーが存在しない場合や、実行ユーザーがそのカレンダーにアクセスできない場合、カレンダーを登録していない場合は、nullが返却されます。
サンプルコード
Section titled “サンプルコード”/** カレンダーのID */const CALENDAR_ID = '___取得するカレンダーのID___';
function myFunction() { /** 対象カレンダー */ const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
if (!calendar) { console.log('カレンダーが見つかりませんでした'); return; }
console.log(`カレンダー名は「${calendar.getName()}」です`);}名前を指定して取得する
Section titled “名前を指定して取得する”特定のカレンダーを、名前を指定して取得することが可能です。
CalendarApp.getCalendarsByName関数を使用します。
利用する関数
Section titled “利用する関数”/** * @return { CalendarApp.Calendar[] } 対象カレンダー */CalendarApp.getCalendarsByName;自分が所有または登録しているカレンダーのうち、名前が一致するものが対象になります。名前の大文字・小文字は区別されません。
カレンダー名には重複が許されているため、取得されるカレンダーは配列になります。
同一名称のカレンダーが 2 つ存在する場合は、どちらも取得することになります。
サンプルコード
Section titled “サンプルコード”/** カレンダー名 */const CALENDAR_NAME = 'ToDo リスト';
/** 対象カレンダー */const calendars = CalendarApp.getCalendarsByName(CALENDAR_NAME);
console.log(`${CALENDAR_NAME}というカレンダーは${calendars.length}件存在します`);