I have a nice function that shows all events on my calendar for this month but I can't figure out how to change that to this week (monday to sunday).
I can change the list by changing date to today's date and adding days for example. I tried some things with Utilities.formatDate but this gives me weeknumbers, no dates. I have no clue how to get the current week.
function listEvents2() {
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var events2 = CalendarApp.getDefaultCalendar().getEvents(firstDay, lastDay);
var data2 = [];
data2.push("Deze maand: ");
if (events2 && events2.length > 0) {
for (i = 0; i < events2.length; i++) {
data2.push(events2[i].getTitle()+' : '+Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"dd MMMM HH:mm")+' - '+Utilities.formatDate(events2[i].getEndTime(),Session.getScriptTimeZone(),"HH:mm"))
}
return data2;
}
else {
return ['No events found','',''];
}
}
I would like some tips how to achieve this.
Edit: Maybe I need to explain more. This function is used for a personal weekplanner dashboard with HtmlService. There is another function that gives a list of today's events.
Edit 2: Here is my working code:
function email() {
var date = new Date();
var day = date.getDay();
var firstDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()-day+1);
var lastDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()-day+8);
var events2 = CalendarApp.getCalendarById(CalendarID).getEvents(firstDay, lastDay);
var data2 = [];
data2.push("<b>Title</b>");
if (events2 && events2.length > 0) {
for (i = 0; i < events2.length; i++) {
data2.push(Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"EEEE dd/MM")+ ' ' + Utilities.formatDate(events2[i].getStartTime(),Session.getScriptTimeZone(),"HH:mm") +'<br/>'+events2[i].getTitle())
}
return data2;
} else {
return ['Nothing to do'];
}
}
I decided to split the lists per calendar to have it more organized. But both answers below work.
There are still two things I need to figure out. First one is how I can get
Session.getScriptTimeZone(),"EEEE dd/MM")
to show EEEE in my native language instead of English.
The other one is that the function gives a perfect list of things to do but it skips empty days. I would like to display the empty days as the dashboard should function like a calendar view planner.
Thanks for the help so far!