1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

駅すぱあと VBA SDKを使って、探索結果をIC運賃で取得する

Posted at

本記事はヴァル研究所の支援として作成された記事です

駅すぱあとはAPIを公開しており、駅情報や経路探索などをAPI経由で行えます。メソッドはすべてGETメソッドで、情報取得系のみです。レスポンスはJSONまたはXMLで返ってきます。

この駅すぱあと APIを利用しやすくするSDKを開発しています。SDKはVBAPythonで開発していますが、どちらも非公式SDKなので、公式への問い合わせはご遠慮ください。

今回は、VBA SDKを使って、探索結果をIC運賃で取得します。最近ではICカードを利用して乗車すする人が増えていますので、普通運賃よりも安価な結果を確認するのに利用できます。EkispertAPIMania/VBA-SDK: Excel VBAなどで動作する駅すぱあと VBA SDKです(Windowsのみ)の実践的な使い方として、参考にしてください。

利用している機能

このデモでは、SDKの以下の機能を使っています。

image1.png

セットアップ

Releases · EkispertAPIMania/VBA-SDKにて公開しているExcelファイルをダウンロードするか、リポジトリをクローンした上でvbaidiot/ariawaseを使ってコンパイルします。

git clone https://github.com/vbaidiot/ariawase.git
cd ariawase
mkdir src

この src の中にSDKのリポジトリをクローンします。名前は Ekispert.xlsm です。

git clone https://github.com/EkispertAPIMania/VBA-SDK.git Ekispert.xlsm

そして、ariawase ディレクトリに移動して、以下のコマンドを実行します。

cd ..
cscript vbac.wsf combine

そうすると bin ディレクトリの中に Ekispert.xlsm が生成されます。このファイルの中に、SDKのコードが入っています。

Excelファイルの作成とSDKのインストール

新しいExcelファイルをマクロの有効なファイルとして作成します(.xlsmファイルになります)。開発メニューを開いて、Visual Basicを選択します。

image4.png

一緒に、先ほど作成した(またはダウンロードした)SDKのExcelファイルを開きます。標準モジュール、クラスモジュールをすべてコピー(ドラッグ&ドロップ)してください。

image5.png

これで、ExcelファイルにSDKが組み込まれました。

SDKの初期化

次に、SDKを初期化します。VBAのコードエディタで新しいモジュールを作成し、以下のコードを入力します。

Dim client As Ekispert
Set client = New Ekispert
client.apiKey = "YOUR_API_KEY"

経路探索に用いる条件を生成する

まず、探索条件生成を利用して、IC運賃の探索条件を作成します。利用するのは ToolboxCourseConditionQuery です。

Dim IcQuery As ToolboxCourseConditionQuery
Set IcQuery = client.ToolboxCourseConditionQuery
   
IcQuery.TicketSystemType = "ic" ; IC運賃の指定

そして、条件を生成します。

Dim IcResult As ResultSet
IcResult = IcQuery.Find()
If IcResult.Success = False Then
    Debug.Print IcResult.Error.Message
End If

この結果の、 IcResult.Condition が探索条件になります。

経路探索を行う

次に、生成した探索条件を利用して経路探索を行います。利用するのは CourseExtremeQuery です。 ConditionDetail プロパティは、配列の文字列を受け取る点に注意してください。 IcResult.ConditionT3221233232319:F3321121120000:A23121141: といった文字列であり、本来は IcResult.Condition に対して : で区切った配列を順番に適用します。

Dim CourseExtremeQuery As CourseExtremeQuery
Set CourseExtremeQuery = client.CourseExtremeQuery

CourseExtremeQuery.ViaList(0) = "高円寺"
CourseExtremeQuery.ViaList(1) = "阿佐ヶ谷"
CourseExtremeQuery.ConditionDetail(0) = IcResult.Condition ; 探索条件を適用

Dim Result As ResultSet
Result = CourseExtremeQuery.Find()
If Result.Success = False Then
    Debug.Print Result.Error.Message
End If

そして、 Price.Type = "FareICCard" なデータを探します。

Dim Course As Course
Dim Price As Price

For i = 0 To UBound(Result.Courses)
    Course = Result.Courses(i)
    For j = 0 To UBound(Course.Prices)
        Price = Course.Prices(i)
        If Price.Type = "FareICCard" Then
            Debug.Print Price.Name         ' ICカード乗車券
            Debug.Print Price.Oneway       ' 146
        End If
    Next j
Next i

まとめ

通常の探索の場合、ICカード運賃は出てこないので注意してください。また、実行するタイミング(デフォルトは現在日時)によって FareICCard が探索結果に出てこない場合もあります。ご注意ください。

VBA SDKを使えば、Excel上で簡単に駅すぱあとAPIを利用できます。ぜひ試してみてください。

EkispertAPIMania/VBA-SDK: Excel VBAなどで動作する駅すぱあと VBA SDKです(Windowsのみ)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?