3

Google Apps 脚本 - Gmail

会实现GmailMessage( GmailThread).getAsPdf()方法吗?预期的输出将与 Gmail 中可用的打印到 PDF 相同。该功能在网站上可用,那么为什么不在 Script 中呢?

这对于将选定的 Gmail 对话以 PDF 格式快速分发给其他人/外部人员是必要的。

另外,GmailMessage.getAttachments()虽然在网上的文档中,在现实中并不存在。这会实施吗?

谢谢

4

1 回答 1

2

我试过这个并且效果很好(不确定这是唯一的方法):

function getattach(){
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var attach = message.getAttachments();
Logger.log(attach[0].getDataAsString() )
if(attach.length>0){
var file=DocsList.createFile(attach[0])
var pdf=file.getAs('application/pdf').getBytes();
// for test purpose I send the pdf as attachment
var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'};
   MailApp.sendEmail('emailadress@gmail.com', 'Your test as PDF ', 'see attachment', {attachments:[attach_to_send]});
file.setTrashed(true);// delete after use ;-)
}
}

编辑1:删除

编辑 2:这是一个带有 pdf 附件正文的新版本,也支持 html(使用 DocsList 服务),临时文档被删除。一言以蔽之:非常令人满意;-)

function getAttachAndBody(){
  var firstThread = GmailApp.getInboxThreads(0,1)[0];
  var message = firstThread.getMessages()[0];
  var attach = message.getAttachments();
  var body = message.getBody();//is a string
  var bodydochtml = DocsList.createFile('body.html', body, "text/html")
  var bodyId=bodydochtml.getId()
  var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
  if(attach.length>0){
    var file=DocsList.createFile(attach[0])
    var pdf=file.getAs('application/pdf').getBytes();
    var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'};
    var body_to_send = {fileName: 'body.pdf',content:bodydocpdf, mimeType:'application/pdf'};
    MailApp.sendEmail('emailadress@gmail.com', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]});
    file.setTrashed(true);
    DocsList.getFileById(bodyId).setTrashed(true)
    }
}
于 2012-06-17T08:39:36.343 回答