将 google 文档转换为 pdf 会生成空白 pdf,google 脚本

Converting a google doc to a pdf results in blank pdf, google scripts

这是我创建 google 文档的代码,将一些信息粘贴到(最终来自电子表格),将其转换为 pdf 并通过电子邮件发送给我自己。

不幸的是,虽然驱动器中的文档中有 'This document was created by 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