GASで祝日判定するとき、ググると Google Calendar の祝日カレンダーを使うなどが出てくるが、それを試したところ Exception: The Google Calendar is not enabled for the user.
が出てしまい、権限をどうこうするのがだるかったので祝日APIからデータを取得する方法をとった。
Holidays JP API (日本の祝日API) を利用させてもらった。
コードはこんな感じ。今日の日付とAPIから取得した日付を比較している。
function isSyukujitsu(){ const today = new Date(); const todayDate = `${today.getFullYear()}-${(today.getMonth()+1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}`; const url = 'https://holidays-jp.github.io/api/v1/date.json'; const response = UrlFetchApp.fetch(url).getContentText(); const json = JSON.parse(response); const isSyukujitsu = Object.keys(json).includes(todayDate); return isSyukujitsu; }
雑に動かしているのでコードの綺麗さはあまり気にしてないが、 todayDate のところはもっと綺麗なフォーマット方法がありそう。