Skip to content

Instantly share code, notes, and snippets.

@yoonchulkoh
Last active December 4, 2019 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoonchulkoh/bc6faaace09d7fa7442dc9d3ae995707 to your computer and use it in GitHub Desktop.
Save yoonchulkoh/bc6faaace09d7fa7442dc9d3ae995707 to your computer and use it in GitHub Desktop.
自分のカレンダーを会社のカレンダーにコピーする
// https://qiita.com/howdy39/items/b92c9ba0b050151a889b
var scriptProperties = PropertiesService.getScriptProperties();
var nextSyncTokenKey = 'NEXT_SYNC_TOKEN';
var otherDestinationCalendarId = '[コピー先のカレンダーのメールアドレス]';
function calendarUpdatedSample(e) {
console.info('calendarUpdatedSample() ------------------------------');
console.log('authMode:%s/calendarId:%s/triggerUid:%s', e.authMode, e.calendarId, e.triggerUid);
var calendarId = e.calendarId;
// 予定取得時にsyncTokenを指定して差分イベントを取得
var optionalArgs = {
'syncToken': getNextSyncToken(calendarId)
};
var events = Calendar.Events.list(calendarId, optionalArgs);
console.log('取得した予定数:%s', events.items.length);
// 差分イベントを処理(サンプルなのここではログ表示しているだけ)
for (var i = 0; i < events.items.length; i++) {
var event = events.items[i];
console.log('event.summary:%s/event.start:%s/event.end:%s/status:%s', event.summary, event.start, event.end, event.status);
console.log(event);
updateDestinationCalendar(event);
}
// 今回処理したイベントを対象外とするためsyncTokenを更新
saveNextSyncToken(events.nextSyncToken);
}
//
function updateDestinationCalendar(event) {
var calendar = CalendarApp.getCalendarById(otherDestinationCalendarId);
if (event.status == 'cancelled') {
// その時間の予約を取得
// TODO: キャンセルイベントに情報がないのでどうやって取ればいいかわからない。。
// 名前がbusyだったら削除
return;
}
var event = calendar.createEvent('busy', new Date(event.start.dateTime), new Date(event.end.dateTime));
}
function getNextSyncToken(calendarId) {
// ScriptPropetiesから取得
var nextSyncToken = scriptProperties.getProperty(nextSyncTokenKey);
if (nextSyncToken) {
console.log('getNextSyncToken(from property):%s', nextSyncToken);
return nextSyncToken
}
// ScriptPropetiesにない場合は、カレンダーから取得
var events = Calendar.Events.list(calendarId, {'timeMin': (new Date()).toISOString()}); // 最後の予定を取らないといけない?みたいなので timeMinを指定
nextSyncToken = events.nextSyncToken;
console.log('getNextSyncToken(from calendar):%s', nextSyncToken);
return nextSyncToken;
}
function saveNextSyncToken(nextSyncToken) {
console.log('saveNextSyncToken:%s', nextSyncToken);
scriptProperties.setProperty(nextSyncTokenKey, nextSyncToken);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment