Top Tweets for #eventsbydate
最終的にできたコードはこちら
これをClaudeなどに入れて説明してもらうと意味がわかりますよ
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('予定管理')
.addItem('手動実行', 'dailyAutomation')
.addToUi();
}
function dailyAutomation() {
try {
// 過去の予定を削除
deletePastReservedEvents();
// 将来の予約イベントをクリア
clearFutureReservedEvents();
// 飲み会関連の予定を処理
processDrinkingEvents();
// 新規予定を追加
addReservedEvents();
Logger.log('日次自動処理が完了しました');
} catch (error) {
Logger.log('エラーが発生しました: ' + error.toString());
}
}
function clearFutureReservedEvents() {
const calendarId = '******@gmail.com';
const calendar = CalendarApp.getCalendarById(calendarId);
const today = new Date();
const oneMonthLater = new Date();
oneMonthLater.setMonth(today.getMonth() + 1);
// 今日から1ヶ月後までの「予定を確保」と「🍺 予定あり」のイベントを取得
const futureEvents = calendar.getEvents(today, oneMonthLater);
let deletedCount = 0;
futureEvents.forEach(event => {
const title = event.getTitle();
if (title === '予定を確保' || title === '🍺 予定あり') {
event.deleteEvent();
deletedCount++;
}
});
Logger.log(`${deletedCount}件の将来の予約イベントを削除しました`);
return deletedCount;
}
function processDrinkingEvents() {
const calendarId = '[email protected]';
const calendar = CalendarApp.getCalendarById(calendarId);
const today = new Date();
const oneMonthLater = new Date();
oneMonthLater.setMonth(today.getMonth() + 1);
// キーワードのリスト
const keywords = [
'飲', '懇親', '宴', 'パーティ', '会食',
'集まり', '交流', '親睦', '合同', '打ち上げ',
'ディナー', '歓送迎', 'オフ会'
];
const keywordPattern = keywords.join('|');
const regex = new RegExp(keywordPattern);
// 今日から1ヶ月後までのイベントを取得
const events = calendar.getEvents(today, oneMonthLater);
const drinkingEvents = [];
// まず飲み会関連イベントを処理してリストに追加
events.forEach(event => {
const eventTitle = event.getTitle();
const cleanTitle = eventTitle.replace('🍺 ', ''); // 既存の🍺を除去して検査
if (regex.test(cleanTitle)) {
// まだ🍺が付いていない場合のみタイトルを更新
if (!eventTitle.startsWith('🍺')) {
event.setTitle('🍺 ' + eventTitle);
}
drinkingEvents.push({
date: event.getStartTime(),
title: event.getTitle()
});
}
});
// 次に「🍺 予定あり」イベントを作成
drinkingEvents.forEach(drinkingEvent => {
const startTime = new Date(https://t.co/Fxtx6dhgXO);
startTime.setHours(21, 0, 0);
const endTime = new Date(https://t.co/Fxtx6dhgXO);
endTime.setHours(24, 0, 0);
// 「🍺 予定あり」イベントを作成
calendar.createEvent('🍺 予定あり', startTime, endTime);
});
Logger.log(`${drinkingEvents.length}件の飲み会関連イベントを処理しました`);
}
function deletePastReservedEvents() {
const calendarId = '[email protected]';
const calendar = CalendarApp.getCalendarById(calendarId);
const today = new Date();
const oneWeekAgo = new Date();
oneWeekAgo.setDate(today.getDate() - 7);
// 過去1週間の「予定を確保」と「🍺 予定あり」のイベントを取得
const pastEvents = calendar.getEvents(oneWeekAgo, today);
let deletedCount = 0;
pastEvents.forEach(event => {
const title = event.getTitle();
if (title === '予定を確保' || title === '🍺 予定あり') {
event.deleteEvent();
deletedCount++;
}
});
Logger.log(`${deletedCount}件の過去の予定を削除しました`);
return deletedCount;
}
function addReservedEvents() {
const calendarId = '[email protected]';
const calendar = CalendarApp.getCalendarById(calendarId);
const today = new Date();
const oneMonthLater = new Date();
oneMonthLater.setMonth(today.getMonth() + 1);
const events = calendar.getEvents(today, oneMonthLater);
const eventsByDate = {};
let addedCount = 0;
events.forEach(event => {
const eventDate = event.getStartTime().toDateString();
if (!eventsByDate[eventDate]) {
eventsByDate[eventDate] = [];
}
eventsByDate[eventDate].push(event);
});
for (const date in eventsByDate) {
const dayEvents = eventsByDate[date];
const hasEveningEvent = dayEvents.some(event => {
const startHour = event.getStartTime().getHours();
return startHour >= 19 && startHour < 24;
});
if (hasEveningEvent) {
const reserveStart = new Date(date);
reserveStart.setHours(18, 30, 0);
const reserveEnd = new Date(date);
reserveEnd.setHours(21, 0, 0);
calendar.createEvent('予定を確保', reserveStart, reserveEnd);
addedCount++;
}
}
Logger.log(`${addedCount}件の予定を確保を追加しました`);
return addedCount;
}
Most Popular Users

Elon Musk 
@elonmusk
239.4M followers

Barack Obama 
@barackobama
118.5M followers

Cristiano Ronaldo 
@cristiano
114.3M followers

Donald J. Trump 
@realdonaldtrump
111.7M followers

Narendra Modi 
@narendramodi
106.9M followers

Rihanna 
@rihanna
98.6M followers

NASA 
@nasa
92M followers

Justin Bieber 
@justinbieber
91.7M followers

KATY PERRY 
@katyperry
89.9M followers

Taylor Swift 
@taylorswift13
83.9M followers

Lady Gaga 
@ladygaga
75.4M followers

Virat Kohli 
@imvkohli
73.3M followers

Kim Kardashian 
@kimkardashian
70.8M followers

YouTube 
@youtube
68.6M followers

Neymar Jr 
@neymarjr
66.3M followers

Bill Gates 
@billgates
65.1M followers

Selena Gomez 
@selenagomez
63M followers

The Ellen Show
@theellenshow
62.1M followers

CNN 
@cnn
61.6M followers

X 
@x
60.6M followers
