Help us understand the problem. What is going on with this article?

【GAS】明日の出張先で雨が振りそうなら通知する

概要

Googleカレンダーの予定から明日の出張先の場所をとってきて、その場所の天気が雨ならGmailに通知メールを送ります。
LINEやSlackに送りたい方はAPI叩いてください。

UI

  • Googleカレンダーに予定とその場所の追加
  • 1日前に予定の場所が雨ならメールが来る 無題3.png

フロー

  • カレンダーから明日の予定を取得
  • それらの場所を取り出す
  • 参考資料2のURLを叩いてお天気情報を手に入れる
  • 雨っぽいならメールを送る

ソース

function sendEmailForWeather() {
  const today = new Date();
  //dayLater が1なら明日、2なら明後日、3以降はお天気データが無いので無理です
  const dayLater = 1;
  //ミリ秒変換による次の日の取得 
  const date = new Date(today.getTime() + dayLater * 24 * 60 * 60 * 1000);
  //明日の予定の取得
  const events = getEventsByDate(date);

  events.map(function(e) {
    //CityIDの取得(細かくすると面倒臭かったのでその県の県庁所在地のCityIDを取得)
    const cityId = getCityIdByLocation(e.getLocation())
    if(cityId != null){
      const obj = JSON.parse(UrlFetchApp.fetch('http://weather.livedoor.com/forecast/webservice/json/v1?city=' + cityId));
      const weather = obj.forecasts[dayLater].image.title;
      if(weather.indexOf("") >= 0) {
        const body = "" + e.getTitle() + "\n" + date + "\n" + e.getLocation() + "\n" + weather;
        GmailApp.sendEmail('xxx@Eメール.アドレス', '予定の場所で天気が悪そうです', body);
      }
    } 
  })

  function getEventsByDate(date) {
    const calendarId = "カレンダーID";
    const calendar = CalendarApp.getCalendarById(calendarId);
    return calendar.getEventsForDay(date);
  }
  //CityIDの取得はいい方法ありそうだが、調べるのもっ面倒なので県の最初の2文字をkeyとして県庁所在地のCityIDをvalueとした
  function getCityIdByLocation(Location) {
    const cityId = {"北海":016010,"青森":020010,"岩手":030010,"宮城":040010,"秋田":050010,"山形":060010,"福島":070010,"東京":130010,"神奈":140010,"埼玉":110010,"千葉":120010,"茨城":080010,"栃木":090010,"群馬":100010,"山梨":190010,"新潟":150010,"長野":200010,"富山":160010,"石川":170010,"福井":180010,"愛知":230010,"岐阜":210010,"静岡":220010,"三重":240010,"大阪":270000,"兵庫":280010,"京都":260010,"滋賀":250010,"奈良":290010,"和歌":300010,"鳥取":310010,"島根":320010,"岡山":330010,"広島":340010,"山口":350020,"徳島":360010,"香川":370000,"愛媛":380010,"高知":390010,"福岡":400010,"大分":440010,"長崎":420010,"佐賀":410010,"熊本":430010,"宮崎":450010,"鹿児":460010,"沖縄":471010};
    return cityId[Location.slice(0,2)];
  }
}

あとがき

2日後の天気しかライブドアでは取れないので1週間後の天気がほしい方はそれらしいサービス探してください。CityIDも県庁所在地の天気でごまかしてるので北海道とかだと当てになりません。

参考資料

  1. 【Google Apps Script】天気予報をWeb APIで取得する方法
  2. お天気Webサービス仕様 - Weather Hacks - livedoor 天気情報
  3. livedoor Weather Hacks APIで、お天気データのJSONを取得するための1次細分区(cityタグ)の地域id一覧まとめ
  4. Class Calendar | Apps Script | Google Developers
  5. Date - JavaScript | MDN

更新

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away