0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スプレッドシートに記載したスケジュール表をGoogleカレンダーに連携

Last updated at Posted at 2025-10-21

前提

  • 原稿や旅行、結婚式など中長期の予定をスプレッドシートで管理→Googleカレンダーにて確認できるようにGASでスクリプトを作成
  • カレンダーを初期化→登録の処理を行うため中長期の予定用に新しいカレンダーを作成、使用することを推奨です
  • 一つのプロジェクト用にカレンダーを作成する前提で作成(カレンダーを初期化→登録を挟む)
sample.js


function RegisterCalendar(){


// ----------------------------------------
// データ取得セクション
// ----------------------------------------

    // スプレッドシートID
    let spreadSheetId = '★スプレッドシートID★';

    // シート名
    let sheetName = '★シート名を入力★';
    // スプレッドシート全体を取得し、そのまま特定のシート(タブ)を取得
    let sheet = SpreadsheetApp.openById(spreadSheetId).getSheetByName(sheetName);

    // カレンダーIDを取得
    let genkoCalendarId = '★カレンダーID★'
    // カレンダーIDをgetCalendarById()にセットしてカレンダーを取得
    let calendar = CalendarApp.getCalendarById(genkoCalendarId);

// ----------------------------------------
// カレンダー初期化セクション
// ----------------------------------------

    let deleteStartDate = new Date('1970-01-01'); //削除開始日
    let deleteEndDate = new Date('2035-12-31'); //削除終了
    let deleteEvents = calendar.getEvents(deleteStartDate,deleteEndDate); //指定期間のイベントを取得

    // deleteEventsの中身を一つずつ取り出してdeleteEvent()で削除
    for (let i = 0 ; i < deleteEvents.length; i++){
        let eachEvent = deleteEvents[i];
        eachEvent.deleteEvent();
    }


// ----------------------------------------
// カレンダー取得セクション
// ----------------------------------------

    //データが入力されているRangeを取得
    let range = sheet.getDataRange().getValues();
    console.log(range);

    // カラムインデックスの定数定義 (A=0, B=1, ...)
    const COL_TITLE = 0;   // A列
    const COL_STARTDAY = 2;     // C列 (日付)
    const COL_STARTTIME = 3;   // D列 (開始時間)
    const COL_ENDDAY = 4;     // E列 (終了日)
    const COL_ENDTIME = 5;     // F列 (終了時間)
    const COL_DESCRIPTION = 6;  // G列 (メモ)

// ----------------------------------------
// イベント登録セクション
// ----------------------------------------

    // 1行ずつカレンダー情報を取得していく
    for (let i = 1; i < range.length; i++){
    let row = range[i]; //1行ずつ取得
    let title =row[COL_TITLE]; //タイトル
    let startDay = row[COL_STARTDAY]; //日付
    let startTime = row[COL_STARTTIME]; //開始時間
    let endDay = row[COL_ENDDAY]; //終了日
    let endTime = row[COL_ENDTIME]; //終了時間
    let description = row[COL_DESCRIPTION]; //メモ


        //期間指定のない終日イベント
        if(startTime == ''&& endTime == ''&& endDay == '') {
            let date = new Date(startDay);
            //終日イベントを登録
            let event = calendar.createAllDayEvent(title,date,{description:description});
        } 
        //期間指定イベント
        else if(startDay != ''&& endDay != '') 
        {
            let date = new Date(startDay);
            let endDateNext = new Date(endDay);
            //終日イベントの終了日は、実際の終了日の翌日を指定する必要があるため、1日追加
            endDateNext.setDate(endDateNext.getDate() + 1);
            let event = calendar.createAllDayEvent(title,date,endDateNext,{description:description});
        } else {
            //開始、終了日時のあるイベント
            let startTimeEvent = new Date(startDay+' '+startTime); //イベント開始日時
            let endTimeEvent = new Date(endDay+' '+endTime); //イベント終了日時
            
            if(endTimeEvent.getTime() <= startTimeEvent.getTime()){
                //終了日時が開始日時以下の場合、終了日に1日追加
                endTimeEvent.setDate(endTimeEvnent.getDate()+1);
            }
            //日時指定イベントを登録
            let event = calendar.createEvent(title,startTimeEvent,endTimeEvent,{description:description});
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?