How to create an event series with a start date and an end date

Viewed 109

First of all, sorry if my English isn't good as I'm a french guy.

I have a problem with a google script I took on the internet that automatically inputs holliday in my calendar.

Let me explain in detail: I use a google form to get all the data I need to input my calendar, such as the name of the employee, the start date of his holliday, the end date, and other information like mail and stuff. The start date and end date are with hours to indicate at which hour start and finish the holliday.

Here's my problem, I use CreateEventSeries but when I use this, it creates a "bar" until the end date but I would like to have an event day by day, until the end date, that finish at 18pm.

Here's the code i use :

  else if (approval == Approval.Approved && reason == Reason.Vacation) {
    CalendarApp.getCalendarById(email)
      .createEventSeries(
         'CP',
         startDate,
         endDate,
        CalendarApp.newRecurrence().addWeeklyRule()
        .onlyOnWeekdays([CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.WEDNESDAY, CalendarApp.Weekday.THURSDAY, CalendarApp.Weekday.FRIDAY])
        .until(endDate),

    
            {
              description: message,
              sendInvites: true,
            }
            );

    // Send a confirmation email.
    let subject = 'Congés acceptés';
    MailApp.sendEmail(additionalEmail, subject, message, );

    row[Header.NotifiedStatus] = NotifiedStatus.Notified;

    Logger.log(`Approved, calendar event created, row=${JSON.stringify(row)}`);
      
  }

I don't know if I made myself clear.

I don't want an all-day event cause it's not visible enough on the calendar.

In short, I would like to create an event series day by day from 8 am until 18pm with a start date and an end date.

Does someone have a solution or a lead?

Best regards

1 Answers

Issue:

When creating an event series via createEventSeries(title, startTime, endTime, recurrence, options), the parameters startTime and endTime refer to the start and end time of the first event in the series, not of the series as a whole:

startTime: the date and time when the first event in the series starts

endTime: the date and time when the first event in the series ends

This endTime is completely different from what you want to set at until(endDate), which refers to when the series of events will finish.

Code snippet:

For example, if you want to have an event every day that lasts from 8 AM to 6 PM, from, let's say, 1 to 15 May, you would do this:

const startTime = new Date('May 1, 2021 08:00:00 GMT+01:00');
const endTime = new Date('May 1, 2021 18:00:00 GMT+01:00');
const endDate = new Date('May 15, 2021 18:00:00 GMT+01:00');
const recurrence = CalendarApp.newRecurrence().addWeeklyRule()
    .onlyOnWeekdays([
      CalendarApp.Weekday.MONDAY, 
      CalendarApp.Weekday.TUESDAY, 
      CalendarApp.Weekday.WEDNESDAY, 
      CalendarApp.Weekday.THURSDAY, 
      CalendarApp.Weekday.FRIDAY
    ])
    .until(endDate)
CalendarApp.getCalendarById(email).createEventSeries('CP',startTime,endTime,recurrence);

Update:

Assuming you have installed an onFormSubmit trigger, your function could be something like this:

function createEventSeriesOnSubmit(e) {
  const formResponse = e.response;
  let itemResponses = formResponse.getItemResponses();
  const itemNames = ["startDate", "endDate", "startHour", "endHour"];
  const responses = itemNames.map(itemName => getResponseForItem(itemResponses, itemName));
  const [startDay, endDay, startHour, endHour] = responses;
  const startTime = new Date(`${startDay} ${startHour}`);
  const endTime = new Date(`${startDay} ${endHour}`);
  const endDate = new Date(`${endDay} ${endHour}`);
  const recurrence = CalendarApp.newRecurrence().addWeeklyRule()
      .onlyOnWeekdays([
        CalendarApp.Weekday.MONDAY, 
        CalendarApp.Weekday.TUESDAY, 
        CalendarApp.Weekday.WEDNESDAY, 
        CalendarApp.Weekday.THURSDAY, 
        CalendarApp.Weekday.FRIDAY
      ])
      .until(endDate)
  CalendarApp.getCalendarById(email).createEventSeries('CP',startTime,endTime,recurrence);
}

Where "startDate", "endDate", "startHour", "endHour" are the names of the corresponding items in your Form. For example:

enter image description here

As you can see in the code sample, you can build the corresponding date to use in calendar by interpolating the date response and the time response, like this:

const startTime = new Date(`${startDay} ${startHour}`);

The resulting calendar has the series of events corresponding to the start hour, end hour, start date and end date that have been specified:

enter image description here

Related