Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active October 5, 2019 01:52
Show Gist options
  • Select an option

  • Save jirawatee/69a413e39b309cd2df605021ff03585d to your computer and use it in GitHub Desktop.

Select an option

Save jirawatee/69a413e39b309cd2df605021ff03585d to your computer and use it in GitHub Desktop.
Creating Calendar Event script
function createCalendarEvent(dateTimeStart, dateTimeEnd, appointment_type) {
return new Promise((resolve, reject) => {
calendar.events.list({
auth: serviceAccountAuth, // List events for time period
calendarId: calendarId,
timeMin: dateTimeStart.toISOString(),
timeMax: dateTimeEnd.toISOString()
}, (err, calendarResponse) => {
// Check if there is a event already on the Calendar
if (err || calendarResponse.data.items.length > 0) {
reject(err || new Error('Requested time conflicts with another appointment'));
} else {
// Create event for the requested time period
calendar.events.insert({
auth: serviceAccountAuth,
calendarId: calendarId,
resource: {
summary: appointment_type + ' Appointment', description: appointment_type,
start: { dateTime: dateTimeStart },
end: { dateTime: dateTimeEnd }
}
}, (err, event) => {
if (err !== null) {
reject(err);
} else {
resolve(event);
}
});
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment