How to getAs(“application/pdf”) a newly created do

2019-05-18 11:32发布

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)?

1条回答
欢心
2楼-- · 2019-05-18 11:54

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.

查看更多
登录 后发表回答