Search code examples
google-apps-scriptgoogle-sheetsgoogle-calendar-apigoogle-calendar-recurring-events

How can I change onlyOnWeekday(day) with weekday from Google Sheets?


I can make this example example work. What I need is to set the weekday from a cell in column "weekday".

I have tried this:

if(type=='PD'){
  var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(weekday);
  event.setRecurrence(recurrence, tstart, tstop);
}else

...where weekday refers to column "weekday".

What am I doing wrong in my own version?


Solution

  • I could confirm that from your script and shared Spreadsheet, the values of NewWeekday are WEDNESDAY and MONDAY of the string type. In this case, how about the following modification?

    From:

    var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(weekday);
    event.setRecurrence(recurrence, tstart, tstop);
    

    To:

    var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday[NewWeekday]);  // Modified
    event.setRecurrence(recurrence, tstart, tstop);
    

    References:

    If I misunderstood your question and this was not the result you want, I apologize.

    Added:

    • You want to use tstop as "until".

    For this, how about the following modification?

    From:

    var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(weekday);
    event.setRecurrence(recurrence, tstart, tstop);
    

    To:

    var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday[NewWeekday]).until(tstop);  // Modified
    event.setRecurrence(recurrence, tstart);  // Modified