Google Apps 脚本 - 将横向参数添加到 PDF 电子邮件附件

Google Apps Script - Add landscape parameter to PDF email attachment

本文关键字:PDF 电子邮件 添加 参数 脚本 Apps 横向 Google      更新时间:2023-09-26

任何帮助将不胜感激。我成功地使用以下代码每天使用触发器自动发送电子邮件,将附加的 Google 表格转换为.PDF并将其通过电子邮件发送给收件人列表。我很难将参数设置为横向而不是纵向。

提前感谢您的帮助。

// START
function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu("Sender-Thingy")
        .addItem("Send", "send")
        .addToUi();
};
function send() {
    var ss = SpreadsheetApp.getActive();
    var email = "email@gmail.com";
    var subject = "New PRT Daily Report: 09/02/2016";
    var body = "Please find attached your PRT Daily Report.";

    MailApp.sendEmail(email, subject, body, {
        attachments: [{
            fileName: ss.getName() + ".pdf",
            content: ss.getAs("application/pdf").getBytes(),
            mimeType: "application/pdf"
        }]
    });
};
// END

通过使用 URL 来构建 PDF,您可以指定它是否是横向的。

然后通过电子邮件将其发送到需要去的任何地方,使用您已经拥有的内容

像这样的东西,请记住,此脚本需要您编辑几位并且只导出单个页面,如果需要,它可以执行更多页面。

这是未经测试的

    function creatPDF() {
  SpreadsheetApp.flush();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var url = ss.getUrl();
  //remove the trailing 'edit' from the url
  url = url.replace(/edit$/, '');
  //additional parameters for exporting the sheet as a pdf
  var url_ext = 'export?exportFormat=pdf&format=pdf' + //export as pdf
    //below parameters are optional...
    '&size=letter' + //paper size
    '&portrait=true' + //orientation, false for landscape
    '&fitw=true' + //fit to width, false for actual size
    '&sheetnames=false&printtitle=false&pagenumbers=false' + //hide optional headers and footers
    '&gridlines=false' + //hide gridlines
    '&fzr=false' + //do not repeat row headers (frozen rows) on each page
    '&gid=' + sheet.getSheetId(); //the sheet's Id
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url + url_ext, {
    headers: {
      'Authorization': 'Bearer ' + token
    }
  });
  var blob = response.getBlob().setName(ss.getName() + '.pdf');
  //from here you should be able to use and manipulate the blob to send and email or create a file per usual.
  //In this example, I save the pdf to drive
    var email = "email@gmail.com";
    var subject = "New PRT Daily Report: 09/02/2016";
    var body = "Please find attached your PRT Daily Report.";
    MailApp.sendEmail(email, subject, body, {
        attachments: [{blob
        }]
    });

}