Calendar

【GAS】祝日の予定を削除する カレンダーの定例会議をチャットに通知⑤

前回のおさらい

前回は日本の祝日アカウントから祝日イベントを取得しました。祝日イベントの「日」を取得して、祝日に入れてしまった定例会議イベントを削除する魂胆です。

祝日アカウントのカレンダーオブジェクトを取得しました。

const calendarApp = CalendarApp
const holidayCalendar = calendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com')

祝日アカウントのカレンダーオブジェクトから祝日イベントを取得しました。(翌月1日〜末日までの祝日イベント)

const nextMonthFirstDay = new Date(current.getFullYear(), current.getMonth() + 1, 1)
const nextMonthLastDay = new Date(current.getFullYear(), current.getMonth() + 2, 0)
const holidays = holidayCalendar.getEvents(nextMonthFirstDay, nextMonthLastDay)
console.log(holidays[0].getDescription())
console.log(holidays[0].getTitle())


今回は取得した祝日イベントと同日に入っている定例会議の予定を削除していきます。

ここまでのコード全体

const setRegularMeeting = () => {
  // 翌月末を取得
  const current = new Date()
  const nextMonthLastDay = new Date(current.getFullYear(), current.getMonth() + 2, 0)

  // 繰り返しルールを設定
  const calendarApp = CalendarApp
  const recurrence = calendarApp.newRecurrence()
  const weeklyRecurrenceRule = recurrence.addWeeklyRule()
  const recurrenceRule = weeklyRecurrenceRule.onlyOnWeekday(calendarApp.Weekday.MONDAY).until(nextMonthLastDay)

  // 定例会議の名称、当月最初の定例会議の開始日時、終了日時を設定
  const title = '定例会議ですよ!'
  const dayIndex = new Date(current.getFullYear(), current.getMonth() + 1, 1).getDay()
  const firstDay = (9 - dayIndex) % 7 === 0 ? 7 : (9 - dayIndex) % 7
  const startTime = new Date(current.getFullYear(), current.getMonth() + 1, firstDay, 10, 0, 0)
  const endTime = new Date(current.getFullYear(), current.getMonth() + 1, firstDay, 11, 0, 0)

  // 予定の繰り返しを作成
  const defaultCalendar = calendarApp.getDefaultCalendar()
  const eventSeries = defaultCalendar.createEventSeries(title, startTime, endTime, recurrenceRule)
  
  // 翌月の祝日を取得
  const holidayCalendar = calendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com')
  const nextMonthFirstDay = new Date(current.getFullYear(), current.getMonth() + 1, 1)
  const holidays = holidayCalendar.getEvents(nextMonthFirstDay, nextMonthLastDay)
  console.log(holidays[0].getDescription())
  console.log(holidays[0].getTitle())
}

全体として実現したいこと

  • カレンダーへの追加は毎月1日に翌月分を入れる 
  • 定例会議は毎週月曜日の10時から行われる 
  • 祝日は定例会議の予定が入らない
  • 開始時刻の1時間前にチャットに定例会議の予定が通知される
  • 予定に定型の議事録をくっつける
  • 毎週金曜日に翌週の定例会議の議題を議事録に記入するよう参加者に通知する

翌月の定例会議を取得する

祝日に入ってしまった定例会議を削除するために、まずは翌月のすべての定例会議を取得します。

const defaultCalendar = calendarApp.getDefaultCalendar()
const events = defaultCalendar.getEvents(nextMonthFirstDay, nextMonthLastDay)
const regularMeetings = events.reduce((accum, event) => {
  if (event.getTitle() === title) accum.push(event)
  return accum
}, [])

配列に対して利用できるreduceメソッドという便利なやつを使っています。

イベントのタイトルが「定例会議ですよ!」になっている場合にaccumという一時的な配列にpushしてます(格納)。

で、最後にregularMeetings変数に格納しております。

翌月の定例会議がない場合は処理を抜ける

気持ちよく取得した定例会議の配列に何も入っていない場合は、処理を続ける必要がありません。(ま、たぶん定例会議の配列に何も入っていない場合はないんですけど)

処理を抜けてしまいます。

if (regularMeetings.length === 0) return

祝日に設定された定例会議を取得する

祝日に設定されてしまった定例会議を取得します。

const targets = holidays.map(holiday => {
  return regularMeetings.find(regularMeeting => {
    return regularMeeting.getStartTime().getDate() === holiday.getStartTime().getDate()
  })
})


holidays変数に格納された祝日を一つずつ取り出します。

regularMeetings変数に格納された定例会議も一つずつ取り出します。

祝日と同日に設定されてしまった定例会議を探し出して、配列に格納していきます。

ちなみに同日に設定されていない定例会議は、「undefined」(違います!)が配列に格納されます。

祝日に設定された定例会議を削除する

いよいよ削除です。

targets変数には祝日に設定された定例会議とundefinedが入っています。

targets.forEach(target => {
  if (target) target.deleteEvent()
})

deleteEventメソッド ー イベントを削除するメソッド

構文:イベントオブジェクト.deleteEvent()

戻り値:なし

参照)Google Apps Scriptのマニュアル カレンダーイベントクラス

undefined以外を削除したいのでif (target)としています。


これで削除完了です。

ちなみにこのコードですと、月曜日以外に設定された「定例会議ですよ!」というタイトルのイベントも削除されます。

月曜日の定例会議のみ削除したい場合は抽出条件を付け足すといいです。


次回は定例会議イベントに議事録を添付する処理をやっていきます。

だんだん完成に近づいてきました。


アチョオ

Copied title and URL