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

如何修改Google Sheet脚本仅发送指定工作表「PO Template」为PDF附件?

Fix: Send Only "PO Template" Sheet as PDF Attachment

Got it, let's tweak your script so it only sends the specific "PO Template" worksheet instead of the entire Google Sheet file. The original code had two main issues: it was exporting the full spreadsheet, plus there was an undefined shs variable that would throw errors.

Here's the revised script with comments breaking down the key changes:

function emailGoogleSpreadsheetAsPDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var email = ss.getRange("A19").getValue();
  var PO = ss.getRange("N3").getValue();
  var subject = "PO No. " + PO;
  
  // 1. Grab the exact sheet we need to export
  var targetSheet = ss.getSheetByName("PO Template");
  if (!targetSheet) {
    SpreadsheetApp.getUi().alert("Sheet 'PO Template' not found! Check the sheet name and try again.");
    return;
  }
  
  // 2. Build a custom URL to export only this sheet as PDF
  var sheetId = targetSheet.getSheetId();
  var exportUrl = "https://docs.google.com/spreadsheets/d/" + ss.getId() + "/export?" +
    "format=pdf" +
    "&gid=" + sheetId + // Critical: specifies which sheet to export
    "&portrait=true" + // Switch to false for landscape orientation
    "&fitw=true" + // Fit content to page width
    "&sheetnames=false&printtitle=false&pagenumbers=false" + // Hide extra UI elements
    "&gridlines=false"; // Remove gridlines from the PDF (adjust as needed)
  
  // 3. Fetch the PDF blob with proper authorization
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(exportUrl, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });
  var blob = response.getBlob().setName(ss.getName() + "_PO Template.pdf");
  
  // 4. Send the email with the targeted PDF attachment
  var body = "Your PO document is attached as a PDF.";
  if (MailApp.getRemainingDailyQuota() > 0) {
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments: [blob]
    });
  }
}

Key Updates Explained:

  • Targeted Sheet Check: We explicitly select the "PO Template" sheet and add a safety check to alert you if the sheet is missing or renamed.
  • Custom Export URL: Instead of exporting the entire spreadsheet, we use Google Sheets' export API with the gid parameter set to the target sheet's unique ID. This ensures only that sheet gets converted to PDF.
  • Clean PDF Output: Added optional parameters to remove clutter like gridlines, page numbers, and sheet names from the final PDF (you can tweak these settings if you need them visible).
  • Authorized Fetch: Since we're accessing the export URL directly, we include an OAuth token to authenticate the request, which avoids permission errors.
  • Clear Filename: Updated the PDF name to include the sheet name so recipients can easily identify the attachment.

内容的提问来源于stack exchange,提问作者Eric K.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:43:24