|
|
|
|
|
// This builds the body of the email |
|
|
|
function test_buildNoticesForDate() { |
|
buildNoticesForDate(today); |
|
} |
|
|
|
function buildNoticesForDate(targetDate) { |
|
|
|
// Grab the data from the sheet |
|
|
|
var values = getSpreadsheetValues(); |
|
var obj, row, docId, before, after; |
|
|
|
// Make the "body" a string that contains the message we want to send |
|
|
|
var body = ""; |
|
var incidents = 0; // tally of how many incidents |
|
|
|
gl && Logger.log("Today is %s", today.format(dateFormat)); |
|
|
|
// forEach function loops through after filtering out bad stuff |
|
|
|
notices = {}; |
|
|
|
// index will definitely be the row index |
|
values.forEach(function (row, index, arr) { |
|
if (row[0] === "") { |
|
return; |
|
} |
|
obj = new Object(); |
|
obj.timestamp = moment(row[0]); |
|
obj.username = row[indexUsername]; |
|
obj.user = ContactsApp.getContact(obj.username); |
|
obj.by = '<br />(' + obj.user.getFullName() + ')'; |
|
obj.startDate = moment(row[indexStartDate]); |
|
obj.endDate = row[indexEndDate]; |
|
if (!obj.endDate) { |
|
obj.endDate = obj.startDate; |
|
} else { |
|
obj.endDate = moment(obj.endDate); |
|
} |
|
obj.content = row[indexContent]; |
|
obj.section = row[indexSection]; |
|
obj.embededUrl = row[indexEmbedded]; |
|
|
|
// Calculate the priority by determining how many days it has been published |
|
// Increase the priority if it is a priority username, that way they appear higher up |
|
obj.priority = targetDate.diff(obj.startDate, 'days') * 10; |
|
if (priorityUsernames.indexOf(obj.username)) { |
|
obj.priority += 1; |
|
} |
|
|
|
// Derive an internal index of each notice so we can sort by it and can figure out what order they appear on the page |
|
// We'll use that when outputting the attachments, below |
|
obj.index = parseInt(getSectionOrder(obj.section).toString() + obj.priority.toString() + index.toString()); |
|
|
|
if (obj.embededUrl) { |
|
docId = getIdFromUrl_(obj.embededUrl); |
|
obj.embededDoc = DocumentApp.openById(docId); |
|
obj.embededText = ' <a href="#embedded' + obj.index.toString() + '">[Attachment]</a> '; |
|
} else { |
|
obj.embededDoc = undefined; |
|
obj.embededText = ""; |
|
} |
|
|
|
obj.fullContent = htmlify_(obj.content) + obj.embededText + obj.by; |
|
|
|
obj.htmlContent = '<li style="padding-bottom:10px;"><a name="notice' + obj.index.toString() + '"></a>'; |
|
obj.htmlContent += obj.fullContent + '</li>'; |
|
|
|
// Ensure that today is a weekday |
|
//if (timestamp.weekday() == 0 || timestamp.weekday() > 5) { |
|
// return |
|
//} |
|
|
|
// Another filter to only collate items that fall within our desired range |
|
|
|
gl && Logger.log("End date is %s", obj.endDate.format(dateFormat)); |
|
|
|
if (obj.endDate.isBefore(targetDate, 'day')) { |
|
|
|
// We have already passed it, so mark it as such and move on.... |
|
sheet.getRange(index + 2, indexPast + 1).setValue('Yes'); |
|
gl && Logger.log("Set as past"); |
|
|
|
} else { |
|
sheet.getRange(index + 2, indexPast + 1).setValue('No'); |
|
before = obj.startDate.isBefore(targetDate, 'day') || obj.startDate.isSame(targetDate, 'day') |
|
after = obj.endDate.isAfter(targetDate, 'day') || obj.endDate.isSame(targetDate, 'day'); |
|
|
|
if ( before && after ) { |
|
// Process it |
|
if (!(obj.section in notices)) { |
|
notices[obj.section] = new Object(); |
|
notices[obj.section].title = obj.section; |
|
notices[obj.section].order = getSectionOrder(obj.section); |
|
Logger.log("Section %s order %s", obj.section, notices[obj.section].order); |
|
notices[obj.section].items = []; |
|
notices[obj.section].html = ""; |
|
} |
|
notices[obj.section].items.push(obj); |
|
|
|
if (obj.embededDoc !== undefined) { |
|
if (!('Attachments' in notices)) { |
|
notices['Attachments'] = []; |
|
} |
|
notices['Attachments'].push(obj); |
|
} |
|
} |
|
} |
|
}); |
|
|
|
notices.html = ""; |
|
_.sortBy(notices, 'order').forEach(function (notice, index, arr) { |
|
if (notice.title === "Attachments") { |
|
// Don't do embedded docs the normal way, because we'll handle it differently |
|
return; |
|
} |
|
|
|
if (notice.items === undefined) { |
|
return; |
|
} |
|
notice.html += '<div><strong>' + notice.title + '</strong></div>'; |
|
notice.html += '<ul class="unilist">'; |
|
_.sortBy(notice.items, 'priority').forEach(function (item, index, obj) { |
|
notice.html += item.htmlContent; |
|
}); |
|
notice.html += '</ul>'; |
|
|
|
notices.html += notice.html; |
|
}); |
|
|
|
// Now handle the attachments: |
|
if (notices["Attachments"]) { |
|
notices.html += '<br /><div><strong>Attachments</strong></div><br />'; |
|
|
|
// Sort by index, so that attachments appear in the same order as given on the page |
|
_.sortBy(notices['Attachments'], 'index').forEach(function (item, index, obj) { |
|
notices.html += '<a name="embedded' + item.index.toString() + '"></a>' + htmlify_(item.content) + ' [<a href="#notice'+ item.index.toString() + '">Back to notice</a>]'; |
|
notices.html += '<div><b><div><div class="sites-embed-align-left-wrapping-off"><div class="sites-embed-border-on sites-embed sites-embed-full-width" style="width:100%;"><h4 class="sites-embed-title">' + item.embededDoc.getName() + '</h4><div class="sites-embed-object-title" style="display:none;">' + item.embededDoc.getName() + '</div><div class="sites-embed-content sites-embed-type-writely"><iframe src="https://docs.google.com/document/preview?hgd=1&id=' + item.embededDoc.getId() + '" width="100%" height="400" title="' + item.embededDoc.getName() + '" frameborder="0"></iframe></div><div class="sites-embed-footer"><div class="sites-embed-footer-icon sites-writely-icon"> </div><a href="https://docs.google.com/document/edit?hgd=1&id=' + item.embededDoc.getId() + '" target="_blank">Open <i>'+ item.embededDoc.getName() + '</i></a></div></div></div></div><br></b></div>'; |
|
}); |
|
notices.html += ''; |
|
} |
|
|
|
gl && Logger.log(notices.html); |
|
return notices; |
|
} |
|
|
|
function emailAgents() { |
|
// Determine who should get the email, and actually send it |
|
// No duplicate sends |
|
|
|
var agents = []; |
|
|
|
var notices = buildNoticesForDate(today); |
|
|
|
//spreadsheet.getViewers().forEach(function (user, index, obj) { |
|
// Add this user's email only if not already there |
|
if (agents.indexOf(user.getEmail()) <= -1) { |
|
agents.push(user.getEmail()); |
|
} |
|
|
|
spreadsheet.getEditors().forEach(function (user, index, obj) { |
|
// Add this user's email only if not already there |
|
if (agents.indexOf(user.getEmail()) <= -1) { |
|
agents.push(user.getEmail()); |
|
} |
|
}); |
|
|
|
// For debugging, define agents here and test |
|
// agents = ['[email protected]']; |
|
|
|
// Now email them out to everyone who is a viewer or editor |
|
agents.forEach(function (value, _, obj) { |
|
// TODO: send notices.html, but as html |
|
//MailApp.sendEmail(value, 'Notices for: ' + now.format(dateFormat), body); |
|
}); |
|
} |
|
|
|
function updateSite(title, notices) { |
|
var noticesPage = site.getChildByName(title); |
|
if (noticesPage === null) { |
|
gl && Logger.log("Creating site %s", title.shortNameToLongName()); |
|
site.createWebPage(title.shortNameToLongName(), title, notices.html) |
|
} else { |
|
noticesPage.setHtmlContent(notices.html); |
|
} |
|
} |
|
|
|
function onDailyTrigger(e) { |
|
var notices = buildNoticesForDate(tomorrow); |
|
updateSite('staff-notices-' + tomorrow.format(dateFormat), notices); |
|
} |
|
|
|
function onSubmit(e) { |
|
var notices = buildNoticesForDate(today); |
|
updateSite('staff-notices', notices); |
|
} |