且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-09 12:42:05

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

然后通过电子邮件将它发送到需要的任何地方,使用您已有的

就像这样,记住这个脚本需要你编辑几个位,并且只导出一个页面,如果你需要的话,它可以做更多的页面。



这未经测试

 函数creatPDF(){$​​ b $ b SpreadsheetApp .flush(); 

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

var url = ss.getUrl();

//删除网址后面的'编辑'
url = url.replace(/ edit $ /,'');

//用于以PDF格式导出工作表的附加参数
var url_ext ='export?exportFormat = pdf& format = pdf'+ // export as pdf
// // below参数是可选的...
'& size = letter'+ //纸张尺寸
'& portrait = true'+ //方向,景观为假
'& fitw = true // +适合宽度,false表示实际大小
'& sheetnames = false& printtitle = false& pagenumbers = false'+ //隐藏可选页眉和页脚
'& gridlines = false '+ //隐藏网格线
'& fzr = false'+ //不要在每一页上重复行标题(冻结行)
'& gid ='+ sheet.getSheetId(); //工作表的Id

var token = ScriptApp.getOAuthToken();

var response = UrlFetchApp.fetch(url + url_ext,{
headers:{
'Authorization':'Bearer'+ token
}
} );

var blob = response.getBlob()。setName(ss.getName()+'.pdf');

//从这里开始,您应该可以使用和操作blob发送和发送电子邮件或按照惯例创建文件。
//在这个例子中,我保存pdf以驱动
var email =email@gmail.com;
var subject =新PRT每日报告:09/02/2016;
var body =请查看附件中的PRT每日报告。;

MailApp.sendEmail(电子邮件,主题,正文,{
附件:[{blob
}]
});


}


any help would be greatly appreciated. I use the below code successfully to automatically send an email using triggers daily that turns the attached Google Sheet into a .PDF and emails it to a list of recipients. I am having difficulty in setting the parameter to landscape instead of portrait.

Thank you for your help in advance.

// 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

By using a URL to build the PDF instead, you can specify if it's landscape or not.

Then email it to wherever it needs to go, using what you have already

Something like this, bear in mind this script needs you to edit a few bits and only exports a single page, it can do more pages if you needed it to.

This is untested

    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
        }]
    });


}