1

Based on this answer by @Ilya, I have created this code:

var event = CalendarApp.getCalendarById(calendarid).createEvent(
    eventtitle,
    startfull,
    endfull,
    {description: eventdescription,
    location: eventaddress}
);
event.id = event.getId();
event.colorId = "#616161";
Calendar.Events.patch(event, calendarid, event.id);

Basically trying to "grey-out" some events of less importance, while leaving others in the default calendar color. This color change happens on the same execution as the actual event creation. The event is created fine, but only with the default calendar color. When the above code runs, I get an error:

Object does not allow properties to be added or changed.

I think that perhaps it has something to do with this but I'm not sure how to resolve it.

For future reference by other users, here's the best list of colors I could find as of now.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
user13708028
  • 315
  • 3
  • 13
  • What value does calendarid hold...in the answer cited , code passes the string 'primary'. – Siddharth Seth Jul 13 '20 at 06:33
  • 1
    Why are you trying to assign to an `event` id? Could you please tell us where (what line) the error happens? I would assume it is this line: `event.id = event.getId()`. Please note that you should not mix `Event` class instances and normal objects – Oleg Valter is with Ukraine Jul 13 '20 at 06:46
  • 1
    There is a difference between built-in (i.e. `CalendarApp`) and advanced services (`Calendar`). When you use the built-in service, you should rely on methods exposed publicly. Property setters on the `Event` instance are non-writable (`writable : false`) most likely, hence the error. When using advanced service - you can do whatever you like provided you follow the specs, but you can't use the API provided by the built-in service – Oleg Valter is with Ukraine Jul 13 '20 at 06:51
  • 2
    And also, it was required to modify the event ID like `event.getId().replace("@google.com", "")`. – Tanaike Jul 13 '20 at 06:58
  • 1
    btw, @Tanaike, thank you - I did not know the `getId()` should be sanitized from `@google.com` myself (I think there is rarely a need for mixing built-in and advanced services, but it is good to know) – Oleg Valter is with Ukraine Jul 13 '20 at 07:02
  • 1
    @Oleg Valter The event ID retrieved by Calendar service is different from the event ID which can be used for Calendar API. I was confused by the error related to the event ID like this before. I'm worry that this situation might also confuse other users. – Tanaike Jul 13 '20 at 07:07
  • user13708028 - please don't add off-topic references to the questions, especially to off-site resources. Your question is fine as it is (in fact, it is an interesting case of mixed usage of services). – Oleg Valter is with Ukraine Jul 13 '20 at 07:56

1 Answers1

2

The color ID can be retrieved with the method of Colors: get in Calendar API. For example, you can test this method at "Try this API". In the case of "grey-out", I think that the color ID might be 8. So please test the following modification.

From:

var event = CalendarApp.getCalendarById(calendarid).createEvent(
    eventtitle,
    startfull,
    endfull,
    {description: eventdescription,
    location: eventaddress}
);
event.id = event.getId();
event.colorId = "#616161";
Calendar.Events.patch(event, calendarid, event.id);

To:

var event = CalendarApp.getCalendarById(calendarid).createEvent(
    eventtitle,
    startfull,
    endfull,
    {description: eventdescription,
    location: eventaddress}
);
Calendar.Events.patch({colorId: 8}, calendarid, event.getId().replace("@google.com", ""));

Note:

  • Unfortunately, event.getId() cannot be used for Calendar.Events.patch. In this case, @google.com is required to be removed. Please be careful this.
  • If you want to use other colors, please retrieve the color ID and use it.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks @Tanaike. I tried both event.colorId = 8 and event.colorId = "8". Keep getting the same error: Object does not allow properties to be added or changed. – user13708028 Jul 13 '20 at 06:41
  • 1
    @user13708028 Thank you for replying. I apologize for the inconvenience. From your replying, I would like to confirm `event` in your script. So in order to correctly understand about your current issue, can you provide your whole script for replicating your issue without your personal information? By this, I would like to confirm it. If you can cooperate to resolve your issue, I'm glad. – Tanaike Jul 13 '20 at 06:45
  • 1
    Sure, I've updated my question above with the event creation section of the code. Thanks! – user13708028 Jul 13 '20 at 06:49
  • 1
    @user13708028 Thank you for replying and adding more information. From your additional information, I updated my answer. Could you please confirm it? – Tanaike Jul 13 '20 at 06:55
  • 1
    @user13708028 Thank you for replying. I'm glad your issue was resolved. I could understand about your issue by your additional information. Thank you, too. – Tanaike Jul 13 '20 at 07:08
  • Just noticed strange behavior, which might be by design. The calendar in question is shared. I see the "special" colors but anyone else see these events in the default calendar color that they have set for this calendar. I'm the owner of the calendar. Is this by design? – user13708028 Jul 13 '20 at 15:19
  • @user13708028 Although I'm not sure whether I could correctly understand about your `"special" colors`, when you check the color ID and the color at [this](https://developers.google.com/calendar/v3/reference/colors/get), what result did you get? When the color ID is used, the color code is used from them. So I think that when the color code is different for the color ID, the result color is different. But I'm not sure whether this is the answer you expect. So if this is not what you expect, how about posting it as new question? By this, many users can think of the situation. How about this? – Tanaike Jul 13 '20 at 22:30
  • @user13708028 For example, as another method, how about giving the event color using Calendar service instead of Calendar API? In that case, please replace `Calendar.Events.patch(###)` with `event.setColor(CalendarApp.EventColor.GRAY)`. I think that this can be also used. How about this? [Ref](https://developers.google.com/apps-script/reference/calendar/calendar-event#setcolorcolor) – Tanaike Jul 13 '20 at 23:03
  • thanks for your continued help. I tried your new suggestion using SetColor. I got no error, but it didn't change the color :( – user13708028 Jul 14 '20 at 06:53
  • @user13708028 Thank you for replying. From your replying, although I tried to replicate your situation, I couldn't replicate it. This is due to my poor skill. I deeply apologize for this. So, how about posting it as new question? By this, a lot of users can see it and the clear answer might be posted. How about this? – Tanaike Jul 14 '20 at 07:00