| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>Projects</title>
- <meta charset='utf-8' />
- </head>
- <body>
- <p>Projects</p>
- <!--Add buttons to initiate auth sequence and sign out-->
- <button id="authorize-button" style="display: none;">Authorize</button>
- <button id="signout-button" style="display: none;">Sign Out</button>
-
- <input type="button" id="submit" onclick="listUpcomingEvents()" />
- <pre>
- <table border="1" id="content"></table>
- </pre>
- <script type="text/javascript">
- // Client ID and API key from the Developer Console
- var CLIENT_ID = '72444000263-arb8mvd5ko1i4mb7cel04bbuiipej6lc.apps.googleusercontent.com';
- var API_KEY = 'AIzaSyBfM8IAHkv5JRlo3nCxWpp5b3Zw2XnWXZY';
- // Array of API discovery doc URLs for APIs used by the quickstart
- var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"];
- // Authorization scopes required by the API; multiple scopes can be
- // included, separated by spaces.
- var SCOPES = "https://www.googleapis.com/auth/calendar.readonly";
- var authorizeButton = document.getElementById('authorize-button');
- var signoutButton = document.getElementById('signout-button');
- /**
- * On load, called to load the auth2 library and API client library.
- */
- function handleClientLoad() {
- gapi.load('client:auth2', initClient);
- }
- /**
- * Initializes the API client library and sets up sign-in state
- * listeners.
- */
- function initClient() {
- gapi.client.init({
- apiKey: API_KEY,
- clientId: CLIENT_ID,
- discoveryDocs: DISCOVERY_DOCS,
- scope: SCOPES
- }).then(function() {
- // Listen for sign-in state changes.
- gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
- // Handle the initial sign-in state.
- updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
- authorizeButton.onclick = handleAuthClick;
- signoutButton.onclick = handleSignoutClick;
- });
- }
- /**
- * Called when the signed in status changes, to update the UI
- * appropriately. After a sign-in, the API is called.
- */
- function updateSigninStatus(isSignedIn) {
- if (isSignedIn) {
- authorizeButton.style.display = 'none';
- signoutButton.style.display = 'block';
- listUpcomingEvents();
- } else {
- authorizeButton.style.display = 'block';
- signoutButton.style.display = 'none';
- }
- }
- /**
- * Sign in the user upon button click.
- */
- function handleAuthClick(event) {
- gapi.auth2.getAuthInstance().signIn();
- }
- /**
- * Sign out the user upon button click.
- */
- function handleSignoutClick(event) {
- gapi.auth2.getAuthInstance().signOut();
- }
- /**
- * Append a pre element to the body containing the given message
- * as its text node. Used to display the results of the API call.
- *
- * @param {string} message Text to be placed in pre element.
- */
- function appendPre(message) {
- var pre = document.getElementById('content');
- var dateOnly = new Date(message.start.dateTime);
- var tableRow = document.createElement("tr");
- tableRow.appendChild(createCell(message.summary));
- tableRow.appendChild(createCell(dateOnly.toLocaleDateString("is-IS")));
- tableRow.appendChild(createCell(dateOnly.toLocaleTimeString("is-IS", {
- hour: '2-digit',
- minute: '2-digit'
- })));
- tableRow.appendChild(createCell(message.end.dateTime));
- var diff = new Date(message.end.dateTime) - new Date(message.start.dateTime);
- diff = diff / 60000 / 60;
- tableRow.appendChild(createCell(diff));
- //dur = dur / 60000 / 60;
- pre.appendChild(tableRow);
- }
- function clearArray() {
- document.getElementById('content').innerHTML = "";
- }
- function appendArray(tdArray) {
- console.dir(tdArray);
- var pre = document.getElementById('content');
- var tableRow = document.createElement("tr");
- for (aa = 0; aa < tdArray.length; aa++) {
- var td = createCell(tdArray[aa]);
- tableRow.appendChild(td);
- }
- pre.appendChild(tableRow);
- }
- /*
- function addNewDay(newDate) {
- }
- function appendText(message) {
- var pre = document.getElementById('content');
- var tableRow = document.createElement("tr");
- var diff = new Date(message.end.dateTime) - new Date(message.start.dateTime);
- tableRow.appendChild(createCell(diff));
- //dur = dur / 60000 / 60;
- pre.appendChild(tableRow);
- }
- */
- function createCell(message) {
- var RowCell = document.createElement("td");
- var WorkItem = document.createTextNode(message);
- RowCell.appendChild(WorkItem);
- return RowCell;
- }
- function convertToStruct(inEvent) {
- try {
- var timeoptions = {
- hour: '2-digit',
- minute: '2-digit'
- };
- var startDate = new Date(inEvent.start.dateTime)
- var event = {};
- if (inEvent.summary.split(" ").length > 1) {
- var event = {
- startDate: '' + startDate.getDate() + '.' + (startDate.getMonth() + 1) + '.' + startDate.getFullYear(),
- endDate: new Date(inEvent.end.dateTime).toLocaleDateString("is-IS"),
- startTime: new Date(inEvent.start.dateTime).toLocaleTimeString("is-IS", timeoptions),
- endTime: new Date(inEvent.end.dateTime).toLocaleTimeString("is-IS", timeoptions),
- diffTime: 0,
- task: inEvent.summary.split(" ")[1].slice(1, inEvent.summary.split(" ")[1].length),
- client: inEvent.summary.substring(0, inEvent.summary.indexOf(' ', 1)),
- duration: 0
- };
- }
- var diffDates = new Date(inEvent.end.dateTime) - new Date(inEvent.start.dateTime);
- event.duration = diffDates / 60000 / 60;
- event.diffTime = new Date(diffDates).toLocaleTimeString("is-IS", timeoptions);
- return event;
- } catch (e) {
- console.error(e);
- }
- /*
- if (!when) {
- when = event.start.date;
- }
- if (!end) {
- end = event.end.date;
- }
- */
- }
- /**
- * Print the summary and start datetime/date of the next ten events in
- * the authorized user's calendar. If no events are found an
- * appropriate message is printed.
- */
- function listUpcomingEvents() {
- var date = new Date();
- var firstDay = new Date(2017, 1 , 1);
- var lastDay = new Date(date.getFullYear(), date.getMonth(), 1);
-
- console.log(firstDay);
- console.log(lastDay);
-
- clearArray();
- gapi.client.calendar.events.list({
- 'calendarId': '[email protected]',
- 'timeMin': firstDay.toISOString(),
- 'timeMax': lastDay.toISOString(),
- 'showDeleted': false,
- 'singleEvents': true,
- 'maxResults': 5400,
- 'orderBy': 'startTime'
- }).then(function(response) {
- var events = response.result.items;
-
- const result = Array.from(new Set(events.client));
- console.log(result)
- // appendPre('Last month:');
- var sumHrs = 0;
- var interimsums = [];
- if (events.length > 0) {
- var previousDate = null;
- for (i = 0; i < events.length; i++) {
- var event = convertToStruct(events[i]);
-
- if (previousDate == null || previousDate != event.startDate) {
- previousDate = event.startDate;
- appendArray([event.startDate]);
- }
- appendArray([event.client, event.startTime, event.endTime, event.diffTime, event.duration, event.task]);
- if (interimsums[event.task] == null) {
- interimsums[event.task] = 0;
- }
- interimsums[event.task] += event.duration;
- sumHrs += event.duration;
- //if (i == 7) break;
- //appendPre(event.summary + ' (' + when + ') - (' + end + ') : ' + (dur))
-
- }
- console.dir(interimsums);
- //appendPre('Sum:' + sumHrs);
- } else {
- //appendPre('No upcoming events found.');
- }
- });
- }
- </script>
- <script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};handleClientLoad()" onreadystatechange="if (this.readyState === 'complete') this.onload()">
- </script>
- </body>
- </html>
|