Last active
December 19, 2015 03:29
-
-
Save KxNxT/5890449 to your computer and use it in GitHub Desktop.
Calendar merge using Google Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//まとめた後のカレンダー | |
var outCalId = CAL_ID0; | |
//まとめる前のカレンダー | |
var inCalIds = [ | |
CAL_ID1, | |
CAL_ID2 | |
]; | |
//メイン関数 | |
//出力先カレンダーの未来5ヶ月を削除 | |
//入力元カレンダーから出力先カレンダーにイベントを追加する | |
function main(){ | |
var dateNow = new Date(); | |
//普段は2ヵ月先までを更新するが、毎月1日だけ6ヶ月先まで取得 | |
var month = 2; | |
if(dateNow.getDate() == 1){ month = 6; } | |
var dateFuture = new Date; | |
dateFuture.setTime(dateNow.getTime() + month*30*24*60*60*1000); | |
//OUTPUTカレンダー情報の未来イベントを取得 | |
var outCal = CalendarApp.getCalendarById(outCalId); | |
var oFutureEv = outCal.getEvents(dateNow,dateFuture); | |
//OUTPUTカレンダー情報の未来イベントを削除 | |
for(var i = 0; i < oFutureEv.length;i++){ | |
oFutureEv[i].deleteEvent(); | |
} | |
//OUTPUTカレンダーにINPUカレンダーの未来情報を追加する | |
for(var i = 0; i < inCalIds.length; i++){ | |
mergeCal(outCal, inCalIds[i], dateNow, dateFuture); | |
} | |
} | |
//出力先カレンダーに入力元カレンダーからイベントを追加する | |
function mergeCal(outCal, inCalId, startDate, endDate) { | |
//INPUTカレンダーの未来イベントを取得 | |
var inCal = CalendarApp.getCalendarById(inCalId); | |
var iFutureEv = inCal.getEvents(startDate,endDate); | |
//INPUTカレンダー情報をOUTPUTカレンダーへ追加する | |
for(var i = 0; i < iFutureEv.length;i++){ | |
//終日イベントの場合は終日で作成 | |
if(iFutureEv[i].isAllDayEvent()){ | |
outCal.createAllDayEvent(iFutureEv[i].getTitle() + "<" + inCal.getName() + ">",//元のカレンダー名をタイトルに含める | |
iFutureEv[i].getStartTime(), | |
{description:iFutureEv[i].getDescription(), | |
location:iFutureEv[i].getLocation()}); | |
}else{ | |
outCal.createEvent(iFutureEv[i].getTitle() + "<" + inCal.getName() + ">",//元のカレンダー名をタイトルに含める | |
iFutureEv[i].getStartTime(), | |
iFutureEv[i].getEndTime(), | |
{description:iFutureEv[i].getDescription(), | |
location:iFutureEv[i].getLocation()}); | |
} | |
} | |
} | |
//memo alertの代わり | |
//Browser.msgBox(""); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment