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

时间:2016-02-09 09:45:16

标签: javascript google-apps-script

任何帮助将不胜感激。我成功使用以下代码,每天使用触发器自动发送电子邮件,将附加的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

1 个答案:

答案 0 :(得分:2)

通过使用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
        }]
    });


}