Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting google calendar as PDF with events using GAS

I have extracted the Google Calendar print request parameters so that they can be used to export or UrlFetchApp them using Apps Script

The parameters are listed below, When I access this URL via the signed in browser it displays events

But when I run the same via apps script, I get a calendar PDF without events.

How is the authorization is passed implicitly or explicitly? I had just passed Bearer

https://calendar.google.com/calendar/printable?psd=false
&ctz=Asia%2FCalcutta
&dates=20170507%2F20170520
&mode=month
&pbw=false
&pfs=NORMAL
&pft=pdf
&pgsz=a4
&pli=1
&po=PORTRAIT
&pred=20170520
&prsd=20170507
&psa=false
&psdec=false
&pset=false
&psr=false
&rand=-6094307348214370018#
&src=Y29mbS5lZHUuaW5fdWxoOHIza2Fyc2QyYnVzaDAzNG10OTIzbjRAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ

In my Apps script,

//url specified as above

    var response = UrlFetchApp.fetch(url, {
    headers: {
        'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    },
    'followRedirects': true

});
like image 533
Code Guy Avatar asked May 27 '17 14:05

Code Guy


1 Answers

I always found that the printable view cut off the event names, no matter what I did to adjust the view and make the boxes as big as possible. I ended up writing a script that generates a print friendly calendar using the HTML service. I have it published as a web app, and visitors to the site can use their browser's print tool to print it, but I also call it in a different script that emails a pdf of it to a mailing list every month. The Google Apps Script:

var hasFourthRow=false;
var hasFifthRow=false;
var hasPreviousMonth=false;

function doGet(e) {
  var calendarID = "your_cal_id";
  var calName = "My Calendar";
  var future;
  if(!e.parameter.future){
    future = 0
  } else {
    future = +e.parameter.future;
    hasPreviousMonth = true;
  }
  if(!d){
    var d = new Date();
  }
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthNumber = future + d.getMonth()
  var yearNumber = d.getFullYear();

  if(monthNumber > 12){
    monthNumber = monthNumber - 12;
    yearNumber = yearNumber + 1;
  }



  var monthBegin = new Date(yearNumber, monthNumber, 1);
  var firstSundayInGrid = new Date(monthBegin);



  monthNumber = monthNumber + 1;
  if(monthNumber > 12){
    monthNumber = monthNumber - 12;
    yearNumber = yearNumber + 1;
  }

  var prevMonthNumber = monthNumber - 1;
  var prevYearNumber = yearNumber;
  if(prevMonthNumber < 1){
    prevMonthNumber = prevMonthNumber + 12;
    prevYearNumber = prevYearNumber - 1;
  }
  var nextMonthNumber = monthNumber + 1;
  var nextYearNumber = yearNumber;
  if(nextMonthNumber > 12){
    nextMonthNumber = nextMonthNumber - 12;
    nextYearNumber = nextYearNumber + 1;
  }

  var calDate = months[monthNumber-1].concat(" ", yearNumber);
  firstSundayInGrid.setDate(monthBegin.getDate() - monthBegin.getDay());
  var monthEnd = new Date(yearNumber, monthNumber,0);
  var lastSaturdayInGrid = new Date(monthEnd);
  lastSaturdayInGrid.setDate(monthEnd.getDate() + (6 - monthEnd.getDay()));

  var cal = CalendarApp.getCalendarById(calendarID);

  var gridStrings = [];
  var i=0;
  for(var indexDate = new Date(firstSundayInGrid);indexDate <= lastSaturdayInGrid;indexDate.setDate(indexDate.getDate()+1)){
    if(indexDate.getDate() == 1){
      gridStrings[i] = "<strong>" + months[indexDate.getMonth()].concat(" ", indexDate.getDate().toString()) + "</strong><br/>";
    } else {
      gridStrings[i] = indexDate.getDate().toString() + "<br/>";
    }
    var calEvents = cal.getEventsForDay(indexDate);
    for(e in calEvents){
      if(calEvents[e].isAllDayEvent()){
        gridStrings[i] = gridStrings[i].concat(calEvents[e].getTitle(), '<br/>');
      } else if(calEvents[e].getStartTime() < indexDate){
        gridStrings[i] = gridStrings[i].concat(calEvents[e].getTitle(), '<br/>');
      } else {
        gridStrings[i] = gridStrings[i].concat(friendlyTimeString(calEvents[e].getStartTime()), ' - ', calEvents[e].getTitle(), '<br/>');
      }
    }
    i++;
  }

  var calGrid = HtmlService.createTemplateFromFile("CalendarTemplate");
  calGrid.CalName = calName;
  calGrid.CalDate = calDate;
  var newFuture;
  if(future>1){
    newFuture = future - 1;
    calGrid.PrevMonthURL = "?future=" + newFuture;
    calGrid.PrevMonth = months[prevMonthNumber-1].concat(" ", prevYearNumber);
  }
  if(future==1){
    calGrid.PrevMonthURL = "";
    calGrid.PrevMonth = months[prevMonthNumber-1].concat(" ", prevYearNumber);
  }
  newFuture = future + 1;
  calGrid.NextMonthURL = "?future=" + newFuture;
  calGrid.NextMonth = months[nextMonthNumber-1].concat(" ", nextYearNumber);

  calGrid.Sun0 = gridStrings[0];
  calGrid.Mon0 = gridStrings[1];
  calGrid.Tue0 = gridStrings[2];
  calGrid.Wed0 = gridStrings[3];
  calGrid.Thu0 = gridStrings[4];
  calGrid.Fri0 = gridStrings[5];
  calGrid.Sat0 = gridStrings[6];

  calGrid.Sun1 = gridStrings[7];
  calGrid.Mon1 = gridStrings[8];
  calGrid.Tue1 = gridStrings[9];
  calGrid.Wed1 = gridStrings[10];
  calGrid.Thu1 = gridStrings[11];
  calGrid.Fri1 = gridStrings[12];
  calGrid.Sat1 = gridStrings[13];

  calGrid.Sun2 = gridStrings[14];
  calGrid.Mon2 = gridStrings[15];
  calGrid.Tue2 = gridStrings[16];
  calGrid.Wed2 = gridStrings[17];
  calGrid.Thu2 = gridStrings[18];
  calGrid.Fri2 = gridStrings[19];
  calGrid.Sat2 = gridStrings[20];

  calGrid.Sun3 = gridStrings[21];
  calGrid.Mon3 = gridStrings[22];
  calGrid.Tue3 = gridStrings[23];
  calGrid.Wed3 = gridStrings[24];
  calGrid.Thu3 = gridStrings[25];
  calGrid.Fri3 = gridStrings[26];
  calGrid.Sat3 = gridStrings[27];

  if(gridStrings[28]){
      hasFourthRow=true;
      calGrid.Sun4 = gridStrings[28];
      calGrid.Mon4 = gridStrings[29];
      calGrid.Tue4 = gridStrings[30];
      calGrid.Wed4 = gridStrings[31];
      calGrid.Thu4 = gridStrings[32];
      calGrid.Fri4 = gridStrings[33];
      calGrid.Sat4 = gridStrings[34];
  }

  if(gridStrings[35]){
      hasFifthRow=true;
      calGrid.Sun5 = gridStrings[35];
      calGrid.Mon5 = gridStrings[36];
      calGrid.Tue5 = gridStrings[37];
      calGrid.Wed5 = gridStrings[38];
      calGrid.Thu5 = gridStrings[39];
      calGrid.Fri5 = gridStrings[40];
      calGrid.Sat5 = gridStrings[41];
  }

  calGrid.datePrinted = d.toDateString();

  return calGrid.evaluate();
  }

  function fourthRow() {
  return hasFourthRow;
  }

  function fifthRow() {
  return hasFifthRow;
  }

  function showPreviousMonth(){
  return hasPreviousMonth;
  }


  function friendlyTimeString(time){
  var hours = time.getHours();
  var minutes = time.getMinutes();
  var minutesString = "";
  if(minutes < 10){
      minutesString = "0" + minutes.toString();
  } else {
      minutesString = minutes.toString();
  }
  var dayPart = "AM";
  if(hours>=12){
      dayPart = "PM";
      hours -= 12;
  }
  if(hours == 0){
      hours = 12;
  }
  return hours.toString() + ":" + minutesString + " " + dayPart;

And the HTML for the template:

<!DOCTYPE html>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}

@media print  { .noprint  { display: none; } }

tfoot {
    font-size: small;
}

table {
    width: 100%;
    height: 100%;
}
    
th, td {
    padding: 5px;
    vertical-align: top;
    width: 14%;
}

h2 {
    text-align: center;
}

body {
    margin: 1;
    padding: 1;
}

.cal{
    min-height: 90px;
}

.container {
    position: relative;
    width: 100%;
}

.right {
    position: absolute;
    right: 0px;
    width: 50px;
}

.left {
    position: absolute;
    left: 0px;
    width: 50px;
}
</style>
<div>
<div class="container">
    <img class="left" src="">

<img class="right" src="">
<h2><?=CalName?><br/>
       <?=CalDate?>
   </h2>
</div>
<table>
 <tr>
  <th>Sun</th>
  <th>Mon</th>
  <th>Tue</th>
  <th>Wed</th>
  <th>Thu</th>
  <th>Fri</th>
  <th>Sat</th>
 </tr>
 <tr>
  <td><div class="cal"><?!=Sun0?></div></td>
  <td><?!=Mon0?></td>
  <td><?!=Tue0?></td>
  <td><?!=Wed0?></td>
  <td><?!=Thu0?></td>
  <td><?!=Fri0?></td>
  <td><?!=Sat0?></td>
 </tr>
 <tr>
  <td><div class="cal"><?!=Sun1?></div></td>
  <td><?!=Mon1?></td>
  <td><?!=Tue1?></td>
  <td><?!=Wed1?></td>
  <td><?!=Thu1?></td>
  <td><?!=Fri1?></td>
  <td><?!=Sat1?></td>
 </tr>
 <tr>
  <td><div class="cal"><?!=Sun2?></div></td>
  <td><?!=Mon2?></td>
  <td><?!=Tue2?></td>
  <td><?!=Wed2?></td>
  <td><?!=Thu2?></td>
  <td><?!=Fri2?></td>
  <td><?!=Sat2?></td>
 </tr>
 <tr>
  <td><div class="cal"><?!=Sun3?></div></td>
  <td><?!=Mon3?></td>
  <td><?!=Tue3?></td>
  <td><?!=Wed3?></td>
  <td><?!=Thu3?></td>
  <td><?!=Fri3?></td>
  <td><?!=Sat3?></td>
 </tr>
 <?  if(fourthRow()){?>
     <tr>
       <td><div class="cal"><?!=Sun4?></div></td>
       <td><?!=Mon4?></td>
       <td><?!=Tue4?></td>
       <td><?!=Wed4?></td>
       <td><?!=Thu4?></td>
       <td><?!=Fri4?></td>
       <td><?!=Sat4?></td>
     </tr>
 <?  } ?>
 <?  if(fifthRow()){?>
     <tr>
       <td><div class="cal"><?!=Sun5?></div></td>
       <td><?!=Mon5?></td>
       <td><?!=Tue5?></td>
       <td><?!=Wed5?></td>
       <td><?!=Thu5?></td>
       <td><?!=Fri5?></td>
       <td><?!=Sat5?></td>
     </tr>
 <?  } ?>
 <tfoot>
   <tr><td colspan="7">This calendar is accurate as of <?= datePrinted ?>.</td></tr>
 </tfoot>
</table>
</div>
<div class="noprint">
   <table>
      <tr>
         <td>
            <? if(showPreviousMonth()){?>
               <a href="url to your script when you publish it<?!=PrevMonthURL?>" target="_new"><?!=PrevMonth?></a>
            <? } ?>
         </td>
         <td style="text-align: right">
            <a href="url to your script when you publish it<?!=NextMonthURL?>" target="_new"><?!=NextMonth?></a>
         </td>
      </tr>
   </table>
</div>
like image 196
bigelowr Avatar answered Nov 11 '22 10:11

bigelowr