Last active
October 17, 2024 19:49
-
-
Save momo-lab/b019179facedf10c76747ee8ebb55aef to your computer and use it in GitHub Desktop.
Import Japanese holidays from the Cabinet Office with GAS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 内閣府の祝日リストをインポートします。 | |
* | |
* GASに本関数を登録の上、適当なセルに「=importJapaneseHoliday()」と入力すると | |
* 祝日のリストが出力されます(1955年~翌年まで)。 | |
* 毎年このデータは更新されるので、メンテナンス無しで祝日がアップデートされます。 | |
* | |
* 参照: https://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html | |
* | |
* IMPORTDATA関数を直接利用する場合に比べて、以下の点で優れています。 | |
* - ヘッダは出力されないので、任意のヘッダを表示できます。 | |
* - 祝日名の文字化けが無くなります。 | |
* - 祝日は日付型として出力されるため、表示形式の変更が容易です。 | |
* - 引数に開始年を指定することで、利用しない過去の祝日を除くことができます。 | |
* - 最大30日間データはキャッシュされます。 | |
* | |
* @param {number} [start_year] - 取得する開始年。省略時は全件取得する。 | |
* @returns {[Date, string][]} - [...[祝日, 祝日名]] | |
*/ | |
function importJapaneseHoliday(start_year) { | |
const CACHE_KEY = "JAPANESE_HOLIDAY_CSV"; | |
const cache = CacheService.getScriptCache(); | |
var csv = cache.get(CACHE_KEY); | |
if (csv === null) { | |
const url = 'https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv'; | |
csv = UrlFetchApp.fetch(url).getContentText('Shift_JIS'); | |
cache.put(CACHE_KEY, csv, 60 * 60 * 24 * 30); | |
} | |
const result = Utilities.parseCsv(csv) | |
.slice(1) | |
.map(r => [Utilities.parseDate(r[0], 'JST', 'yyyy/M/d'), r[1]]); | |
if (start_year == null) { | |
return result; | |
} | |
return result.filter(r => r[0].getFullYear() >= start_year); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment