Convert multi-sheet Google Sheet to PDF using Apps Script

This Google Apps Script creates a PDF from a multi-sheet Google Sheet with the option of emailing the PDF.

Simply copy the script into the script editor (Tools > Script editor…) of the GSheet in question, save it, refresh the sheet and use the new custom menu option (Create PDF > Create PDF).

It is also possible to call the script for another spreadsheet, specify just the one tab to be exported and specify an email address to send it to.

Here is a list of all the options I could find for exporting a PDF from a Google spreadsheet.

Further to user requests, I have created many variations on the PDF creation theme.

Get in touch now if you need any help with this or any other GAS scripts or have a look through some other free scripts and snippets.

The Gist of the script.

Share

28 thoughts on “Convert multi-sheet Google Sheet to PDF using Apps Script”

  1. This is such a wonderful script! Is there a way to narrow it down even further by having a PDF generated only up to where cells contain value, instead of the whole sheet which may contain an ungodly amount of blank rows?

    1. Hi Diane, Glad you found it useful. To just send individual sheets you’d need to create an array of sheet names and pass them to the function one by one, I’ve not tested this but try:

      var sheetNames = [‘Sheet Name 1’, ‘Sheet Name 4’, ‘Sheet Name 5’]

      sheetNames.forEach(function(sheetName) {

      convertSpreadsheetToPdf(TEST_EMAIL, null, sheetName)
      })

  2. Hi! If someone is also looking for a solution how to get multiple sheets into one pdf file then anwer is hiding the sheets.

    You just hide the sheets not needed in pdf and run the script (without specifing sheetName variable)

    Keep only needed sheets visible:

    function hideSheets() {

    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

    var visible = [“Sheet 1”, “Sheet 2”]; //sheet names

    sheets.forEach(function(sheet) {

    if (visible.indexOf(sheet.getName()) == -1) {

    sheet.hideSheet();

    }

    })

    };

    Show all sheets:

    function showSheets() {

    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

    sheets.forEach(function(sheet) {

    sheet.showSheet()

    })
    };

  3. Hi Andrew great script just wondering using your answer to Diane to send
    multiple pages to the PDF using this code

    var sheetNames = [‘Sheet Name 1’, ‘Sheet Name 4’, ‘Sheet Name 5’]

    sheetNames.forEach(function(sheetName) {

    convertSpreadsheetToPdf(TEST_EMAIL, null, sheetName)
    })

    I get an error

    Cannot call method “getSheetId” of null. (line 16, file “test”)

    any thought on how it could be fix

Leave a Comment

Scroll to Top