这是我创建谷歌文档的代码,将一些信息粘贴到(最终来自电子表格(,将其转换为pdf并通过电子邮件发送给我自己。
不幸的是,虽然驱动器中的文档有"此文档是由Google Apps Script创建的"注释,但电子邮件中的pdf没有。它具有正确的标题,但页面内容丢失。我已经尝试了几个关于堆栈溢出的示例,但到目前为止还没有一个可以工作。
var ss = SpreadsheetApp.getActive();
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('TEST MENU') //creates main menu tab
.addItem('pdf', 'pdf')
.addToUi();
}
function pdf() {
// Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Hello, world!');
// Access the body of the document, then add a paragraph.
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
var pdfContent = doc.getAs('application/pdf');
var draftMail = GmailApp.createDraft('will@exampleEmail.co.uk',
'Email title', 'Pls see attached',
{
attachments: [pdfContent.getAs(MimeType.PDF)],
name: 'Converted doc content'
});
// Now send the mail
draftMail.send();
}
在将文档转换为 pdf 之前,您没有保存和关闭文档,也许这就是您更改的内容没有被清除的原因。
尝试这样的事情:
var ss = SpreadsheetApp.getActive();
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('TEST MENU') //creates main menu tab
.addItem('pdf', 'pdf')
.addToUi();
}
function pdf() {
var doc = DocumentApp.create('Hello, world!');
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
doc.saveAndClose()
var pdfContent = doc.getAs('application/pdf');
var draftMail = GmailApp.createDraft('will@exampleEmail.co.uk',
'Email title', 'Pls see attached', {
attachments: [pdfContent.getAs(MimeType.PDF)],
name: 'Converted doc content'
});
draftMail.send();
}
参考 : https://developers.google.com/apps-script/reference/document/document#saveandclose