Google Calendarを操作するJavaScriptライブラリを作成する

Google APIs Client Library for JavaScriptは必要にして十分な機能を有しているが、やや機能が豊富すぎるので、この上に薄いラッパーライブラリを作成して、コーディングしやすくする。

名前:xugcal.js

  • 使用例:予定の新規登録
    // JavaScript
    var sd = new Date(2013,0,1,10,0); // 2013/1/1 10:00
    var ed = new Date(2013,0,1,11,0); // 2013/1/1 11:00
    var summary = 'appointment';
    var location = 'somewhere;
    var description = 'hoge';
    gGCalendar.addEventA(summary, sd, ed, location, description, function(resp){
      var event_id = resp.id; // event id
    });
    

ライブラリの中身は以下の感じ。

  • Google CalendarのEventオブジェクトのサブセット定義
    // JavaScript
    function newEvent(id, summary, startDatetime, endDatetime, 
        location, description, updated, sequence){
      var resource = new Object();
      resource['id'] = id; // GoogleがEvent作成時に付与するID
      resource['summary'] = summary; // 予定のタイトル
      resource['start_dateTime'] = startDatetime;//Dateオブジェクト
      resource['end_dateTime'] = endDatetime;//Dateオブジェクト
      resource['location'] = location; // 場所
      resource['description'] = description; // 説明
      resource['updated'] = updated; // Dateオブジェクト
      resource['sequence'] = sequence; // シーケンス番号(更新時に1ずつ増えていく)
     return resource;
    }
    
  • 予定登録:addEventA(), addEvent()
    // JavaScript
    // 項目を指定して予定登録
    this.addEventA = function(summary, start, end, location,
        description, callback){
      var item = newEvent(null, summary, start, end, location, 
        description, null, 0);
      this.addEvent(item, function(data){
        callback(data);
      });
    }
    // 
    this.addEvent = function (xEvent, callback){
      xEvent['start'] = new Object();
      xEvent['start'].dateTime = xEvent['start_dateTime'].toISOString();
      xEvent['end'] = new Object();
      xEvent['end'].dateTime = xEvent['end_dateTime'].toISOString();
      // xEvent['updated'] は追加時に自動でセットされる
      gapi.client.load('calendar', 'v3', function(){
        var request = gapi.client.calendar.events.insert({
          'calendarId': calendarID,
          'resource': xEvent
        });
        request.execute(function(resp){
          callback(resp);
        });
      });
    }
    
  • 予定更新:updEvent()
    // JavaScript
    this.updEvent = function(xEvent, callback){
      xEvent['start'] = new Object();
      xEvent['start'].dateTime = xEvent['start_dateTime'].toISOString();
      xEvent['end'] = new Object();
      xEvent['end'].dateTime = xEvent['end_dateTime'].toISOString();
      gapi.client.load('calendar', 'v3', function(){
        var request = gapi.client.calendar.events.update({  // メソッド
          'calendarId': calendarID,  // パラメータ
          'eventId': xEvent.id,
          'resource': xEvent
        });
        request.execute(function(resp){
          callback(resp);
        });
      });
    }
    
カテゴリー: Development タグ: パーマリンク