Google Calendar: cannot call getEvents method - google-apps-script

I'm trying to get all the events in a calendar using the Script Editor.
function getEvents() {
var cal = CalendarApp.getCalendarById('aaa#bbb.com');
var events = cal.getEvents(new Date("01/01/2018 12:00 AM"), new Date("12/31/2018 11:59 PM"));
for(i=0; i<events.length;i++){
var title = events[i].getTitle()
Logger.log(title)
}
}
I'm returning an error in line 4. It says:
TypeError: Cannot call method "getEvents" of null. (lĂ­nea 4, archivo
"Calendar")Ignorar
What's wrong and how should it be fixed?
Thanks a lot

Related

Google Apps Script : The parameters (String) don't match the method signature for CalendarApp.Calendar.createEvent

I'm trying to write a script for google apps script that will create an event everyday at 09:00 (UTC-4) and reporting the value displayed in the xpath //div/div/div/div/div/div/div/div/div/a displayed in this URL https://etherscan.io/address/0x260ee8f2b0c167e0cd6119b2df923fd061dc1093
I tried to change so many things but i still don't understand why i get this error message :
"Exception: The parameters (String) don't match the method signature for CalendarApp.Calendar.createEvent." Line 21
Please help
function createEvent() {
var startTime = new Date();
startTime.setHours(9);
startTime.setMinutes(0);
startTime.setSeconds(0);
startTime.setUTCHours(-4);
var endTime = new Date();
endTime.setHours(10);
endTime.setMinutes(0);
endTime.setSeconds(0);
endTime.setUTCHours(-4);
var event = {
summary: 'Etherscan Value Report',
start: {
dateTime: startTime.toISOString(),
},
end: {
dateTime: endTime.toISOString(),
}
};
CalendarApp.getDefaultCalendar().createEvent(event);
}
function reportSpanValue() {
var response = UrlFetchApp.fetch("https://etherscan.io/address/0x260ee8f2b0c167e0cd6119b2df923fd061dc1093");
var html = response.getContentText();
var xpath = '//div/div/div/div/div/div/div/div/div/a';
var value = xpath.evaluate(html, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML;
Logger.log(value);
}
function createTrigger() {
ScriptApp.newTrigger('reportSpanValue')
.timeBased()
.atHour(9)
.everyDays(1)
.atMinute(0)
.create();
}
To resolve this issue I tried to pass an event object to the createEvent method, not a string. But it didn't work
Try:
CalendarApp.getDefaultCalendar().createEvent('Etherscan Value Report',startTime,endTime)

Create an Event with an ID to Google Calendar: Google Script

I am trying to add an event to Google Calendar, and I am not getting an event added when doing so. I am sure the code is working around it. Does anyone have any ideas why an event is not being added to the calendar?
I would add the entire code, but I feel like it misses the point.
var calendarId = "Typed Out ID of Calendar";
var calendar = CalendarApp.getCalendarById(calendarId);
calendar.createEvent('Breakfast',
new Date("April 19, 2021"));
You can use this to display your calendar ids:
function displayCalendarInfo() {
var cals=CalendarApp.getAllCalendars();
var s='Calendar Information';
for(var i=0;i<cals.length;i++)
{
s+=Utilities.formatString('<br />Name: %s Id: %s', cals[i].getName(),cals[i].getId());
}
s+='<br /><input type="button" value="Close" onClick="google.script.host.close();" />';
var ui=HtmlService.createHtmlOutput(s).setWidth(1200).setHeight(450);
SpreadsheetApp.getUi().showModelessDialog(ui, 'Calendar Data');
}
Just to make sure that you using the correct id.
Create Event: (It works)
function createEvent() {
const cal = CalendarApp.getCalendarById('id');
const start = new Date(2021,3,20,10,30,0,0);//4/20/2021 10:30:00
const end = new Date(2021,3,20,11,30,0,0);//4/20/2021 11:30:00
cal.createEvent('new event',start,end);
}
Months range from Jan - Dec which is 0 - 11 which one less than you would expect

Apps Script ReferenceError: eventCal is not defined

I created this function
function createEvent() {
emailList = ['test#gmail.com','test1#gmail.com','test2#gmail.com'];
var startTime = new Date('Feb 11, 2021 12:00:00 EST');
var endTime = new Date('Feb 11, 2021 14:00:00 EST');
var description = "Here is the link for the webinar: ";
var event = {'location': '','description': description,'guests':emailList+',', 'sendInvites':'True'};
eventCal.createEvent(summary, startTime, endTime, event).setGuestsCanSeeGuests(false);
}
But when I try running the script from the editor I see this error
ReferenceError: eventCal is not defined (line 7, file "Code")
Can somebody please help

Duplicate new google calendar event to another calendar with google script

I'm trying to create a script so when I create an event in one google calendar it automatically duplicates the event in another google calendar. I have tried using the code below (swapping Calendar IDs with my own - assuming I use the full ID e.g. xyz#group.google etc):
function myFunction() {
var calendarSource = CalendarApp.getCalendarById("calendarID");
var calendarDestination = CalendarApp.getCalendarById("calendarID");
var eventToCopy = calendarSource.getEvents(new Date("July 21, 2009 EST"), new Date("July 22, 2009 EST"));
//read up: https://developers.google.com/apps-script/class_recurrence
var newRecurrence = CalendarApp.newRecurrence().addWeeklyRule().times(10);
for (var i in eventToCopy){
if (eventToCopy[i].getTitle() == "event name"){
var newEvent = calendarDestination.createEventSeries(eventToCopy[i].getTitle(), eventToCopy[i].getStartTime(), eventToCopy[i].getEndTime(), newRecurrence);
}
}
Nothing seems to happen though. Anyone give me a headstart? I only want this to trigger on creation of a NEW event.
In the code snippet you posted, events are only copied to the new calendar if their title equals "event name" due to the line if (eventToCopy[i].getTitle() == "event name"){. The events you have in your source calendar probably don't have this title, so nothing is being copied. You should be able to remove this if condition to get all events to copy.
function myFunction() {
var calendarSource = CalendarApp.getCalendarById("calendarID");
var calendarDestination = CalendarApp.getCalendarById("calendarID");
var eventToCopy = calendarSource.getEvents(new Date("July 21, 2009 EST"), new Date("July 22, 2009 EST"));
//read up: https://developers.google.com/apps-script/class_recurrence
var newRecurrence = CalendarApp.newRecurrence().addWeeklyRule().times(10);
for (var i in eventToCopy){
var newEvent = calendarDestination.createEventSeries(eventToCopy[i].getTitle(), eventToCopy[i].getStartTime(), eventToCopy[i].getEndTime(), newRecurrence);
}
}

CalendarEventSeries, missing setRecurrence() method?

Google scripts doc states CalendarEventSeries should have at least two methods setRecurrence(). When I'm trying to use it:
setRecurrence(new Date(), CalendarApp.newRecurrence().addYearlyRule().interval(1));
I'm getting this error:
Cannot find method (class)setRecurrence(object,$Proxy754). (line 60,
file "Sync").
The method is invoked on CalendarEventSeries for sure. How to resolve that error?
The example you show applies to all day Events (specifying only one date for the first occurrence), are you applying to such an event or to a 'normal' event ?
In this last case you should provide 2 dates (start and end) for the first occurrence.
The link above shows the documentation that is pretty clear about it...
Here is an "end-to-end" example that shows how it works, the first function creates an event with a yearly recurrence starting today, the second one changes this recurrence to be daily, starting today...
It uses your default calendar
function createTestSerie(){
var cal = CalendarApp.getDefaultCalendar()
if (cal) {
var title = 'Test Event';
var desc = 'Created using Google Apps Script';
var loc = 'Here, there and everywhere...';
var recur = CalendarApp.newRecurrence().addYearlyRule().interval(1)
var start = new Date();
var end = new Date(start.valueOf()+3600*1000);
var event = cal.createEventSeries(title, start, end,recur,{description:desc,location:loc});// event will be every year and last 1 hour
}
}
function modifyRecurrence(){
var cal = CalendarApp.getDefaultCalendar()
if (cal) {
var start = new Date();
var end = new Date(start.getTime()+3600*1000*2);
var events = cal.getEvents(new Date("February 16, 2013 08:00:00 PDT"), new Date("February 19, 2013 08:00:00 PDT"))
for(i in events){
if(events[i].getTitle()=='Test Event'){
var recur = CalendarApp.newRecurrence().addDailyRule().interval(1)
var eventId = events[i].getId()
cal.getEventSeriesById(eventId).setRecurrence(recur, start, end);// now events will be lasting 2 hours evey day starting today
}
}
}
}
EDIT following your comment : It works exactly the same for allDayEvents, here is a modified example code :
function createAllDaySerie(){
var cal = CalendarApp.getDefaultCalendar()
if (cal) {
var title = 'All Day Test Event';
var start = new Date();
var desc = 'Created using Google Apps Script';
var loc = 'home';
var recur = CalendarApp.newRecurrence().addYearlyRule().interval(4)
var start = new Date();
var event = cal.createAllDayEventSeries(title, start,recur,{description:desc,location:loc});// event will be every 4 years starting today
}
}
function modifyAllDayRecurrence(){
var cal = CalendarApp.getDefaultCalendar()
if (cal) {
var start = new Date("February 19, 2010 08:00:00 PDT");// here you can choose any date you like that will be the new start date.
var events = cal.getEvents(new Date("February 16, 2013 08:00:00 PDT"), new Date("February 22, 2013 08:00:00 PDT"))
for(i in events){
if(events[i].getTitle()=='All Day Test Event'){
var recur = CalendarApp.newRecurrence().addYearlyRule().interval(1)
var eventId = events[i].getId()
cal.getEventSeriesById(eventId).setRecurrence(recur, start);// now events will occur once a year starting on february 19, 2010 (see screen capture below)
}
}
}
}
And we have the solution for my problem. There is a bug in API documentation. It is written there:
method setRecurrence(startDate, recurrence)
Changes the recurrence rules of this series to a new rule, and to an all day event.
But in fact method setRecurrence() takes those arguments in upside down order:
method setRecurrence(recurrence, startDate)
Many thanks to Serge for help, it was crutial! Unfortunately without reputation 15 I'm not able to give you any points :(