Skip to content

Instantly share code, notes, and snippets.

@Edo78
Last active February 11, 2018 15:06
Show Gist options
  • Save Edo78/8bf50b306d6cc066a9bbf083dbc5673b to your computer and use it in GitHub Desktop.
Save Edo78/8bf50b306d6cc066a9bbf083dbc5673b to your computer and use it in GitHub Desktop.
/**
* Retrieve the birthdays in a range and return them in two lists, one for today's and one for the rest of the period
*
* @name getBirthdays
* @function
* @param {number} days - number of days to add to today to get the date range (optional)
* @returns {object} object with following properties 'today' with today's birthdays 'remaining' with the remaingin birthdays for the date range
*/
function getBirthdays(days) {
var result = {
'today': []
};
var _tmpNow = new Date();
// Preparo un oggetto per avere a disposizione le parti della data odierna che mi serviranno
var now = {
'year': _tmpNow.getFullYear(),
'month': _tmpNow.getMonth(),
'day': _tmpNow.getDate()
};
var start = new Date(now.year, now.month, now.day, 0, 0, 0, 0);
var end = new Date(now.year, now.month, now.day + days, 23, 59, 59, 999);
// Recupero i compleanni tra oggi e "days" giorni da oggi
var contactsBirthdays=CalendarApp
.getCalendarById('#[email protected]')
.getEvents(start, end);
// Per ogni compleanno controllo quanti siano in data odierna e li sposto
// in result.today
contactsBirthdays.forEach(function(bday) {
if(bday.getAllDayStartDate().getDate() === now.day){
result.today.push(contactsBirthdays.shift())
}
});
// Assegno i restanti compleanni a result.remaining
result.remaining = contactsBirthdays;
return result;
}
/**
* Send an HTML email with a list of birthdays
*
* @name sendTodaysBirthday
* @function
*/
function sendTodaysBirthday() {
// Creo un template HTML a partire dal file 'Email.html'
var emailTemplate = HtmlService
.createTemplateFromFile('Email');
// assegno alla proprità 'birthdays' quanto viene restituito dalla funzione getBirthdays(7)
emailTemplate.birthdays = getBirthdays(7);
// Genero l'HTML a partire dal template e i dati che gli ho precedentemente passato
var email = emailTemplate.evaluate();
// Invio una mail a me stesso con oggetto 'Promemoria' e come corpo l'HTML precedentemente generato
MailApp.sendEmail(Session.getActiveUser().getEmail(), "Promemoria", "", {"htmlBody" : email.getContent()});
}
/**
* Include the partial HTML of a specific file
*
* @name include
* @function
* @param {string} filename - name of the file to get the HTML from
* @returns {string} partial HTML
*/
function include(filename) {
return HtmlService
.createHtmlOutputFromFile(filename)
.getContent();
}
/**
* Format nicely the date
*
* @name niceDate
* @function
* @param {date} date - the date object to format
* @returns {string} nicely formatted date
*/
function niceDate(date) {
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
return date.toLocaleDateString('it-IT', options);
}
<!DOCTYPE html>
<!--
Questo template riceve i dati restituiti dalla funzionalità getBirthdays
quindi un oggetto
{
'taday': CalendarEvent[],
'remaining': CalendarEvent[]
}
-->
<html>
<head>
<base target="_top">
<!-- Utilizzo la funzione 'include' per incorporare i CSS dal file 'Stylesheet.html' -->
<?!= include('Stylesheet'); ?>
</head>
<body>
<div class="main">
<h1>I Prossimi Compleanni</h1>
<div id="today">
<? if(birthdays.today.length === 0) { ?>
Non ci sono compleanni oggi
<? } else {
var bPrefix = (birthdays.today.length === 1) ? 'c\'è' : 'ci sono';
var bSuffix = (birthdays.today.length === 1) ? 'evento' : 'eventi'; ?>
Oggi <?= bPrefix ?> <?= birthdays.today.length ?> <?= bSuffix ?>
<? birthdays.today.forEach(function(bday) { ?>
<div><?= bday.getDescription() ?></div>
<? }); ?>
<? } ?>
</div>
<div id="remaining">
<? birthdays.remaining.forEach(function(bday) { ?>
<div><?= niceDate(bday.getStartTime()) ?> | <?= bday.getDescription() ?></div>
<? }); ?>
</div>
</div>
</body>
</html>
<style>
@media screen and (min-width: 500px) {
.main {
width: 90%;
}
}
.main {
margin: 0 auto;
}
#today {
padding: 1% 1% 3%;
background-color: lightblue;
}
#remaining {
padding: 1%;
background-color: lightgrey;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment