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
- Tab name
SummaryOnly this tab is exported into the PDF.
- Folder name
Monthly PDFsExisting Drive folder where the PDF file will be saved.
- Landscape
falseChange 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.
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
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.
Paste the script into the bound editor
In the spreadsheet, click Extensions > Apps Script, replace the starter code with this script, and save.
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.
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
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Email a Google Doc as a PDF
Send a Google Doc to someone as a PDF attachment. Paste the file ID of the Doc into the script, and it builds the PDF and emails it. The Doc itself is not changed.
- Task guide Back up a spreadsheet to Drive every week
Keep a dated copy of a spreadsheet in a Drive folder. Run the setup function once, and every Monday morning the script saves a new copy with the date in its name.
- Object reference Spreadsheet
A Spreadsheet is the whole Google Sheets file. Use SpreadsheetApp.getActiveSpreadsheet() or openById() to get it, then call getSheetByName() or getActiveSheet().
- Object reference Sheet
A Sheet is one tab inside a Google spreadsheet. Get it with getSheetByName() or getActiveSheet(). Use getLastRow() to find the last row with content, or getRange() to select cells to read.
- Object reference Drive folder
A DriveApp.Folder is a container in Google Drive. Find one by name with DriveApp.getFoldersByName(name) or by ID with DriveApp.getFolderById(id).
- Object reference HTTP response
UrlFetchApp.fetch() returns an HTTPResponse after calling a URL. Pass muteHttpExceptions: true so error status codes return an HTTPResponse you can check with getResponseCode() and getContentText().
- Google app Google Sheets
SpreadsheetApp lets a Google Sheets script read a Sheet or Range, add a custom menu or formula, and act on each row, such as sending one email per row.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDFs to a folder you choose.
- Google app Script utilities
UrlFetchApp, PropertiesService, and LockService work in any Apps Script project. Use them to call web APIs, save settings between runs, and stop two runs from overlapping.
- Book chapter Export PDFs from Sheets and Docs
Export a Google Doc with getAs(MimeType.PDF) or export a single Google Sheet tab and range with layout parameters, flush(), and Drive checkpoints.