Created
June 23, 2019 23:27
-
-
Save lucashaley/f5e4ad3fbd2ac8e578d4aa85946fadbf to your computer and use it in GitHub Desktop.
Google Sheets code to PDF generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD. | |
// Load a menu item called "Project Admin" with a submenu item called "Send Status" | |
// Running this, sends the currently open sheet, as a PDF attachment | |
function onOpen() { | |
var submenu = [{name:"Email Lecture Form", functionName:"createLecturePDF"}]; | |
SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu); | |
} | |
function createLecturePDF() { | |
// Set the Active Spreadsheet so we don't forget | |
var originalSpreadsheet = SpreadsheetApp.getActive(); | |
// Set the message to attach to the email. | |
var message = "Please see attached"; // Could make it a pop-up perhaps, but out of wine today | |
/* | |
// Get Project Name from Cell A1 | |
var projectname = originalSpreadsheet.getRange("A1:A1").getValues(); | |
// Get Reporting Period from Cell B3 | |
var period = originalSpreadsheet.getRange("B3:B3").getValues(); | |
// Construct the Subject Line | |
var subject = projectname + " - Weekly Status Sheet - " + period; | |
*/ | |
var subject = "Weekly Lecture Form"; | |
/* | |
// Get contact details from "Contacts" sheet and construct To: Header | |
// Would be nice to include "Name" as well, to make contacts look prettier, one day. | |
var contacts = originalSpreadsheet.getSheetByName("Contacts"); | |
var numRows = contacts.getLastRow(); | |
var emailTo = contacts.getRange(2, 2, numRows, 1).getValues(); | |
*/ | |
var emailTo = "[email protected]"; | |
// Google scripts can't export just one Sheet from a Spreadsheet | |
// So we have this disgusting hack | |
// Create a new Spreadsheet and copy the current sheet into it. | |
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export"); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
var projectname = SpreadsheetApp.getActiveSpreadsheet(); | |
sheet = originalSpreadsheet.getActiveSheet(); | |
sheet.copyTo(newSpreadsheet); | |
// Find and delete the default "Sheet 1", after the copy to avoid triggering an apocalypse | |
newSpreadsheet.getSheetByName('Sheet1').activate(); | |
newSpreadsheet.deleteActiveSheet(); | |
//repace cell values with text (to avoid broken references) | |
var sourceRange = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns()); | |
var sourcevalues = sourceRange.getValues(); | |
var destRange = newSpreadsheet.getActiveSheet().getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); | |
destRange.setValues(sourcevalues); | |
// Make zee PDF, currently called "Weekly status.pdf" | |
// When I'm smart, filename will include a date and project name | |
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes(); | |
var attach = {fileName:'WeeklyLectureForm.pdf',content:pdf, mimeType:'application/pdf'}; | |
// Send the freshly constructed email | |
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]}); | |
// Delete the wasted sheet we created, so our Drive stays tidy. | |
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true); | |
} | |
function generatePdf() { | |
var sheetName = "Lecture Printout"; | |
var folderID = "use a folder id from google drive"; // Folder id to save in a folder. | |
var pdfName = "Weekly Lecture Printout"; | |
var sourceSpreadsheet = SpreadsheetApp.getActive(); | |
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName); | |
var thisFileId = sourceSpreadsheet.getId(); | |
var thisFile = DriveApp.getFileById(thisFileId); | |
var folder = thisFile.getParents().next(); | |
// var folder = DriveApp.getFolderById(folderID); | |
//Copy whole spreadsheet | |
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder)) | |
//delete redundant sheets | |
var sheets = destSpreadsheet.getSheets(); | |
for (i = 0; i < sheets.length; i++) { | |
if (sheets[i].getSheetName() != sheetName){ | |
destSpreadsheet.deleteSheet(sheets[i]); | |
} | |
} | |
var destSheet = destSpreadsheet.getSheets()[0]; | |
//repace cell values with text (to avoid broken references) | |
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns()); | |
var sourcevalues = sourceRange.getValues(); | |
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns()); | |
destRange.setValues(sourcevalues); | |
//save to pdf | |
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName); | |
var newFile = folder.createFile(theBlob); | |
//Delete the temporary sheet | |
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment