首页 文章

Google Script - 将活动工作表作为PDF发送到单元格中列出的电子邮件

提问于
浏览
1

我正在尝试使用下面的脚本将Google表格文档中的第一张表格发送为PDF格式的电子邮件 . 要发送到的电子邮件列在单元格A1中 .

但是,此脚本将整个电子表格作为PDF发送,而不仅仅是第一个表单 . 我一直在尝试使用stackoverflow中的一些其他脚本,但这是唯一一个实际发送电子邮件的脚本 .

希望你们能帮助我 .

/* Email Google Spreadsheet as PDF */
function emailGoogleSpreadsheetAsPDF() {

  // Send the PDF of the spreadsheet to this email address
  var email = "amit@labnol.org"; 

  // Get the currently active spreadsheet URL (link)
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Subject of email message
  var subject = "PDF generated from spreadsheet " + ss.getName(); 

  // Email Body can  be HTML too 
  var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for one-click conversion.";

  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");

  blob.setName(ss.getName() + ".pdf");

  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blob]     
    });  
}

1 回答

  • 3

    下面是一个工作版本,其中包含一些您可能想要使用的有用参数 .

    更新代码

    function sendSheetToPdfwithA1MailAdress(){ // this is the function to call
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getSheets()[0]; // it will send sheet 0 wich is the first sheet in the spreadsheet.
      // if you change the number, change it also in the parameters below
      var shName = sh.getName()
      sendSpreadsheetToPdf(0, shName, sh.getRange('A1').getValue(),"test email with the adress in cell A1 ", "This is it !");
    }
    function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var spreadsheetId = spreadsheet.getId()  
      var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;  
      var url_base = spreadsheet.getUrl().replace(/edit$/,'');
    
      var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
    
          + (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId)) 
          // following parameters are optional...
          + '&size=A4'      // paper size
          + '&portrait=true'    // orientation, false for landscape
          + '&fitw=true'        // fit to width, false for actual size
          + '&sheetnames=true&printtitle=false&pagenumbers=true'  //hide optional headers and footers
          + '&gridlines=false'  // hide gridlines
          + '&fzr=false';       // do not repeat row headers (frozen rows) on each page
    
      var options = {
        headers: {
          'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
        }
      }
    
      var response = UrlFetchApp.fetch(url_base + url_ext, options);
      var blob = response.getBlob().setName(pdfName + '.pdf');
      if (email) {
        var mailOptions = {
          attachments:blob, htmlBody:htmlbody
        }
    MailApp.sendEmail(
          email, 
          subject+" (" + pdfName +")", 
          "html content only", 
          mailOptions);
    
    MailApp.sendEmail(
          Session.getActiveUser().getEmail(), 
          "FRWD "+subject+" (" + pdfName +")", 
          "html content only", 
          mailOptions);
      }
    }
    

相关问题