Skip to content

Instantly share code, notes, and snippets.

@ryu1
Last active November 10, 2025 01:50
Show Gist options
  • Select an option

  • Save ryu1/6f1d9f1eead1d3ba8699b3a7924ad4f0 to your computer and use it in GitHub Desktop.

Select an option

Save ryu1/6f1d9f1eead1d3ba8699b3a7924ad4f0 to your computer and use it in GitHub Desktop.
google app scripts for calendar
function removeEventsOnHolidays() {
// 評価期間:現在〜1年後
const now = new Date();
const oneYearLater = new Date(now);
oneYearLater.setFullYear(now.getFullYear() + 1);
keywords = ["event name 1", "event name 2"];
// Googleが公開している日本の祝日カレンダー
const holidayCalendar = CalendarApp.getCalendarById("ja.japanese.official#holiday@group.v.calendar.google.com");
// 自分のカレンダー
const myCalendar = CalendarApp.getDefaultCalendar();
// 祝日イベントを取得(Dateオブジェクトのまま)
const holidayEvents = holidayCalendar.getEvents(now, oneYearLater);
let count = 0;
// 祝日と重複するイベントを削除
holidayEvents.forEach(function (holiday) {
const holidayName = holiday.getTitle();
const holidayFrom = holiday.getStartTime();
const holidayTo = holiday.getEndTime();
// 検索キーワード(タイトルに含まれる文字)
keywords.forEach(function (keyword) {
const events = myCalendar.getEvents(holidayFrom, holidayTo, { search: keyword });
events.forEach(function (event) {
event.deleteEvent();
count++;
Logger.log(`削除しました: ${event.getTitle()} (${holidayFrom} - ${holidayTo} ${holidayName})`);
});
});
});
Logger.log(`削除完了: ${count}件のイベントを削除しました。`);
}
function removeEventsOnWeekends() {
const calendar = CalendarApp.getDefaultCalendar(); // 自分のメインカレンダー
const keywords = [""]; // 🔸削除したいイベント名(部分一致OK)
const today = new Date();
const oneYearLater = new Date(today);
oneYearLater.setFullYear(today.getFullYear() + 1);
// 1年分の全イベントを取得
const events = calendar.getEvents(today, oneYearLater);
let count = 0;
const tz = Session.getScriptTimeZone();
for (const event of events) {
const start = event.getStartTime();
const day = start.getDay(); // 0:日曜, 6:土曜
const title = event.getTitle();
keywords.forEach(function (keyword) {
// 土日かつタイトルにキーワードを含むイベントを削除
if ((day === 0 || day === 6) && title.includes(keyword)) {
Logger.log(`削除: ${Utilities.formatDate(start, tz, "yyyy-MM-dd")} ${title}`);
event.deleteEvent();
count++;
}
})
}
Logger.log(`削除完了: ${count}件のイベントを削除しました。`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment