You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写仅导出单个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:

  1. Open your Google Spreadsheet.
  2. Click Extensions > Apps Script to open the script editor.
  3. Replace the placeholder values for SPREADSHEET_ID and TARGET_SHEET_NAME with your actual spreadsheet ID (found in the URL of your sheet) and the name of the sheet you want to export.
  4. 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=true to portrait=false if you want a landscape PDF.
  • Show Gridlines: Set gridlines=true if you want gridlines included in the export.
  • Specify a Range: Add &range=A1:Z50 to the export URL if you only want to export a specific range of cells instead of the entire sheet.
  • Include Sheet Names: Switch sheetnames=true if 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:28:08