35
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

GoogleAppsScriptで日本の祝日を取得する

ざっくりいうと、日本の祝日のカレンダーを取得してきて。今から10分後までにイベントがあるかを調べる感じです。

ちなみにholidaysはCalendarEvent型の配列、CalendarEventが持ってるメソッドとかは以下参照
https://developers.google.com/apps-script/reference/calendar/calendar-event?hl=ja

// 2つ目の引数のmonthは0始まりなので4は5月
function main() {
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 1, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 2, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 3, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 4, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 5, 12, 0, 0)));
  Logger.log(isJapaneseHoliday(new Date(2020, 4, 6, 12, 0, 0)));
}

/*
 * 今日が日本の祝日かどうか判定
 */
function isJapaneseHoliday(date) {
  // 今から10分後までで検索 (もっと短くてもいいかも)
  var startDate = new Date(date.getTime());
  var endDate = new Date(date.getTime() + (10*60*1000));

  var cal = CalendarApp.getCalendarById("ja.japanese#holiday@group.v.calendar.google.com");
  var holidays =  cal.getEvents(startDate, endDate);

  return holidays.length != 0;
}

mainを実行した結果ログ。
ちゃんと2020/05/01と2020/05/02だけfalseになっている。

[20-05-02 19:32:40:332 JST] false
[20-05-02 19:32:40:407 JST] false
[20-05-02 19:32:40:480 JST] true
[20-05-02 19:32:40:544 JST] true
[20-05-02 19:32:40:583 JST] true
[20-05-02 19:32:40:646 JST] true
Why not register and get more from Qiita?
  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
Sign upLogin
35
Help us understand the problem. What are the problem?