LoginSignup
0
0

More than 3 years have passed since last update.

小売物価統計を Zoho Sheet のカスタム関数 (Deluge) で取得する

Last updated at Posted at 2019-08-22

(初出: 2019-08-21 を一部改訂)

Zoho Sheet は以前は MS Office にそっくりなウェブスプレッドシートだった。今ではだいぶ見た目が変わったが、依然として VBA が使える稀なサービスである。ただ VBA はマクロとしてしか呼べないようで、FUNCTION は定義できるのにそれをセル中の式で使うことはできない。自作の関数は Deluge で書く必要がある。なぜ。

さて、Deluge はドキュメントがはなはだ未整備で困るのだが、Zoho Creator 用の説明を読んでエスパーすると、松本市のガソリン価格を取得する次のような関数を作ることができる。

int GETGASPRICE(int year, int month)
{
  appId = <e-Stat AppID>;
  url = "https://api.e-stat.go.jp/rest/2.1/app/getStatsData?appId=" + appId + "&statsDataId=0003105586&cdArea=20202&cdCat01=07301";
  xmlresult = getUrl(url,false).get("responseText");
  mm = leftpad(month.toString(), 2).replaceAll(" ", "0");
  yyyy = leftpad(year.toString(), 4).replaceAll(" ", "0");
  timeid = yyyy + "00" + mm + mm;
  xpath = "/GET_STATS_DATA/STATISTICAL_DATA/DATA_INF/VALUE";
  if (year == 0 && month == 0) {
    xpath = xpath + "[1]";
  } else {
    xpath = xpath + "[@time='" + timeid + "']";
  }
  xpath = xpath + "/text()";
  result = xmlresult.executeXPath(xpath);
  return result.toLong();
}

エラー処理をしていないのでこの小売物価統計データに含まれていない2014年12月以前を指定すると #VALUE! となる。最新の価格は GETGASPRICE(0, 0) で取得できるが、e-Stat が日付の新しいデータほど先に配置している現状を前提にしている。

0
0
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
0
0