Last active
April 12, 2021 20:48
-
-
Save erickoledadevrel/7c882e2808112c381f9496eb0c21f6c1 to your computer and use it in GitHub Desktop.
Walkstation booking with Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @license | |
* Copyright 2019 Google LLC | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/* | |
* This script requires that you enable the Calendar Advanced Service: | |
* https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services | |
*/ | |
var CALENDAR_ID = '...'; | |
var LENGTHS = [60, 45, 30]; | |
function doGet() { | |
var result = tryBook(); | |
var template = HtmlService.createTemplateFromFile('Page'); | |
template.result = result; | |
return template.evaluate().setTitle('Walkstation'); | |
} | |
function tryBook() { | |
for (var i = 0; i < LENGTHS.length; i++) { | |
var minutes = LENGTHS[i]; | |
// Determine start and end time. | |
var start = new Date(); | |
start.setMinutes(Math.round(start.getMinutes() / 15.0) * 15); | |
var end = new Date(start.getTime()); | |
end.setMinutes(end.getMinutes() + minutes); | |
if (isFree(start, end)) { | |
book(start, end); | |
return { | |
minutes: minutes, | |
start: Utilities.formatDate(start, Session.getScriptTimeZone(), 'HH:mm a'), | |
end: Utilities.formatDate(end, Session.getScriptTimeZone(), 'HH:mm a') | |
}; | |
} | |
} | |
return null; | |
} | |
function isFree(start, end) { | |
var result = Calendar.Freebusy.query({ | |
timeMin: start.toISOString(), | |
timeMax: end.toISOString(), | |
timeZone: Session.getScriptTimeZone(), | |
items: [{ | |
id: CALENDAR_ID | |
}] | |
}); | |
return result.calendars[CALENDAR_ID].busy.length == 0; | |
} | |
function book(start, end) { | |
var event = { | |
summary: 'Walk', | |
start: { | |
dateTime: start.toISOString() | |
}, | |
end: { | |
dateTime: end.toISOString() | |
}, | |
attendees: [{ | |
email: CALENDAR_ID, | |
resource: true | |
}] | |
}; | |
Calendar.Events.insert(event, 'primary'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Copyright 2019 Google LLC | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
https://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
</head> | |
<body> | |
<h1>Walkstation Booking</h1> | |
<? if (result != null) { ?> | |
Success! You've booked the walkstation for <?= result.minutes ?> minutes, | |
from <?= result.start ?> to <?= result.end ?>. | |
<? } else { ?> | |
Unfortunately the walkstation wasn't available. | |
<? } ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment