Generate PDF

To use this script, follow these steps:

  1. Open a Google Sheet that you want to generate a PDF file from.
  2. Go to the “Tools” menu and select “Script editor.”
  3. Copy and paste the code I provided into the script editor.
  4. Replace the FOLDER_ID placeholder in the code with the ID of the folder where you want to save the PDF file.
  5. Save the script and close the script editor.
  6. Go back to your Google Sheet and run the script by selecting the function name (generatePdf) from the “Run” menu.

When you run the script, it will generate a PDF file from the active sheet and save it to the specified folder in Google Drive. The file name will be “My Spreadsheet.pdf” by default, but you can change this in the code if needed. The script will then display a message with the URL of the PDF file.

The code to paste in Google Sheets


function generatePdf() {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();

  // Get the data range
  var range = sheet.getDataRange();

  // Get the blob of the PDF file
  var pdfBlob = sheet.getAs("application/pdf");

  // Save the PDF file to Drive
  var fileName = "My Spreadsheet.pdf";
  var folder = DriveApp.getFolderById("FOLDER_ID"); // Replace FOLDER_ID with the ID of the folder where you want to save the file
  var file = folder.createFile(pdfBlob);
  file.setName(fileName);

  // Display a message with the URL of the PDF file
  var message = "PDF file generated: " + file.getUrl();
  Browser.msgBox(message);
}


Reach us if the process is unclear or if the script is outdated
Apps Scripts template
Free
PDF
Generate PDF

Generate PDF

Last updated
4/4/2023
Built by
Avatar Placeholder
internal

This script generates a PDF file from the active sheet in a Google Sheet and saves it to Google Drive. It then displays a message with the URL of the PDF file.

Similar Apps Script for

PDF

in Google Sheets

No similar items yet.