GoogleAppsScriptでカレンダーに予定を登録してみます
単一の予定を登録
createEvent.gs
function createEvent() {
const calendarId = 'yourAccount@gmail.com';
const title = 'gasTest';
const dateStart = new Date('2025/1/1 09:00');
const dateEnd = new Date('2025/1/1 12:00');
const options = {
description: 'メモ書き',
location: '東京都千代田区千代田1−1'
}
const calendar = CalendarApp.getCalendarById(calendarId);
var event = calendar.createEvent(title, dateStart, dateEnd, options);
}

複数の予定を登録
複数の予定を登録(すべての日)
createEvents.gs
// const calendarId = 'yourAccount@gmail.com';
function createEvents() {
const title = 'gasTest';
let dateStart = new Date('2025/2/10 09:00');
let dateEnd = new Date('2025/2/10 18:00');
const options = {
description: 'メモ書き',
location: '東京都千代田区千代田1−1'
}
const calendar = CalendarApp.getCalendarById(calendarId);
for(i=0; i<5; i++){
var event = calendar.createEvent(title, dateStart, dateEnd, options);
event.setColor(CalendarApp.EventColor.BLUE);
dateStart.setDate(dateStart.getDate()+1);
dateEnd.setDate(dateEnd.getDate()+1);
}
}

複数の予定を登録(休日の場合は登録しない)
日本の祝日カレンダーから該当日のイベントを取得してあれば祝日と判定する
isHoliday.gs
function isHoliday(date){
const holidayCalendarId = "ja.japanese#holiday@group.v.calendar.google.com"
const calendar = CalendarApp.getCalendarById(holidayCalendarId);
const events = calendar.getEventsForDay(date);
if(events.length==0){
return false
}else{
return true
}
}
createEventsExceptHolydays.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsExceptHolydays() {
const title = 'gasTest';
let dateStart = new Date('2025/2/10 09:00');
let dateEnd = new Date('2025/2/10 18:00');
const options = {
description: 'メモ書き',
location: '東京都千代田区千代田1−1'
}
const calendar = CalendarApp.getCalendarById(calendarId);
for(i=0; i<5; i++){
if(!isHoliday(dateStart)){
var event = calendar.createEvent(title, dateStart, dateEnd, options);
event.setColor(CalendarApp.EventColor.GREEN);
}
dateStart.setDate(dateStart.getDate()+1);
dateEnd.setDate(dateEnd.getDate()+1);
}
}

複数の予定を登録(指定曜日の場合は登録しない)
getDayメソッドで曜日を取得して判定する
戻り値 | 説明 |
---|---|
0 | 日曜日 |
1 | 月曜日 |
2 | 火曜日 |
3 | 水曜日 |
4 | 木曜日 |
5 | 金曜日 |
6 | 土曜日 |
- 終了日の指定は終了日の翌日に設定する(今回の例だと3/31にすると3/31 0:00という意味になる)
createEventsExceptSpecifiedDays.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsExceptSpecifiedDays() {
const title = 'gasTest';
const dateFrom = new Date('2025/3/1');
const dateUntil = new Date('2025/4/1');
const options = {
description: 'メモ書き',
location: '東京都千代田区千代田1−1'
}
const calendar = CalendarApp.getCalendarById(calendarId)
let dateStart = new Date(dateFrom.toDateString() + ' 09:00:00 GMT+0900 (Japan Standard Time)');
let dateEnd = new Date(dateFrom.toDateString() + ' 18:00:00 GMT+0900 (Japan Standard Time)');
while(dateStart<dateUntil){
switch (dateStart.getDay()) {
// case 0:
case 1:
case 2:
// case 3:
case 4:
case 5:
case 6:
var event = calendar.createEvent(title, dateStart, dateEnd, options);
event.setColor(CalendarApp.EventColor.YELLOW);
}
dateStart.setDate(dateStart.getDate()+1);
dateEnd.setDate(dateEnd.getDate()+1);
}
}

複数の予定を登録(getterで取得)
- getterで配列の予定情報を取得しカレンダーに登録する
- spreadsheetから情報を取得するようにも変更可能
getCalendarArray.gs
function getCalendarArray(){
let calendarArray = [];
let title = 'gasTest';
let dateStart = new Date('2025/2/17 09:00');
let dateEnd = new Date('2025/2/17 18:00');
let options = {
description: 'メモ書き',
location: '東京都千代田区千代田1−1'
}
let color = CalendarApp.EventColor.ORANGE;
calendarArray.push([title,dateStart,dateEnd,options,color]);
title = 'gasTest2';
dateStart = new Date('2025/2/20 11:00');
dateEnd = new Date('2025/2/20 20:00');
options = {
description: 'らくがき',
location: ' 京都府京都市上京区京都御苑'
}
color = CalendarApp.EventColor.RED;
calendarArray.push([title,dateStart,dateEnd,options,color]);
return calendarArray;
}
createEventsFromGetter.gs
// const calendarId = 'yourAccount@gmail.com';
function createEventsFromGetter() {
const calendar = CalendarApp.getCalendarById(calendarId)
const calendarArray = getCalendarArray();
for (let calendarInfo of calendarArray){
var event = calendar.createEvent(calendarInfo[0], calendarInfo[1], calendarInfo[2], calendarInfo[3]);
event.setColor(calendarInfo[4]);
}
}
