Skip to content

Commit

Permalink
Merge pull request #10 from coderbunker/issue9-spreadsheet-calendar
Browse files Browse the repository at this point in the history
issue #9: combine spreadsheet timesheet and calendar sync code
  • Loading branch information
rngadam authored Apr 29, 2018
2 parents 9a147a7 + 5f094a8 commit 597c709
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 21 deletions.
3 changes: 2 additions & 1 deletion profiles/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function checkUpdate(thumbnailsFolder, existingFiles, filename, presentationLast
// ignored code
const thumbnailLastUpdated = existingFiles[filename].getLastUpdated();
const delta = (presentationLastUpdated - thumbnailLastUpdated);
mustUpdate = delta > (7 * 24 * 60 * 60 * 1000); // one week
//const mustUpdate = delta > (7 * 24 * 60 * 60 * 1000); // one week
const mustUpdate = delta > (60 * 60 * 1000); // one hour
if(mustUpdate) {
Logger.log('Thumbnail already exist but needs to be updated as it is too old: ' + filename + ' delta ' + delta);
trashFile(thumbnailsFolder, existingFiles[filename]);
Expand Down
3 changes: 2 additions & 1 deletion profiles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function getTargetFolderName() {
}

function showThumbnailsSidebar() {
const members = convertSlidesFromPresentation(SlidesApp.openById(getTestPresentationId()));
const presentation = SlidesApp.getActivePresentation();
const members = convertSlidesFromPresentation(presentation);
const sortedMembers = members.sort(function(m1, m2) {
return m1.fullname.localeCompare(m2.fullname);
});
Expand Down
7 changes: 6 additions & 1 deletion timesheet/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ function snapshot(spreadsheet, sheetNames, category) {
const snapshotEndpoint = PropertiesService.getScriptProperties().getProperty('SNAPSHOT_ENDPOINT');
const response = postData(spreadsheet.getId(), exported, snapshotEndpoint);
Logger.log(response);
snapshotAllCalendarOfSpreadsheet(spreadsheet);
return response;
}


// run this within Leads & Opportunities sheet only
function snapshotAll(spreadsheet) {
const sheet = spreadsheet.getSheetByName('Accounts');
if(!sheet) {
Logger.log('No sheet Accounts found, is this a the Leads & Opportunities sheet?');
return;
}
const mapping = getHeaderMapping(sheet);
const accounts = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getDisplayValues();
const entries = convertToEntries(accounts, mapping);
Expand Down Expand Up @@ -36,7 +42,6 @@ function snapshotAll(spreadsheet) {
});
}


// needs work, won't activate
/*
function change(e) {
Expand Down
61 changes: 61 additions & 0 deletions timesheet/calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function snapshotAllCalendarOfSpreadsheetTrigger() {
snapshotAllCalendarOfSpreadsheet(getActiveSpreadsheet());
}

function snapshotAllCalendarOfSpreadsheet(spreadsheet) {
const sheet = spreadsheet.getSheetByName('Balance');
const mapping = getHeaderMapping(sheet);
const accounts = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getDisplayValues();
const entries = convertToEntries(accounts, mapping);
const responses = entries.map(function(entry) {
Logger.log(entry.resource);
Logger.log(entry.fullname);
if(!entry.calendar) {
Logger.log('No calendar associated to %s', entry.resource);
return null;
}
const calendar = CalendarApp.getCalendarById(entry.calendarid);
if(!calendar) {
Logger.log('not a valid id: %s', entry.calendarid);
return null;
}
return snapshotCalendar(calendar);
});
Logger.log(responses);
}

function snapshotCalendar(calendar) {
const exportData = exportCalendar(calendar);
const snapshotEndpoint = PropertiesService.getScriptProperties().getProperty('SNAPSHOT_ENDPOINT');
exportData['category'] = 'Timesheet';
const result = postData(calendar.getId(), exportData, snapshotEndpoint);
Logger.log(result);
return result;
}

function exportCalendar(calendar) {
const id = calendar.getId();
const name = calendar.getName();
const timezone = calendar.getTimeZone();

const obj = {
id: id,
name: name,
timezone: timezone,
apptype: 'Calendar',
};

const events = calendar.getEvents(new Date('2018-01-01'), new Date());
const eventsArray = events.map(function(e) {
return {
name: e.getTitle(),
startTime: e.getStartTime(),
endTime: e.getEndTime(),
creators: e.getCreators()
}
});

obj['events'] = eventsArray;

return obj;
}
6 changes: 5 additions & 1 deletion timesheet/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function export(spreadsheet, sheetNames, category) {

sheetNames.forEach(function(name) {
var sheet = spreadsheet.getSheetByName(name);
obj['sheets'][name] = { data: exportSheet(spreadsheet, sheet) }
if(sheet) {
obj['sheets'][name] = { data: exportSheet(spreadsheet, sheet) }
} else {
Logger.log('No sheet %s in %s', name, spreadsheet.getId());
}
});

return obj;
Expand Down
21 changes: 4 additions & 17 deletions timesheet/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,21 @@ function onOpen() {
const orgName = PropertiesService.getScriptProperties().getProperty('ORG_NAME');
const menu = SpreadsheetApp.getUi().createMenu(orgName)
if(getActiveSpreadsheet().getSheetByName('Accounts')) {
menu.addItem('Snapshot All Timesheets', 'snapshotAllTrigger')
menu.addItem('Snapshot Accounts', 'snapshotAccountsTrigger')
menu.addItem('Snapshot Accounts & All Timesheets', 'snapshotAllTrigger');
}
if(getActiveSpreadsheet().getSheetByName('Timesheet')) {
menu.addItem('Snapshot Timesheet', 'snapshotTrigger')
menu.addItem('Snapshot Timesheet and associated calendars', 'snapshotTrigger');
}
menu.addToUi();
}

function snapshotTrigger() {
const spreadsheet = getActiveSpreadsheet();
snapshot(spreadsheet);
snapshot(spreadsheet, ['Timesheet', 'Balance'], 'Timesheet');
}

function snapshotAllTrigger() {
const spreadsheet = SpreadsheetApp.openByUrl(PropertiesService.getScriptProperties().getProperty('DEFAULT_ACCOUNTS_URL'));
snapshotAll(spreadsheet);
}

function snapshotAccountsTrigger() {
const spreadsheet = SpreadsheetApp.openByUrl(PropertiesService.getScriptProperties().getProperty('DEFAULT_ACCOUNTS_URL'));
snapshot(spreadsheet, ['Accounts'], 'Leads & Opportunities')
}

// TODO: doesn't trigger?
/*
function onEdit(e) {
Logger.log('onEdit called ' + JSON.stringify(e))
change(e);
}
*/
}

0 comments on commit 597c709

Please sign in to comment.