I have pasted the code below. To explain what the code is intended to do is block out a conference room so that it can not be reserved for use, it is a small "huddle room" that will be blocked out and only available to reserve a week in advance.
Anyway here is the problem I am encountering with the code below. If I run the code starting from Jan 1. The code will run and then part way through March stops creating events, if this happens exactly at the beginning of a month it wouldn't be an issue as I could either start easily from that point again, or assume the month is spelled wrong. But it creates reservations though march 18th. Also when I restarted this and set it to create blocked reservations starting at the beginning of April it got though December 8th.
My first guess is that I need to deal with reformatting the code to handle months without 31 days, but I assumed that those none existent days would just throw an error and the lop would continue, and it did get through February which is a short month.
Just thinking maybe someone who has more experience with Google Scripting may have an idea or see a flaw in something I am doing. Thanks for any help
function blockReservations(){
var roomcalendar = CalendarApp.getCalendarById('company.com_12458546525839392d898932@resource.calendar.google.com');
//for(var z=2014;z<=2020;z++){
//var year = z;
var year = '2014'; //This Line May be used in place of the above for loop to specify a specific year
for(var x=4;x<=12;x++)
{
if(x==1) var month = 'January';
else if(x==2) var month = 'February';
else if(x==3) var month = 'March';
else if(x==4) var month = 'April';
else if(x==5) var month = 'May';
else if(x==6) var month = 'June';
else if(x==7) var month = 'July';
else if(x==8) var month = 'August';
else if(x==9) var month = 'September';
else if(x==10) var month = 'October';
else if(x==11) var month = 'November';
else if(x==12) var month = 'December';
else month = 'null';
//var month = 'July'; //This Line May be used in place of the above for loop to specify a specific year
for(var y=1;y<=31;y++)
{
var date = y;
var startDateString = month + ' ' + date + ', ' + year +' 00:00:00';
var endDateString = month + ' ' + date + ', ' + year +' 24:00:00';
var event = roomcalendar.createEvent('Time Blocked', new Date(startDateString), new Date(endDateString));
}
}
// }
}
You don't mention any error messages, but I would expect that you're receiving a notification email reporting that the script was killed for running too long. Creating events in a loop can take lots of processing time.
I propose a different approach. Instead of creating daily events to reserve the room, why not create a recurring all-day event, starting a number of days in the future. Then each day, this reservation can be updated (by a timed trigger function) to revise the recurrence rule to start one day later.
/**
* Create or update a block reservation for a conference room,
* starting 'blockFrom' days from today.
*/
function updateBlockReservation() {
// Get Calendar
var calName = 'Huddle Room';
var cal = CalendarApp.getCalendarsByName(calName)[0];
var title = 'Reserved'; // Reserved events will have this title
var blockFrom = 7; // Days from now until room is blocked
var today = new Date(); // Today's date, ...
today.setHours(0,0,0,0); // at midnight.
var startDate // Daily block reservation starts here
= new Date(today.getTime() + (blockFrom * 24 * 60 * 60 * 1000));
var endTime = new Date(startDate.getTime() + (24 * 60 * 60 * 1000) - 1);
var recurrence = CalendarApp.newRecurrence().addDailyRule();
// Look for existing block reservation
var series = cal.getEvents(startDate, endTime, {search:title});
if (series.length == 0) {
// No block reservation found - create one.
var reserved = cal.createAllDayEventSeries(title, startDate, recurrence);
}
else {
// Block reservation exists - update the recurrence to start later.
reserved = series[0].getEventSeries();
reserved.setRecurrence(recurrence, startDate);
}
debugger; // Pause if running in debugger
}
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.