1

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?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Allan Bech
  • 391
  • 1
  • 6

1 Answers1

3

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
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Really close :-) The brackets worked. Two things though; 1) The script puts first event on the startdate even if this is not a monday or wednesday. 2) There is no stop :-) Events keep on for years... – Allan Bech Mar 02 '20 at 07:53
  • Maybe .times(times) fixed 2). – Allan Bech Mar 02 '20 at 08:10
  • @Allan Bech Thank you for replying. I apologize for the inconvenience. About `There is no stop :-) Events keep on for years...`, you want to use `tstop` as "until". If my understanding is correct, could you please confirm the added modification? About `The script puts first event on the startdate even if this is not a monday or wednesday.` and `Maybe .times(times) fixed 2).`, I cannot understand what you want to do. This is due to my poor English skill. I deeply apologize for this. Can you explain about the detail of it? – Tanaike Mar 02 '20 at 08:22
  • I can confirm [tstop] works inside .until. Thank you. I also tried .times([times]) and it worked too. Remaining challenge: If I create events for i.e. MONDAYS and WEDNESDAYS on a SUNDAY, the first appearences will be on those sundays. – Allan Bech Mar 02 '20 at 08:36
  • @Allan Bech Thank you for replying. I'm glad your issue was resolved. About your new question of `If I create events for i.e. MONDAYS and WEDNESDAYS on a SUNDAY, the first appearences will be on those sundays.`, unfortunately, I cannot understand what you want to do. This is due to my poor English skill. I deeply apologize for this. Can you provide the detail of it? – Tanaike Mar 02 '20 at 08:40
  • Thank you for your patience. If I create i.e. 4 events to recur on TUESDAYS - and I create them today - then the first instance will appear today end the next three on the following tuedays. – Allan Bech Mar 02 '20 at 08:46
  • @Allan Bech Thank you for replying. Although I'm not sure whether I could correctly understand about your new question, for example, **how about adding 1 week for "until" date?** Or, how about adding 1 week to the "start" and "until" date? But I might not be able to still correctly understand what you want to do. So if this was not the direction you want, I apologize. – Tanaike Mar 02 '20 at 09:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208831/discussion-between-allan-bech-and-tanaike). – Allan Bech Mar 02 '20 at 09:08