Guest User

Untitled

a guest
Feb 23rd, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. /**
  2. * Retrieve the birthdays in a range and return them in two lists, one for today's and one for the rest of the period
  3. *
  4. * @name getBirthdays
  5. * @function
  6. * @param {number} days - number of days to add to today to get the date range (optional)
  7. * @returns {object} object with following properties 'today' with today's birthdays 'remaining' with the remaingin birthdays for the date range
  8. */
  9. function getBirthdays(days) {
  10. var result = {
  11. 'today': []
  12. };
  13. var _tmpNow = new Date();
  14. // Preparo un oggetto per avere a disposizione le parti della data odierna che mi serviranno
  15. var now = {
  16. 'year': _tmpNow.getFullYear(),
  17. 'month': _tmpNow.getMonth(),
  18. 'day': _tmpNow.getDate()
  19. };
  20. var start = new Date(now.year, now.month, now.day, 0, 0, 0, 0);
  21. var end = new Date(now.year, now.month, now.day + days, 23, 59, 59, 999);
  22. // Recupero i compleanni tra oggi e "days" giorni da oggi
  23. var contactsBirthdays=CalendarApp
  24. .getCalendarById('#contacts@group.v.calendar.google.com')
  25. .getEvents(start, end);
  26. // Per ogni compleanno controllo quanti siano in data odierna e li sposto
  27. // in result.today
  28. contactsBirthdays.forEach(function(bday) {
  29. if(bday.getAllDayStartDate().getDate() === now.day){
  30. result.today.push(contactsBirthdays.shift())
  31. }
  32. });
  33. // Assegno i restanti compleanni a result.remaining
  34. result.remaining = contactsBirthdays;
  35. return result;
  36. }
  37.  
  38. /**
  39. * Send an HTML email with a list of birthdays
  40. *
  41. * @name sendTodaysBirthday
  42. * @function
  43. */
  44. function sendTodaysBirthday() {
  45. // Creo un template HTML a partire dal file 'Email.html'
  46. var emailTemplate = HtmlService
  47. .createTemplateFromFile('Email');
  48. // assegno alla proprità 'birthdays' quanto viene restituito dalla funzione getBirthdays(7)
  49. emailTemplate.birthdays = getBirthdays(7);
  50. // Genero l'HTML a partire dal template e i dati che gli ho precedentemente passato
  51. var email = emailTemplate.evaluate();
  52. // Invio una mail a me stesso con oggetto 'Promemoria' e come corpo l'HTML precedentemente generato
  53. MailApp.sendEmail(Session.getActiveUser().getEmail(), "Promemoria", "", {"htmlBody" : email.getContent()});
  54. }
  55.  
  56. /**
  57. * Include the partial HTML of a specific file
  58. *
  59. * @name include
  60. * @function
  61. * @param {string} filename - name of the file to get the HTML from
  62. * @returns {string} partial HTML
  63. */
  64. function include(filename) {
  65. return HtmlService
  66. .createHtmlOutputFromFile(filename)
  67. .getContent();
  68. }
  69.  
  70. /**
  71. * Format nicely the date
  72. *
  73. * @name niceDate
  74. * @function
  75. * @param {date} date - the date object to format
  76. * @returns {string} nicely formatted date
  77. */
  78. function niceDate(date) {
  79. var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
  80. return date.toLocaleDateString('it-IT', options);
  81. }
Add Comment
Please, Sign In to add comment