Google Apps ScriptからGoogle Calendarを操作
する方法は2つのapproachがある
一番情報量もあって、かつ簡単な方法
使うには色々設定する必要がある
以前はよくわからなくて断念したtakker.icon
2024-04-24 16:25:21 サービスからGoogle Calendar APIを選ぶだけで使えるようになった
例:main()を実行すると、前回実行から更新された予定を得られる
code:main.gs(js)
const main =() =>{
logSyncedEvents("<your mail address>");
}
/**
* Retrieve and log events from the given calendar that have been modified
* since the last sync. If the sync token is missing or invalid, log all
* events from up to a month ago (a full sync).
*
* @param {string} calendarId The ID of the calender to retrieve events from.
* @param {boolean} fullSync If true, throw out any existing sync token and
* perform a full sync; if false, use the existing sync token if possible.
*/
function logSyncedEvents(calendarId, fullSync) {
const properties = PropertiesService.getUserProperties();
const options = {
maxResults: 100
};
const syncToken = properties.getProperty('syncToken');
if (syncToken && !fullSync) {
options.syncToken = syncToken;
} else {
// Sync events up to thirty days in the past.
options.timeMin = getRelativeDate(-30, 0).toISOString();
}
// Retrieve events one page at a time.
let events;
let pageToken;
do {
try {
options.pageToken = pageToken;
events = Calendar.Events.list(calendarId, options);
} catch (e) {
// Check to see if the sync token was invalidated by the server;
// if so, perform a full sync instead.
if (e.message === 'Sync token is no longer valid, a full sync is required.') {
properties.deleteProperty('syncToken');
logSyncedEvents(calendarId, true);
return;
}
throw new Error(e.message);
}
if (events.items && events.items.length === 0) {
console.log('No events found.');
return;
}
for (const event of events.items) {
if (event.status === 'cancelled') {
console.log(Event id ${event.id} was cancelled.);
continue;
}
if (event.start.date) {
const start = new Date(event.start.date);
console.log([${event.status}] ${event.summary} (${start.toLocaleDateString()}));
continue;
}
// Events that don't last all day; they have defined start times.
const start = new Date(event.start.dateTime);
console.log([${event.status}] ${event.summary} (${start.toLocaleString()}));
}
pageToken = events.nextPageToken;
} while (pageToken);
properties.setProperty('syncToken', events.nextSyncToken);
}
/**
* Helper function to get a new Date object relative to the current date.
* @param {number} daysOffset The number of days in the future for the new date.
* @param {number} hour The hour of the day for the new date, in the time zone
* of the script.
* @return {Date} The new date.
*/
function getRelativeDate(daysOffset, hour) {
const date = new Date();
date.setDate(date.getDate() + daysOffset);
date.setHours(hour);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
}
これをGASのトリガーのカレンダーからと組み合わせれば、カレンダーの更新のたびに、更新差分を取得するプログラムを実現できる 更新の購読