请求编写仅导出单个Spreadsheet Sheet为PDF的Script
Export Single Sheet as PDF (With Linked Charts)
Got it, let's solve this problem—since your sheet relies on charts pulling data from other sheets in the same spreadsheet, copying it to a new file breaks those links, so we need a script that directly exports just that single sheet as a PDF while preserving all the linked data and charts.
Here's a custom Google Apps Script that does exactly that:
function exportSingleSheetAsPDF() { // Update these values to match your spreadsheet and target sheet const SPREADSHEET_ID = "your-spreadsheet-id-here"; const TARGET_SHEET_NAME = "Sheet-You-Want-to-Export"; // Get the spreadsheet and target sheet objects const spreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID); const targetSheet = spreadsheet.getSheetByName(TARGET_SHEET_NAME); // Validate the sheet exists if (!targetSheet) { throw new Error(`Oops, couldn't find a sheet named "${TARGET_SHEET_NAME}" in your spreadsheet.`); } const sheetId = targetSheet.getSheetId(); // Build the export URL with parameters focused on the single sheet const exportUrl = `https://docs.google.com/spreadsheets/d/${SPREADSHEET_ID}/export?` + `format=pdf` + `&gid=${sheetId}` + // This is the key parameter to target a single sheet `&portrait=true` + // Switch to false for landscape orientation `&fitw=true` + // Fit content to page width `&sheetnames=false&printtitle=false` + // Hide sheet names and document title `&pagenumbers=false&gridlines=false` + // Hide page numbers and gridlines `&fzr=false`; // Don't freeze header rows in the export // Fetch the PDF content with proper authorization const response = UrlFetchApp.fetch(exportUrl, { headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() } }); // Save the PDF to your Google Drive const pdfBlob = response.getBlob().setName(`${TARGET_SHEET_NAME}.pdf`); const savedFile = DriveApp.createFile(pdfBlob); // Log the file URL so you can easily access it console.log(`Success! Your PDF is saved here: ${savedFile.getUrl()}`); }
How to Use This Script:
- Open your Google Spreadsheet.
- Click
Extensions > Apps Scriptto open the script editor. - Replace the placeholder values for
SPREADSHEET_IDandTARGET_SHEET_NAMEwith your actual spreadsheet ID (found in the URL of your sheet) and the name of the sheet you want to export. - Run the function
exportSingleSheetAsPDF—you'll need to grant authorization the first time (this is safe, it's just giving the script permission to access your spreadsheet and save files to Drive).
Customization Tips:
- Adjust Orientation: Change
portrait=truetoportrait=falseif you want a landscape PDF. - Show Gridlines: Set
gridlines=trueif you want gridlines included in the export. - Specify a Range: Add
&range=A1:Z50to the export URL if you only want to export a specific range of cells instead of the entire sheet. - Include Sheet Names: Switch
sheetnames=trueif you want the sheet name to appear at the top of the PDF.
This script works perfectly with sheets that have linked charts because it exports directly from the original spreadsheet—so all the chart data references stay intact, and you only get the single sheet you need as a PDF.
内容的提问来源于stack exchange,提问作者Santiago Ruffino
相关产品推荐
相关产品推荐

