【问题标题】:Converting a google doc to a pdf results in blank pdf, google scripts将 google doc 转换为 pdf 会导致空白 pdf、google 脚本
【发布时间】:2018-08-31 09:34:55
【问题描述】:

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

很遗憾,驱动器中的文档显示“此文档是由 Google Apps 脚本创建的。”在其中发表评论,电子邮件中的 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();
}

【问题讨论】:

标签: javascript google-apps-script google-sheets google-docs


【解决方案1】:

在将文档转换为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多