0

I'm trying to get a current existing event in my calendar using google scripts, and if the scripts succeed to get the current event, i want to extend it with several minutes, let say 10 in this case. I already found this: How to add minutes to my Date but it seems to be not working using google scripts.

Can someone help me with this?

Regards,

Community
  • 1
  • 1
KLFSBK
  • 1
  • 2
  • Please elaborate on what you want to do and what you have tried so far. Post some code that is not working and that you want to improve. – Chris Jan 22 '15 at 21:19

1 Answers1

2

The link you added "How to add minutes to my Date" is in Java not google script, or Javascript. Java and javascript are two different programming languages.

This should show you how to get an existing calendar event from your calendar. It searches for events that have a keyword in your calendar but it exports the information to an active spread sheet - which isn't what you want.

Here is the key code in google apps script:

// Reference Websites:
// https://developers.google.com/apps-script/reference/calendar/calendar
// https://developers.google.com/apps-script/reference/calendar/calendar-event
//

var mycal = "user@gmail.com"; // change this email to yours
var cal = CalendarApp.getCalendarById(mycal);

// Optional variations on getEvents
// var events = cal.getEvents(new Date("January 3, 2014 00:00:00 CST"), new Date("January 14, 2014 23:59:59 CST"));
// var events = cal.getEvents(new Date("January 3, 2014 00:00:00 CST"), new Date("January 14, 2014 23:59:59 CST"), {search: 'word1'});
// 
// Explanation of how the search section works (as it is NOT quite like most things Google) as part of the getEvents function:
//    {search: 'word1'}              Search for events with word1
//    {search: '-word1'}             Search for events without word1
//    {search: 'word1 word2'}        Search for events with word2 ONLY
//    {search: 'word1-word2'}        Search for events with ????
//    {search: 'word1 -word2'}       Search for events without word2
//    {search: 'word1+word2'}        Search for events with word1 AND word2
//    {search: 'word1+-word2'}       Search for events with word1 AND without word2
//
var events = cal.getEvents(new Date("January 12, 2014 00:00:00 CST"), new Date("January 18, 2014 23:59:59 CST"), {search: 'event name'});

To extend the end time of the event by 10 minutes you would use getStartTime, getEndTime and setTime. You can use the date field like I did below.

Like:

var newEndTime = new Date(events[eventNumber].getEndTime()+10);
var startTime = new Date(events[eventNumber].getStartTime());
events[eventNumber].setTime(startTime,newEndTime); 
Community
  • 1
  • 1
Jordan Stewart
  • 3,187
  • 3
  • 25
  • 37