2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Googleカレンダーのエクスポートファイルからイベント一覧を取り出す

Last updated at Posted at 2020-03-02

やりたいたこと

Googleのカレンダーでイベントを細かく管理しているので、過去にどんなことが有ったのかの記録として、取り出しておきたいと思いました。

ところが、Googleカレンダーでカレンダーのエクスポートをすると、vCalendar 形式のファイル(拡張子は .ics)で出力されます。(zip圧縮を解くと)

BEGIN:VEVENT
UID:11A04694-11B0-4053-984C-700328F928BF
DTSTAMP:20150808T044239Z
LOCATION:
TRANSP:OPAQUE
SUMMARY:重要な会議だよ
SEQUENCE:0
DTSTART:20200302T003000Z
DTEND:20200302T013000Z
END:VEVENT

日付と時刻は、こんなパターンもある

DTSTART;VALUE=DATE:20200302
DTEND;VALUE=DATE:20200302

このままでは検索性や一覧性に欠けるので、次のような日時とイベントタイトルが一行になったテキストデータに変換しようと考えました。

2020/03/02 09:30 - 10:30 重要な会議だよ

プログラム

 Mac の OS Xには Perl が標準で入っているので、以前 Mac のカレンダーアプリの .ics ファイルを加工しようとしたときに Perl で書いたプログラムを、Googleカレンダー用に流用しました。微妙に仕様が違うんですよ。
タイムゾーンは面倒なのでTokyo固定で +9 時間するようにしています。

perl のソース

ics2txt.pl
# !/usr/bin/perl
#### use encoding 'utf8', STDOUT=>'shiftjis';
while(<>) {
     ($EventName, $EvantData, $EventTime, $EventEnd, $Reccurence) = "" if(/BEGIN:VEVENT/) ;
     chop;
     chop;
    $EventName =  $1 if(/SUMMARY:(.+)/);
    $EventDate  = substr($1, 0, 4) . '/' . substr($1, 4, 2) . '/' . substr($1, 6, 2)  if(/DTSTART;.+:([0-9]+)/);
    $EventDate  = substr($1, 0, 4) . '/' . substr($1, 4, 2) . '/' . substr($1, 6, 2)  if(/DTSTART:([0-9]+)/);
    $EventTime  = substr($1,1, 2) . ':' . substr($1, 3, 2)  if(/DTSTART;.+:[0-9]+(T[0-9]+)/);
    $EventTime  = substr($1,1, 2) . ':' . substr($1, 3, 2)  if(/DTSTART:[0-9]+(T[0-9]+)/);
    $EventEnd  = substr($1,1, 2) . ':' . substr($1, 3, 2)  if(/DTEND;.+:[0-9]+(T[0-9]+)/);
    $EventEnd  = substr($1,1, 2) . ':' . substr($1, 3, 2)  if(/DTEND:[0-9]+(T[0-9]+)/);
    $Reccurence = " >> " . $1  if(/RRULE:(.+)/);
    if (/END:VEVENT/) {
        $StartHHMM = TimeZone($EventTime);
        $EndHHMM = TimeZone($EventEnd);
        $wk = substr($EventDate,0,10) . " $StartHHMM - $EndHHMM " . $EventName . $Reccurence . "\n";
        printf($wk);
    }
}

sub TimeZone {
    $arg = @_[0];
    return "00:00" if(length $arg < 5);
    $mm = substr($arg, 3, 2);
    $hh = substr($arg, 0, 2);
    $hh = ($hh + 9) % 24;
    if ($hh <= 9) {$hh = "0" . $hh};
    return $hh . ":" . $mm;
}

二行目はコメントアウトしていますが、出力したい文字コードを変更したい場合に書き換えて利用してください。 use encoding 'utf8', STDOUT=>'shiftjis';

利用方法

このプログラムを保存して実行権限を与えたら、ターミナルで以下のようにコマンドを実行します。
(sort を組み合わせて、日時の順にすると良いでしょう)

$ cat (Googleカレンダーでエクスポートしたファイルパス) | ics2txt.pl | sort > (結果を書き出すファイルパス)
(例) $ cat 2020Calendar.ics | ics2txt.pl | sort > 2020Cal.txt

cat を使うと、複数のカレンダーを一括で処理できます。
$ cat Calendars/* | ics2txt.pl | sort >iCalAllData.txt

お役に立てば幸いです。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?