Email 以PDF格式发送谷歌表单

Email 以PDF格式发送谷歌表单,email,pdf,google-apps-script,Email,Pdf,Google Apps Script,我有一个脚本,通过电子邮件给我发送了一份谷歌电子表格的PDF。我只想它给我发电子邮件的第一个'标签',如果可能的话,作为一个单一的PDF格式的压缩文件 我想知道是否有人能帮忙。还有一个“标签”是隐藏的,所以我不知道这是否有影响。他是样本的链接 代码如下所示: /* Send Spreadsheet in an email as PDF, automatically */ function emailSpreadsheetAsPDF() { // Send the PDF of the spre

我有一个脚本,通过电子邮件给我发送了一份谷歌电子表格的PDF。我只想它给我发电子邮件的第一个'标签',如果可能的话,作为一个单一的PDF格式的压缩文件

我想知道是否有人能帮忙。还有一个“标签”是隐藏的,所以我不知道这是否有影响。他是样本的链接

代码如下所示:

/* 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]     
    });  
}
/*自动以PDF格式通过电子邮件发送电子表格*/
函数emailSpreadsheetAsPDF(){
//将电子表格的PDF发送到此电子邮件地址
var电子邮件=”xxxxx@gmail.com"; 
//获取当前活动的电子表格URL(链接)
//或者使用SpreadsheetApp.openByUrl(“”);
var ss=SpreadsheetApp.getActiveSpreadsheet();
//电子邮件的主题
var subject=“样本表”+ss.getName();
//电子邮件正文也可以是HTML,带有您的徽标图像-请参阅ctrlq.org/HTML-mail
var body=“为一键转换安装。”;
//基本URL
变量url=”https://docs.google.com/spreadsheets/d/SS_ID/export?.replace(“SS_ID”,SS.getId());
/*指定PDF导出参数
发件人:https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext='exportFormat=pdf&format=pdf'//导出为pdf/csv/xls/xlsx
+“&size=letter”//纸张大小合法/letter/A4
+“&trait=false”//方向,横向为false
+“&fitw=true&source=labnol”//适合页面宽度,实际大小为false
+“&sheetnames=false&printtitle=false”//隐藏可选的页眉和页脚
+“&pagenumbers=false&gridlines=false”//隐藏页码和网格线
+“&fzr=false”//不要在每页上重复行标题(冻结行)
+'&gid=';//工作表的Id
var token=ScriptApp.getOAuthToken();
var sheets=ss.getSheets();
//创建一个空数组来保存获取的blob
var blobs=[];
对于(变量i=0;i 0)
GmailApp.sendmail(电子邮件、主题、正文、{
htmlBody:body,
附件:[zipBlob]
});  
}
//**************************************************************************
//修订守则
//**************************************************************************
/*自动以PDF格式通过电子邮件发送电子表格*/
函数emailSpreadsheetAsPDF(){
//将电子表格的PDF发送到此电子邮件地址
var电子邮件=”xxxxx@gmail.com"; 
//获取当前活动的电子表格URL(链接)
//或者使用SpreadsheetApp.openByUrl(“”);
var ss=SpreadsheetApp.getActiveSpreadsheet();
//电子邮件的主题
var subject=“从电子表格生成的PDF”+ss.getName();
//电子邮件正文也可以是HTML,带有您的徽标图像-请参阅ctrlq.org/HTML-mail
var body=“为一键转换安装。”;
//基本URL
变量url=”https://docs.google.com/spreadsheets/d/SS_ID/export?.replace(“SS_ID”,SS.getId());
/*指定PDF导出参数
发件人:https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
*/
var url_ext='exportFormat=pdf&format=pdf'//导出为pdf/csv/xls/xlsx
+“&size=letter”//纸张大小合法/letter/A4
+“&trait=false”//方向,横向为false
+“&fitw=true&source=labnol”//适合页面宽度,实际大小为false
+“&sheetnames=false&printtitle=false”//隐藏可选的页眉和页脚
+“&pagenumbers=false&gridlines=false”//隐藏页码和网格线
+“&fzr=false”//不要在每页上重复行标题(冻结行)
+“&gid=”;//工作表的Id
var token=ScriptApp.getOAuthToken();
var sheets=ss.getSheets();
//创建一个空数组来保存获取的blob
var blobs=[];

//对于代码中的(var i=0;i,您将两张纸都作为blob并压缩


最简单的修复方法是将
更改为(var i=0;i在代码中,您将两张纸都作为blob并压缩


最简单的修复方法是更改
(var i=0;i您的代码似乎工作正常,您收到了什么错误?是的,代码按原样工作,但我想实现的只是将“Front”选项卡发送电子邮件,并将其作为PDF格式的压缩文件发送。当前,压缩文件通过电子邮件发送,附带2个PDF(每个PDF文件1个)。我只想在第一个选项卡上发送一个PDF。Kind Aligy您的代码似乎工作正常,您遇到了什么错误?是的,代码按原样工作,但我想实现的是只将“Front”选项卡发送电子邮件,并将其作为PDF格式的压缩文件发送。目前,一个压缩文件通过电子邮件发送2个PDF(每个选项卡1个)。我只想在第一个选项卡上发送一份PDF。Kind认为AliGSo有任何方法可以阻止它压缩,所以它只发送PDF。考虑到AligOk,所以我停止了压缩,但想按照第一个选项卡命名文件。目前它将文件命名为export.pdfHi James,我已将上面的修订代码包含在原始代码下面,fu非常感谢,标记为“修订代码”。非常感谢。AlGood,没问题。请单击绿色勾号接受我的回答。感谢这对我来说非常有效,可以在我的电子表格中发送特定工作表的定期电子邮件。我想用它为每个月发送提醒。有没有办法选择上个月的工作表作为emailed?意思是,如果我设定了一个计划,在一个月的第一个月运行脚本,我希望上个月选项卡中的工作表被发送?否则答案很好!所以有没有办法阻止它压缩,所以它只发送PDF。关于AligOk,所以我已经停止了压缩,但想按照第一个选项卡命名文件。目前它将文件命名为导出。pdfHi James,我已经在原始代码下面加入了上面的修订代码,很有趣,标记为“修订代码”。非常感谢。AlGood,没问题。请单击绿色勾号接受我的答案。谢谢这对我来说非常有效,可以发送一份回复
function emailSpreadsheetAsPDF() {

    var email = ""; // Enter the required email address here

    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var sheet = ss.getSheetByName("**********"); // Enter the name of the sheet here

    var subject = "PDF generated from spreadsheet " + ss.getName();

    var body = "\n Attached is a PDF copy of the sheet " + sheet.getName() + " in the " + ss.getName() + " spreadsheet.";

    // 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 response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
            headers : {
                'Authorization' : 'Bearer ' + token
            }
        }).getBlob().setName(sheet.getName() + ".pdf");

    // Uncomment the line below to save the PDF to the root of your drive. 
    //  var newFile = DriveApp.createFile(response).setName(sheet.getName() + ".pdf")

    if (MailApp.getRemainingDailyQuota() > 0)
        GmailApp.sendEmail(email, subject, body, {
            htmlBody : body,
            attachments : [response]
        });
}