nicholsonjf
nicholsonjf

Reputation: 1001

Create a recurring event that repeats Monthly on the third Tuesday

Using Google Apps Script Calendar Service (not the Calendar API Advanced Service) is it possible to create a recurring event that repeats monthly by the day of week? For example, the first event in the series is Tuesday, March 21st. I want this event to repeat monthly on the third Tuesday of the month. The closest I could get to accomplishing this is below:

var recurrence = CalendarApp.newRecurrence();
recurrence = recurrence.addMonthlyRule()
    .interval(this.repeatMonths)
    .onlyOnWeekday(CalendarApp.Weekday[dayOfWeek]);

Does anyone know of a way to do this?

Upvotes: 0

Views: 2296

Answers (4)

Phoenix
Phoenix

Reputation: 1

I know this was asked in 2018 but if anyone still needs to know how to do this.

using this site [https://datatracker.ietf.org/doc/html/rfc5545#autoid-97] as a reference along with the CalendarApp and Calendar API [https://developers.google.com/calendar/api/v3/reference/events/insert#javascript] documentation.

I did this and it worked. Plus if you need to read from sheets. Just put the info into the cells in order.

`/-------- Code Below -----/

var event = {
  'summary': 'title',
 // 'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'generic descr.',
  'start': {
    'dateTime': '2023-12-28T00:01:00',
    'timeZone': 'America/New_York'
  },
  'end': {
    'dateTime': '2023-12-28T23:59:00',
    'timeZone': 'America/New_York'
  },
  'recurrence': [
    'RRULE:FREQ=YEARLY;BYMONTH=12;BYDAY=4TH'
  ],
  'attendees': [
    //{'email': '[email protected]'}
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10}
    ]
  }
};

Logger.log(event);
/*----
//function that creates an event
function createCalendarEvent() {
    let events = SpreadsheetApp.getACtiveSpreadsheet().getRangeByName("events").getValues();

    //add the event to the user's default calendar.

events.forEach(function(e){
    CalendarApp.getCalendarById("").createEvent(
        e[],
        new Date(e[1]),
        new Date(e[2]),
        {guests: e[6],sendInvites:true}
    );
})
}
-----*/
  
  //This actually sends the event. Doesn't need to be changed. Ever. (after adding your calendar ID)
  var request = Calendar.Events.insert( event,calendarId
  );
 // })
  
  }`

Upvotes: 0

Kenny
Kenny

Reputation: 315

This seems like a dirty hack of the worst kind. There must be a better way to handle generating a number sequence. And why is there no dynamic way to get one of the Weekday objects? Horror show aside, this does seem to work.

var start = new Date();  // The event start date
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
var days = [
  CalendarApp.Weekday.SUNDAY,
  CalendarApp.Weekday.MONDAY,
  CalendarApp.Weekday.TUESDAY,
  CalendarApp.Weekday.WEDNESDAY,
  CalendarApp.Weekday.THURSDAY,
  CalendarApp.Weekday.FRIDAY,
  CalendarApp.Weekday.SATURDAY
];
var weekday = days[start.getDay()];
var monthday = start.getDate();
var mod = monthday % 7 - 1;
var good_days = numbers.slice(monthday-mod, monthday-mod+7)

var recurrence = CalendarApp.newRecurrence();
recurrence.addWeeklyRule().onlyOnWeekday(weekday).onlyOnMonthDays(good_days);

The general idea is to determine which week of the month it is by the date. 1-7 has to be the first week, 8-14 second, etc. Then create a monthly recurrence, limited to the appropriate day of week. Finally, limit days of the month to those possibly resulting in the same week-of-month.

It sure would be nice if addMonthlyRule() had a flag to set recurrence by week of month.

Upvotes: 1

D_K
D_K

Reputation: 168

I tried this way and i worked, hope this will work for anyone who still are in confusion

 Calendar calendar = GregorianCalendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH),
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND));



calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK)); // Thursday
            calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3); // third day of month

            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * (7*4) ,pendingIntent);

Upvotes: -1

shortbg
shortbg

Reputation: 11

This will create an event that recurs the 3rd Tuesday of each month from 10am to 12pm. The first event will be on Sept. 11, 2018, and the last event will be March 12, 2019. You would obviously need to build the key parts of the string parameter from your data.

var recurringEvent = myCal.createEventFromDescription("Event Title, the third Tuesday of each Month, 10-12, 2018-09-11 until 2019-03-13");

Upvotes: 1

Related Questions