【问题标题】:How to getAs("application/pdf") a newly created document in Google Apps Script?如何 getAs("application/pdf") Google Apps Script 中新创建的文档?
【发布时间】:2021-04-04 13:15:18
【问题描述】:

我无权访问DriveApp,但我正在尝试通过电子邮件发送新创建文档的 PDF:

var doc = DocumentApp.create('Sample Document');
var body = doc.getBody();
// create lots of content in the document...
//
// after adding all the content and applying styles etc
// send the document as PDF to the user
var pdf_file = doc.getAs("application/pdf");
GmailApp.sendEmail('user@test.com', 'Attachment example', 'Please see the attached file.', {
     attachments: [pdf_file],
     name: 'Test Name'
 });

然而,收到的 PDF 附件是一个空白文档,我认为这是因为在将内容添加到文档之前,该文档已作为 PDF 文件“获取”。

Google Apps 脚本有什么办法:

  • 等到内容添加完毕后再转换为 PDF?

  • 确保getAs() 使用doc 的“刷新”版本(包含所有添加的内容)?

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    然而,收到的 PDF 附件是一个空白文档,我认为这是因为在将内容添加到文档之前,该文档被“获取”为 PDF 文件。

    正确,将行:doc.saveAndClose(); 添加到构建文档的代码末尾。

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2013-07-27
      相关资源
      最近更新 更多