Printing and exporting the result
As of this writing (December 2013), Google Apps Script has no possible way to print a document. Printing all or part of a spreadsheet is a common activity, one that we might want to automate using a script. Unfortunately, security considerations limit Google Apps Script access to local resources such as printers.
As a workaround for this limitation, our script can export spreadsheets to PDF and send them by e-mail or store them in Google Drive.
A quick example that won't need too much explanation is as follows:
function sendThisSsAsPdf(){ var ss = SpreadsheetApp.getActiveSpreadsheet(); var ssID = ss.getId();// get the file unique id to use with driveApp var pdf = DriveApp.getFileById(ssID).getAs('application/pdf');//get the file content in PDF format var saveCopy = DriveApp.createFile(pdf);//create a copy in your drive MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'This is a copy of your spreadsheet','This is a pdf copyof your spreadsheet as an attachment to this message\n'+ 'This mail was sent to you on'+Utilities.formatDate(new Date(), Session.getTimeZone(),"MMM-dd-yyyy @ HH:mm"),{attachments : [pdf]});//send the email with a simple message }
The preceding script shows how to simply and rapidly create a PDF version of the current spreadsheet and send it to the user at the keyboard; this function can be called from a menu of course, but can also be sent at a fixed time every single day if you need an archive copy for any reason.
In the next chapter, we'll see how to create and use Google forms as they are probably the second most-frequent entry point to Google Apps Script for most new users.