スプレッドシートに入力した予定をGoogleカレンダーに登録する方法~GAS(Google Apps Script)~

タスク・スケジュール管理

こんにちは、せこしょーです。
GAS(Google Apps Script)を使って、スプレッドシートに入力した予定をGoogleカレンダーに登録しています。
動画はこちら↓↓

登録.gs

function main() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("登録");
  // シートから値を取得する
  const calendarId = sheet.getRange(3, 3).getValue();
  const year = sheet.getRange(3, 4).getValue();
  let startMonth = sheet.getRange(5, 3).getValue();
  let startTh = sheet.getRange(5, 4).getValue();
  let endMonth = sheet.getRange(6, 3).getValue();
  let endTh = sheet.getRange(6, 4).getValue();
  const startHour = sheet.getRange(7, 3).getValue();
  const startMin = sheet.getRange(7, 4).getValue();
  const endHour = sheet.getRange(8, 3).getValue();
  const endMin = sheet.getRange(8, 4).getValue();
  const title = sheet.getRange(9, 3).getValue();
  const description = sheet.getRange(10, 3).getValue();
  const location = sheet.getRange(11, 3).getValue();
  const remindTime = sheet.getRange(7, 6).getValue();
  const timeUnit = sheet.getRange(7, 7).getValue();

  const calendar = new GoogleCalendar(calendarId);
  calendar.registarCalendar(year, startMonth, startTh, endMonth, endTh, startHour, startMin, endHour, endMin, title, description, location, remindTime, timeUnit);
  // シートをクリア
  sheet.getRange(5, 3, 7, 2).clearContent();
}
/**
 * Googleカレンダークラス
*/
class GoogleCalendar {
  constructor(calendarId) {
    this.calendarId = calendarId;
  }
  /**
   * Googleカレンダーに登録する
   * 
   * @param 年、開始月、開始日、終了月、終了日、開始時、開始分、終了時、終了分、タイトル、詳細、場所、通知時間、通知時間の単位
  */
  registarCalendar(year, startMonth, startTh, endMonth, endTh, startHour, startMin, endHour, endMin, title, description, location, remindTime, timeUnit) {
    // 日付を一定のルールで埋める
    [startMonth, startTh, endMonth, endTh] = this.customizeBlankDate_(startMonth, startTh, endMonth, endTh);
    const startDate = new Date(year, startMonth - 1, startTh, startHour, startMin);
    let endDate = new Date(year, endMonth - 1, endTh, endHour, endMin);

    // カレンダーに登録する
    const calendar = CalendarApp.getCalendarById(this.calendarId);
    let calendarEvent = {};
    // 開始時刻、終了時刻が空欄の場合は終日の予定としてカレンダーに登録
    if (startHour === "" && endHour === "") {
      if (startDate.toDateString() == endDate.toDateString()) {
        calendarEvent = calendar.createAllDayEvent(title, startDate, { description: description, location: location })
      } else {
        endDate = new Date(year, endMonth - 1, endTh + 1, endHour, endMin);
        calendarEvent = calendar.createAllDayEvent(title, startDate, endDate, { description: description, location: location });
      }
    } else if (startHour !== "" && endHour === "") {
      endDate = new Date(year, endMonth - 1, endTh + 1, endHour, endMin);
      calendarEvent = calendar.createEvent(title, startDate, endDate, { description: description, location: location });
    } else {
      calendarEvent = calendar.createEvent(title, startDate, endDate, { description: description, location: location });
    }
    // 通知設定を追加
    if (remindTime !== "") {
      const minutesBefore = this.convertToMinutes_(remindTime, timeUnit);
      calendarEvent.addPopupReminder(minutesBefore);
    }
    // 完了ダイアログを表示
    if (startHour === "" && endHour === "") {
      Browser.msgBox("Googleカレンダーに登録しました。\\n\\n" + year + "年" + startMonth + "月" + startTh + "日" + "〜" + year + "年" + endMonth + "月" + endTh + "日" + "\\n" + title);
    } else if (startHour !== "" && endHour === "") {
      Browser.msgBox("Googleカレンダーに登録しました。\\n\\n" + this.formatDate_(startDate) + "〜" + year + "年" + endMonth + "月" + endTh + "日" + "\\n" + title);
    } else {
      Browser.msgBox("Googleカレンダーに登録しました。\\n\\n" + this.formatDate_(startDate) + "〜" + this.formatDate_(endDate) + "\\n" + title);
    }

  }
  /**
   * 時間を分単位の時間に変換する
   *
   * @param {number} remindTime - 通知の時間
   * @param {string} timeUnit - 時間の単位
   * @return {number} minutes - 分単位の時間
  */
  convertToMinutes_(remindTime, timeUnit) {
    let minutes = 0;
    if (timeUnit == "分前") {
      minutes = remindTime;
    } else if (timeUnit == "時間前") {
      minutes = remindTime * 60;
    } else if (timeUnit == "日前") {
      minutes = remindTime * 60 * 24;
    } else if (timeUnit == "週前") {
      minutes = remindTime * 60 * 24 * 7;
    }
    return minutes;
  }
  /**
   * 空白の日付を一定のルールで埋める。
   * 
   * 開始日付、終了日付が両方とも空欄 → 今日日付とする
   * 開始日付のみ空欄 → 終了日付を利用
   * 終了日付のみ空欄 → 開始日付を利用
   * 
  */
  customizeBlankDate_(startMonth, startTh, endMonth, endTh) {
    if (startMonth == "" || startTh == "") {
      if (endMonth == "" || endTh == "") {
        // 開始日付、終了日付が両方とも空欄 → 今日日付とする
        const date = new Date();
        startMonth = endMonth = date.getMonth() + 1;
        startTh = endTh = date.getDate();
      } else {
        // 開始日付のみ空欄 → 終了日付を利用
        startMonth = endMonth;
        startTh = endTh;
      }
    } else {
      if (endMonth == "" || endTh == "") {
        // 終了日付のみ空欄 → 開始日付を利用
        endMonth = startMonth;
        endTh = startTh;
      }
    }
    return [startMonth, startTh, endMonth, endTh];

  }
  /** 
   * 日付をフォーマットして返す。
   * @param {date} date
   * @return {string} yyyy年m月d日h時min分
  */
  formatDate_(date) {
    const y = date.getFullYear();
    const m = date.getMonth() + 1;
    const d = date.getDate();
    const h = date.getHours();
    const min = date.getMinutes();
    return (y + "年" + m + "月" + d + "日" + h + "時" + min + "分");
  }
}

コメント