简体   繁体   中英

how to get the ColorID of a Google Calendar Event in JavaScript

I want to write a script to duplicate events from one Google Calendar to another, but only the events that are colored in bold red .

As it stands now, I can duplicate events between multiple calendars, but I don't know how to get the ColorID from the event, so that only the bold red events are being duplicated. Is such a thing possible in Javascript of by using Google Script?

I searched for this multiple times in the past days, but can only find answers based on PHP. To be honest, I can't imagine such a thing NOT possible through javascript of Google Script.

Here is the script I wrote so far:

function myFunction() }
var CalendarSource = CalendarApp.getCalendarById("CalendarIDPrimaryCalendar@gmail.com"); var CalendarDest = CalendarApp.getCalendarById("CalendarIDDuplicatedCalendar@group.calendar.google.com"); var Today = new Date(); //Deze variabele bevat de datum van vandaag var HalfYear = new Date(new Date(Today).setMonth(Today.getMonth() + 6));//Deze variabele bevat dat datum van vandaag + 6 maanden var EventToCopy = CalendarSource.getEvents(Today, HalfYear); //Deze variabele bevat alle Calendar Events voor het aankomende half jaar

// Nieuwe events aanmaken. for (var i in EventToCopy){ var newEvent = CalendarDest.createEvent(EventToCopy[i].getTitle(), EventToCopy[i].getStartTime(), EventToCopy[i].getEndTime()); } }

What I want is to add an if statement (or something), which checks that only the events which have got ColorID 11 are going to be duplicated.

Can somebody help me?

Thanks in Advance,

Jackuss

After a week of searching, reading, trying and hitting my head against a wall because of was frustrated, I finally created a script that does the task I want :). You can find it below. You are free to use it anyway you want, just acknowledge my work (and my headaches ;) ) by not changing the first comment. I would also love to hear from you when you used it.

 /* ===========================================
    =============== Created by: ===============
    ============== Jack Reinieren =============
    ========== Date: 29 Juli 2016 =============
    ===========================================
 */

// declare variables
var PriCalendar = "PrimaryCalendar@gmail.com"; //name of primary calendar
var SecCalendar = "*********@group.calendar.google.com"; //name of secondary calendar
var CalendarSource = CalendarApp.getCalendarById(PriCalendar); //variable to store the PriCalendar in
var CalendarDest = CalendarApp.getCalendarById(SecCalendar);//variable to store the SecCalendar in
var Today = new Date(); //todays date
var HalfYear = new Date(new Date(Today).setMonth(Today.getMonth() + 6)); //todays date + 6 months
var Events = Calendar.Events.list(PriCalendar, {timeMin:Today.toISOString(), timeMax:HalfYear.toISOString()}).items; 
// The above creates a list of events with the start date of today and the end date 6 months ahead in time.

//Loop to check if there are events with colored in red if so, then copy to the Secondary Calendar
for (var i in Events){ 
  /* Check if status of events is 'confirmed'  AND the colorId of the event is "11" 
(which means "red", check the colorchart in https://eduardopereira.pt/2012/06/google-calendar-api-v3-set-color-color-chart/) 
*/
if (Events[i].status == "confirmed" && Events[i].colorId == "11"){

  /*Check whether an event already exists, 
this is because Google Calendar doesn't really delete events, when you press delete. Instead the event gets the status "cancelled". 
More info here: https://stackoverflow.com/questions/27542721/creating-the-same-event-after-removing-causes-409-error
*/  
  try {
  var newEvent = Calendar.Events.insert(Events[i], SecCalendar);
  }
  catch (e){
    // I don't do anything with the errors I encounter, because for what I want, this is not really necessary.
    // You can put in anything you want of course :)
           }
       }
   }

Here are the clickable links which are also stated in the comments of the code:

As I said before, I would love to hear from you if you used (portions) of this code.

With kind regards.

Jack

If you check this documentation of Google Calendar API , it explain here the basic explanation on how the color works on Calendar API, you can also check here the description of the different property name here.

The colors.get also gives you different example code.

Now, for the javascript or apps Script code that you want, I think this sample code from Calendar Apps Script documentation can give you an idea on how to use colorId

function createEvent() {
var calendarId = 'primary';
var start = getRelativeDate(1, 12);
var end = getRelativeDate(1, 13);
var event = {
summary: 'Lunch Meeting',
location: 'The Deli',
description: 'To discuss our plans for the presentation next week.',
start: {
dateTime: start.toISOString()
},
end: {
dateTime: end.toISOString()
},
attendees: [
{email: 'alice@example.com'},
{email: 'bob@example.com'}
],
// Red background. Use Calendar.Colors.get() for the full list.
colorId: 11
};
event = Calendar.Events.insert(event, calendarId);
Logger.log('Event ID: ' + event.getId());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM