Google Apps脚本 - 将横向参数添加到PDF电子邮件附件 [英] Google Apps Script - Add landscape parameter to PDF email attachment

查看:95
本文介绍了Google Apps脚本 - 将横向参数添加到PDF电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助将不胜感激。我成功使用以下代码,每天使用触发器自动发送电子邮件,将附加的Google表格转换为.PDF并通过电子邮件发送给收件人列表。

感谢您提前给予帮助。

  // START 
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu(Sender-Thingy)
.addItem(Send,send)
.addToUi();
};

函数send(){
var ss = SpreadsheetApp.getActive();
var email =email@gmail.com;
var subject =新PRT每日报告:09/02/2016;
var body =请查看附件中的PRT每日报告。;


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

// END


解决方案

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


}

这篇关于Google Apps脚本 - 将横向参数添加到PDF电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆