11̓R[hꗗ

LESSON 31@iߓƂȂ郁C̊֐낤

SECTION 1@iߓ̃R[h낤

C֐iiߓj̏

function runScheduler() {
  const CALENDAR_IDS = ['primary'];
  const SEARCH_DAYS = 3;
  const periodStart = new Date();
  periodStart.setHours(0, 0, 0, 0);
  const periodEnd = new Date(periodStart);
  periodEnd.setDate(periodStart.getDate() + SEARCH_DAYS);
  console.log('' + periodStart + ' ` ' + periodEnd);
  // --- iA@\̎擾 ---
  // --- iB@󂫎Ԃ̌ ---
  // --- iC@ʂ̏o ---
}

LESSON 32@\W߂֐낤

SECTION 1@͌W̃R[h낤

function getCalendarEvents(calendarIds, periodStart, periodEnd) {
  const events = [];
  for (const id of calendarIds) {
    const calendar = CalendarApp.getCalendarById(id);
    const calendarEvents = calendar.getEvents(periodStart, periodEnd);
    for (const event of calendarEvents) {
      events.push(event);
    }
  }
  return events;
}

SECTION 3@͌W̓mF悤

iA̓mFĎsOɕ\

  // --- iA@\̎擾Ăяo ---
  const allEvents = getCalendarEvents(CALENDAR_IDS, periodStart, periodEnd);
  console.log('擾\萔' + allEvents.length);
  // --- iB@󂫎Ԃ̌ ---

LESSON 33@󂫎ԂT֐낤

SECTION 2@Ԃi߂郋[v

function findAvailableSlots(events, periodStart, periodEnd) {
  const START_HOUR = 10;
  const END_HOUR = 19;
  const MEETING_MINUTES = 60;
  const INTERVAL_MINUTES = 30;
  const slots = [];
  let searchTime = new Date(periodStart);
  searchTime.setHours(START_HOUR, 0, 0, 0);
  while (searchTime < periodEnd) {
    console.log('`FbN: ' + searchTime.toLocaleString());
    searchTime.setMinutes(searchTime.getMinutes() + INTERVAL_MINUTES);
  }
  return slots;
}

SECTION 2@[v̓mF悤

Ԃi߂郋[v̓mF

// --- iB@󂫎Ԃ̌ ---
  const availableSlots = findAvailableSlots(allEvents, periodStart, periodEnd);
  console.log('󂫎:' + availableSlots.length);
  // --- iC@ʂ̏o ---

SECTION 3@͔΂ėɃ[v

[v̑Ώۂ瑁E[̎ԑтO

  while (searchTime < periodEnd) {
    const slotEnd = new Date(searchTime);
    slotEnd.setMinutes(slotEnd.getMinutes() + MEETING_MINUTES);
    const workEndForDay = new Date(searchTime);
    workEndForDay.setHours(END_HOUR, 0, 0, 0);
    if (slotEnd > workEndForDay) {
      console.log('---- ɃXLbv ----');
      searchTime.setDate(searchTime.getDate() + 1);
      searchTime.setHours(START_HOUR, 0, 0, 0);
    } else {
      console.log('`FbN:' + searchTime.toLocaleString());
      searchTime.setMinutes(searchTime.getMinutes() + INTERVAL_MINUTES);
    }
  }

LESSON 34@󂫎Ԃ𔻒肷֐낤

SECTION 3@󂫎Ԃ̔R[hɂ

󂫎Ԃ𔻒肷

function hasConflict(events, slotStart, slotEnd) {
  for (const event of events) {
    if (event.getStartTime() < slotEnd && event.getEndTime() > slotStart) {
      return true;
    }
  }
  return false;
}

SECTION 4@mFp̃R[h낤

֐̓mF

    } else {
      const slotStart = new Date(searchTime);
      const isConflict = hasConflict(events, slotStart, slotEnd);
      console.log(`${slotStart.toLocaleTimeString()}dH : ${isConflict} `);
      searchTime.setMinutes(searchTime.getMinutes() + INTERVAL_MINUTES);
    }

SECTION 5@󂫎Ԃ̌vZW悤

󂫎Ԃz`ŕۑ

      if (isConflict === false) {
        slots.push([slotStart, slotEnd]);
      }

ǉ3ŝ

LESSON 35@\o͂֐낤

SECTION 1@o͌W̃R[h낤

function writeToSheet(slots) {
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  sheet.appendRow(['Jn', 'I']);
  if (slots.length > 0) {
    sheet.getRange(2, 1, slots.length, 2).setValues(slots);
  }
}
