使う人いないかなと思いつつせっかく作ったので。 殴り書きのソースコードですみません... gSyncitとか試してみたのですがなんだかうまく動かなかったのと、最近コーディングしてなかったので気分転換がてら作ってみました。 動作の説明 †Outlook→Google Calendarに予定をコピーするプログラムです。 ソースコード解説 †OAuthとかは http://d.hatena.ne.jp/shimooka/20130912/1378980591 を参考に。 Win32OLEでデータ引っ張ってきてGoogle Calendarにコピーしてます。 Itemの認識はOutlookの方のEntryIdをGoogle CalendarのDescriptionに突っ込んでます。 はまったのは
の3つ。 XAMPPのPHPにOpenSSLを組み込む方法は http://cms.geeev.com/XAMPP/2.html を参考に。 /.gcalというファイルを作って最終更新日時を管理してます。 この更新日時を使ってOutookのAPIのFindをLastModificationTimeを使って検索して最近更新のあった予定だけを抽出しています。 ソースコード †<?php require_once './google-api-php-client-master/src/Google/autoload.php'; $client_id = 'XXXX'; $auth_email = 'XXXX'; $p12key = file_get_contents("XXXX.p12"); $target_calendar = 'XXXXX'; $scopes = array('https://www.googleapis.com/auth/calendar'); $credential = new Google_Auth_AssertionCredentials($auth_email, $scopes, $p12key); $client = new Google_Client(); $client->setClientId($client_id); $client->setAssertionCredentials($credential); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($credential); } $service = new Google_Service_Calendar($client); $cal_list = $service->calendarList->listCalendarList(); $calendar_id = NULL; foreach ($cal_list['items'] as $calendar) { $calendar_id = $calendar->id; break; } $cfg_file = "~/.gcal"; $ol = new COM('Outlook.Application'); $ns = $ol->GetNamespace("MAPI"); $items = $ns->GetDefaultFolder(9)->Items; if(file_exists($cfg_file)){ $timestamp = file_get_contents($cfg_file); }else{ $timestamp = "2013/01/01 00:00"; } $app = $items->Find("[LastModificationTime]>=\"$timestamp\""); $timestamp = date("Y/m/d h:i"); while($app){ $subject = mb_convert_encoding($app->Subject, "UTF-8", "SJIS"); $location = mb_convert_encoding($app->Location, "UTF-8", "SJIS"); $start = $app->Start; $end = $app->End; $events = $service->events->listEvents($calendar_id, array("q" => $app->entryid)); if(count($events) == 0){ printf("create %s\n", $subject); $event = new Google_Service_Calendar_Event(); $event->setSummary($subject); $event->setLocation($location); $event->setDescription($app->entryid); $dt = new Google_Service_Calendar_EventDateTime(); $start = date("Y-m-d\TH:i:s+09:00", strtotime($start)); $dt->setDateTime($start); $event->setStart($dt); $dt = new Google_Service_Calendar_EventDateTime(); $end = date("Y-m-d\TH:i:s+09:00", strtotime($end)); $dt->setDateTime($end); $event->setEnd($dt); $service->events->insert($calendar_id, $event); }else{ printf("update %s\n", $subject); $event = $events[0]; $event->setSummary($subject); $event->setLocation($location); $event->setDescription($app->entryid); $dt = new Google_Service_Calendar_EventDateTime(); $start = date("Y-m-d\TH:i:s+09:00", strtotime($start)); $dt->setDateTime($start); $event->setStart($dt); $dt = new Google_Service_Calendar_EventDateTime(); $end = date("Y-m-d\TH:i:s+09:00", strtotime($end)); $dt->setDateTime($end); $event->setEnd($dt); $service->events->update($calendar_id, $event->getId(), $event); } $app = $items->FindNext; } file_put_contents($cfg_file, $timestamp); ?> |