通过电子邮件将Google表格发送给PDF

时间:2017-06-03 14:42:21

标签: email pdf google-apps-script

我有这个脚本,通过电子邮件向我发送Google电子表格的PDF。我只想通过电子邮件向我发送第一个' Tab'如果可能的话,作为单个PDF副文件的zip文件。

想知道是否有人可以提供帮助。也是其中一个标签'是隐藏的,所以我不知道这是否有影响。他是示例sheet的链接。

代码如下所示:

/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {

// Send the PDF of the spreadsheet to this email address
  var email = "xxxxx@gmail.com"; 

  // Get the currently active spreadsheet URL (link)
  // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Subject of email message
  var subject = "Sample Sheet " + ss.getName(); 

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

  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", 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=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // 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 sheets = ss.getSheets(); 

  //make an empty array to hold your fetched blobs  
  var blobs = [];

  for (var i=0; i<sheets.length; i++) {

    // Convert individual worksheets to PDF
    var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
    blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');

  }

  //create new blob that is a zip file containing our blob array
  var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip'); 

  //optional: save the file to the root folder of Google Drive
  DriveApp.createFile(zipBlob);

  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

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


//**************************************************************************
//                                 Revised Code
//**************************************************************************


/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {

// Send the PDF of the spreadsheet to this email address
  var email = "xxxxx@gmail.com"; 

  // Get the currently active spreadsheet URL (link)
  // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
  var ss = SpreadsheetApp.getActiveSpreadsheet();

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

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

  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", 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=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // 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 sheets = ss.getSheets(); 

  //make an empty array to hold your fetched blobs  
  var blobs = [];

  //for (var i=0; i<sheets.length; i++) {
  for (var i=0; i<1; i++) {  
    // Convert individual worksheets to PDF
    var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
   // blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');

  }

  //create new blob that is a zip file containing our blob array
 // var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip'); 

  //optional: save the file to the root folder of Google Drive
  //DriveApp.createFile(zipBlob);

  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());

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

亲切的问候 人

1 个答案:

答案 0 :(得分:3)

在你的代码中,你将两张纸都作为blob并压缩它们?

最简单的方法是将<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/white"/> <stroke android:width="2dip" android:color="@color/black"/> </shape> 更改为for (var i=0; i<sheets.length; i++) {,以便只获取第一个标签并将其拉链。

更新根据评论。

for (var i=0; i<1; i++) {