如何在Google Apps脚本中获得新创建的文档(“application / pdf”)? [英] How to getAs("application/pdf") a newly created document in Google Apps Script?

查看:120
本文介绍了如何在Google Apps脚本中获得新创建的文档(“application / pdf”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var doc = DocumentApp.create('Sample Document'); 
var body = doc.getBody();
//在文档中创建大量内容...
//
//在添加所有内容并应用样式等之后
//将文档作为PDF发送给用户
var pdf_file = doc.getAs(application / pdf);
GmailApp.sendEmail('user@test.com','附件示例','请参阅附件。',{
附件:[pdf_file],
名称:'测试名称'
});

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



Google Apps脚本可以使用以下任何一种方式:


  • 在转换为PDF之前等待内容被添加?确保 getAs()使用版本的 doc


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

    正确的,在行末加上: doc.saveAndClose(); 构建该文档的代码。


    I don't have access to DriveApp but am trying to email a PDF of a newly created document with:

    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'
     });
    

    The PDF attachment received, however, is a blank document and I think this is because the document is being "got" as a PDF file before the contents have been added to the document.

    Is there any way for Google Apps Script to either:

    • Wait until the content has been added before converting to PDF?

    • Ensure that getAs() uses a "refreshed" version of doc (that has all the added contents)?

    解决方案

    The PDF attachment received, however, is a blank document and I think this is because the document is being "got" as a PDF file before the contents have been added to the document.

    Correct, add the line: doc.saveAndClose(); to the end of the code that builds the doc.

    这篇关于如何在Google Apps脚本中获得新创建的文档(“application / pdf”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆