Google Apps Scriptでカレンダーのイベント色を取得・変更する方法


GASでカレンダーのイベント色を取得する方法
GASを活用すれば、Googleカレンダーのイベント色を簡単に取得できる。まずはその取得方法について解説しよう。
カレンダーのイベント情報を取得するには、まずカレンダーIDを指定するんだ。
その後、そのカレンダー内のイベントを取得しよう。
function getEventColors() {
const calendar = CalendarApp.getCalendarById('your-calendar-id@example.com');
const events = calendar.getEvents(new Date(), new Date(new Date().setMonth(new Date().getMonth() + 1)));
events.forEach(event => {
Logger.log('Event: ' + event.getTitle() + ', Color: ' + event.getColor());
});
}
ここでは、特定のカレンダーIDに基づき、1か月間のイベントを取得して、その色をログに表示している。
ただし、色の表示はカラーIDになる点に注意しよう。
カラーIDとは、
Googleが定めた イベントカラーの番号(1〜11) のこと
で、次のようなIDと色の対応になる(公式ページ)。
カラーID | プロパティ名 | 色の名前(UI上) | 実際の色 |
---|---|---|---|
1 | PALE_BLUE |
Peacock(ピーコック) | |
2 | PALE_GREEN |
Sage(セージ) | |
3 | MAUVE |
Grape(グレープ) | |
4 | PALE_RED |
Flamingo(フラミンゴ) | |
5 | YELLOW |
Banana(バナナ) | |
6 | ORANGE |
Tangerine(タンジェリン) | |
7 | CYAN |
Lavender(ラベンダー) | |
8 | GRAY |
Graphite(グラファイト) | |
9 | BLUE |
Blueberry(ブルーベリー) | |
10 | GREEN |
Basil(バジル) | |
11 | RED |
Tomato(トマト) |
もし、カラーIDではなく、色の名前を表示したいときは次のコードを試してみよう。
function getEventColors() {
const calendar = CalendarApp.getCalendarById('your-calendar-id@example.com');
const events = calendar.getEvents(
new Date(),
new Date(new Date().setMonth(new Date().getMonth() + 1))
);
const colorMap = {
"1": "Peacock(ピーコック)", // PALE_BLUE
"2": "Sage(セージ)", // PALE_GREEN
"3": "Grape(グレープ)", // MAUVE
"4": "Flamingo(フラミンゴ)", // PALE_RED
"5": "Banana(バナナ)", // YELLOW
"6": "Tangerine(タンジェリン)", // ORANGE
"7": "Lavender(ラベンダー)", // CYAN
"8": "Graphite(グラファイト)", // GRAY
"9": "Blueberry(ブルーベリー)", // BLUE
"10": "Basil(バジル)", // GREEN
"11": "Tomato(トマト)" // RED
};
events.forEach(event => {
const colorId = event.getColor(); // カラーID(例: "4")
const colorName = colorMap[colorId] || "デフォルト";
Logger.log('Event: ' + event.getTitle() + ', Color: ' + colorName);
});
}
GASでカレンダーのイベント色を変更する方法
次に、取得したイベントの色を変更する方法を見ていこう。
イベントの色を変更するには、setColorメソッドを使用するぞ。
function changeEventColors() {
const calendar = CalendarApp.getCalendarById('your-calendar-id@example.com');
const events = calendar.getEvents(new Date(), new Date(new Date().setMonth(new Date().getMonth() + 1)));
events.forEach(event => {
event.setColor(CalendarApp.EventColor.PALE_BLUE);
Logger.log('Changed color of Event: ' + event.getTitle());
});
}
このスクリプトでは、各イベントの色をPALE_BLUEに変えている。
setColorメソッドでCalendarApp.EventColorに定義されている色を指定しているな。
ここでの注意点は、色の指定はプロパティ名でおこなうことだ。
さっきの色の表、もう一度確認しておこう。
練習問題
GASを利用して以下の課題に取り組んでみよう。
- 特定の日にちのイベントのみを取得して、それぞれの色を表示してみてください。
- イベントの色をPALE_BLUE以外の色に変更してみましょう。
解答・解説
下記のスクリプトで特定の日付のイベント色を取得し、別の色に変更できる。
function specificDateEventColors() {
const calendar = CalendarApp.getCalendarById('your-calendar-id@example.com');
const specificDate = new Date(2025, 10, 15); // 取得したい日の指定(例:2025年11月15日)
const events = calendar.getEventsForDay(specificDate);
events.forEach(event => {
Logger.log('Event on specific date: ' + event.getTitle() + ', Color: ' + event.getColor());
event.setColor(CalendarApp.EventColor.MAUVE);
Logger.log('Changed color to MAUVE for Event: ' + event.getTitle());
});
}
getEventsForDayメソッドで指定の日付のイベントを取得。
イベント色を変更したい場合は、setColorメソッドを使い、異なるEventColorを指定する。
ただし、月の番号に注意。JavaScript の Date(year, month, day) は 月が0始まり(0=1月、11=12月) だ。よって、new Date(2025, 10, 15)
は2025年11月15日
となるぞ。