LoginSignup
9
11

More than 5 years have passed since last update.

複数のカレンダーをまとめたカレンダーを作成する

Last updated at Posted at 2014-01-22

複数人のGoogleCalendarから、スケジュールを確認する際に、カレンダーをたくさん追加していると、どうしても利便性が悪くなる。

カレンダーの取得や予定の登録については、GoogleAppsScriptでいろいろ操作できることが分かったので、出張管理用のカレンダーを作成して、そのカレンダーに複数人のカレンダーから、確認したい"出張"とか特定の予定を登録することで把握しやすくしてみた。

作成手順

1. まとめる先のカレンダーの作成

マイカレンダーから、カレンダーの作成を行い、作成したカレンダーのカレンダーID(カレンダー設定から確認できる)をメモする。

2. 予定登録処理スクリプトの作成

Google Driveのスプレッドシートから、ツール > スクリプト エディタから、編集して、GoogleAppsScriptを作成する

3. カレンダー登録処理の作成


// 登録するカレンダーの指定
var base_calendar_id = 'xxxxxx@group.calendar.google.com';

// 取得対象者のカレンダーの指定
var target_calendar_ids = [
'test1@example.com',
'test2@example.com'
];

function main(){
  var start_date = new Date();
  start_date.setTime(start_date.getTime() - 1*24*60*60*1000);
  var month = 2;
  var end_date = new Date();
  end_date.setTime(end_date.getTime() + month*30*24*60*60*1000);
  var to_cal = CalendarApp.getCalendarById(base_calendar_id);
  var old_events = to_cal.getEvents(start_date, end_date);

  old_events.forEach(function(evt){
    evt.deleteEvent();
  });

  for (var i = 0; i < target_calendar_ids.length; i++) {
    var cal_id = target_calendar_ids[i];
    add_events(to_cal, cal_id, start_date, end_date);
  }
}

//出力先カレンダーに入力元カレンダーからイベントを追加する
function add_events(to_cal, cal_id, start_date, end_date) {
  var from_cal = CalendarApp.getCalendarById(cal_id);
  var events   = from_cal.getEvents(start_date, end_date);
  var calendar_owner = from_cal.getName().split('@')[0];
  events.forEach(function(evt){
    // 件名がマッチするものだけ登録
    if (/出張/.exec(evt.getTitle()) != null) {
      if(evt.isAllDayEvent() && evt.getAllDayStartDate() > start_date) {
        var e = to_cal.createAllDayEvent("[" + calendar_owner + "] " + evt.getTitle(),
                                         evt.getStartTime(),
                                         { description: evt.getDescription(),
                                           location: evt.getLocation() });
        e.setTime(evt.getAllDayStartDate(), evt.getAllDayEndDate());
      } else {
        to_cal.createEvent("[" + cal_user + "] " + evt.getTitle(),
                           evt.getStartTime(),
                           evt.getEndTime(),
                           { description: evt.getDescription(),
                             location: evt.getLocation() });
      }
    }
  });
}

4. 定期実行の設定

プロジェクトのトリガーを作成して、定期的に実行するように設定する。

現状、把握している問題

  • 終日で日をまたぐ予定の場合、開始と終了が時刻を含んだ予定に含まれてしまう。
9
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11