Skip to content

Task guide

Export one Google Sheet tab as a PDF

Save one tab of a Google Sheet into a Drive folder as a clean PDF without including the other tabs in the workbook.

1 · Prepare

Put these pieces in place

  • Open the Google Sheet that holds the tab you want to export.
  • Create a folder in Google Drive to receive the PDF, such as Monthly PDFs.
  • Check that the tab name in the script matches the tab at the bottom of your spreadsheet.

Change these values

Change these values

Make the code yours before you run it.
Tab name
Summary

Only this tab is exported into the PDF.

Folder name
Monthly PDFs

Existing Drive folder where the PDF file will be saved.

Landscape
false

Change to true for wide tables.

2 · Review before running

What the script can access and change

Permissions you approve

  • See, edit, create, and delete your spreadsheets in Google Drive. Required so the OAuth token can read the spreadsheet through the export endpoint.
  • See, edit, create, and delete all of your Google Drive files. Used to find the destination folder and create the PDF file inside it.
  • Connect to an external service. UrlFetchApp requests the PDF from docs.google.com.

Effects of one run

  • Creates one new PDF file inside the named Drive folder on each run.
  • Leaves every tab, formula, and cell in the source spreadsheet unchanged.

3 · Copy

Paste this into the Apps Script editor

Create or open a script file, replace the example values marked above, then save the project.

Code.gs
const TAB_NAME = 'Summary'; // Only this tab is exported into the PDF.
const FOLDER_NAME = 'Monthly PDFs'; // Existing Drive folder where the PDF file will be saved.
const LANDSCAPE = false; // Change to true for wide tables.

function exportSheetTabAsPdf() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName(TAB_NAME);
  if (!sheet) throw new Error('Add or rename a tab to ' + TAB_NAME + ' before running.');

  const folders = DriveApp.getFoldersByName(FOLDER_NAME);
  if (!folders.hasNext()) {
    throw new Error('Create a Drive folder named ' + FOLDER_NAME + ' before running.');
  }
  const folder = folders.next();

  // Apply any pending cell edits before requesting the PDF export.
  SpreadsheetApp.flush();

  const params = [
    'format=pdf',
    'gid=' + encodeURIComponent(String(sheet.getSheetId())),
    'size=letter',
    'portrait=' + String(!LANDSCAPE),
    'fitw=true',
    'gridlines=false',
    'sheetnames=false',
    'fzr=true',
  ].join('&');

  const exportUrl =
    'https://docs.google.com/spreadsheets/d/' +
    encodeURIComponent(spreadsheet.getId()) +
    '/export?' +
    params;

  const response = UrlFetchApp.fetch(exportUrl, {
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
    muteHttpExceptions: true,
  });

  if (response.getResponseCode() !== 200) {
    throw new Error('PDF export failed with HTTP status ' + response.getResponseCode() + '.');
  }

  const stamp = Utilities.formatDate(new Date(), spreadsheet.getSpreadsheetTimeZone(), 'yyyy-MM-dd');
  const fileName = spreadsheet.getName() + ' - ' + TAB_NAME + ' - ' + stamp + '.pdf';
  const pdfFile = folder.createFile(response.getBlob().setName(fileName));
  Logger.log('Saved PDF: ' + pdfFile.getUrl());
}

4 · Run

Follow these steps in order

  1. Prepare the tab and Drive folder

    Open your Google Sheet and check that it has a tab named Summary (or change TAB_NAME to match your tab). In Google Drive, create a folder named Monthly PDFs.

  2. Paste the script into the bound editor

    In the spreadsheet, click Extensions > Apps Script, replace the starter code with this script, and save.

  3. Run a test export

    Select exportSheetTabAsPdf in the toolbar and click Run. Approve access when prompted, then open Execution log and click the logged Drive URL to inspect the PDF.

  4. Adjust orientation if columns wrap

    If wide columns shrink too much on portrait paper, set LANDSCAPE to true, save, and run again.

5 · Confirm

How to tell it worked

A new PDF named after the spreadsheet, tab, and date appears inside Monthly PDFs, showing only the chosen tab without gridlines.

If the result is different

Troubleshooting

The PDF is missing edits that another function wrote right before the export.
Keep SpreadsheetApp.flush() right before UrlFetchApp.fetch() so pending spreadsheet writes finish before Google renders the PDF.
The run stops with "PDF export failed with HTTP status 401" or 403.
Run the function from an account that can view the spreadsheet, and check that appsscript.json has not restricted oauthScopes to spreadsheets.currentonly, which the /export URL does not accept.
A second folder with the same name received the PDF.
Drive allows duplicate folder names. Rename extra folders or switch DriveApp.getFoldersByName(FOLDER_NAME) to DriveApp.getFolderById(folderId) when two folders share a name.

Technical details

Names used in this script

Open these details when you want to adapt the example or look up an exact service or method name.

Show Apps Script symbols and service pages

Keep going