发送活动 Sheet 的 PDF 附件,并在同一活动 Sheet 中使用电子邮件单元格引用

Send PDF Attachment of Active Sheet with Email Cell Reference in Same Active Sheet

我正在尝试将活动 sheet 并且仅活动 sheet 作为 pdf 附件发送到位于单元格 C1 或 (1,3)

中的电子邮件

当前脚本执行但我没有收到电子邮件。

function sendSheetToPdf(){
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var email = sheet.getRange(1,3).getValue();

// Base URL
var url = "mysheetsurl", ss.getId());

/* Specify PDF export parameters
From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
 */

var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
     + '&size=A4' // paper size legal / letter / A4
     + '&portrait=true' // orientation, false for landscape
     + '&fitw=true&source=labnol' // fit to page width, false for actual size
     + '&sheetnames=false&printtitle=false' // hide optional headers and footers
     + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
     + '&fzr=false' // do not repeat row headers (frozen rows) on each page
     + '&gid='; // the sheet's Id

var token = ScriptApp.getOAuthToken();

var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {                                 
        headers : {
            'Authorization' : 'Bearer ' + token
        }
    }).getBlob().setName(sheet.getName() + ".pdf");

sheet_as_pdf_blob_document=response;


// Email Business
var recipient = email;
var subject="Test"
var body="TEST.";
var nameOfSender="Test";

// Here we send the email

function sendReport() {
 
   var message = {
    to: recipient,
    subject: subject,
    body: body,
    name: nameOfSender,
    attachments: [sheet_as_pdf_blob_document]
  }

   MailApp.sendEmail(message);
}

我曾经编写过成功发送电子邮件的脚本,但从来没有指定 sheet 姓名和电子邮件地址。这次我想创建一种基于活动 sheet 发送电子邮件的动态方式。提前致谢!

您当前代码中的问题:

  1. 你的函数结构不正确。您的代码应该会在您的编辑器中显示 运行 执行失败日志。

这是它的样子:

function sendSheetToPdf(){
}

// process the sheet data and get pdf blob

function sendReport() {

  //send email
}
  1. 你的基础 url 不正确。根据你的参考 material Specify PDF export parameters,你的基础 url 应该是这样的:

    var url = "https://docs.google.com/spreadsheets/d/" + doc.getId() + "/export?


示例工作代码:

function sendSheetToPdf(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var email = sheet.getRange(1,3).getValue();

  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/"+ ss.getId()+"/export?";
  
  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */

  var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
      + '&size=A4' // paper size legal / letter / A4
      + '&portrait=true' // orientation, false for landscape
      + '&fitw=true&source=labnol' // fit to page width, false for actual size
      + '&sheetnames=false&printtitle=false' // hide optional headers and footers
      + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
      + '&fzr=false' // do not repeat row headers (frozen rows) on each page
      + '&gid='; // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {                                 
          headers : {
              'Authorization' : 'Bearer ' + token
          }
      }).getBlob().setName(sheet.getName() + ".pdf");

  var sheet_as_pdf_blob_document=response;


  // Email Business
  var recipient = email;
  var subject="Test"
  var body="TEST.";
  var nameOfSender="Test";

  // Here we send the email

  var message = {
  to: recipient,
  subject: subject,
  body: body,
  name: nameOfSender,
  attachments: [sheet_as_pdf_blob_document]
  }

  MailApp.sendEmail(message);
   
}
  • 我刚刚修复了您的 base url,将 sendReport() 内容移到 sendSheetToPdf() 中,因为发送电子邮件所需的变量已在 sendSheetToPdf().
  • 中声明

输出: