3
votes

I am currently using Google Apps script to undertake the following task in Google Docs:

  • Open a Google Doc of a given Id
  • Replace some text in the document
  • Export the document to a PDF

The issue that I'm having is that the export process is happening before the text is updated in the body of the document. My question is:

How do I halt the execution of exporting the Doc to PDF until the text has been replaced in the Doc?

I've attached a simplified version of my code below:

function replaceTextAndExportToPDF() {
    var doc = DocumentApp.getActiveDocument();

    // [Start block]

    // Clear the text surrounding "Apps Script", with or without text.
    var body = doc.getBody();
    body.replaceText("^.*Apps ?Script.*$", "Apps Script");

    // [End block -  Need to finish code inside this block before continuing]

    // Now export to PDF
    var parentFolder = DriveApp.getFileById(doc.getId()).getParents().next();
    var pdfBlob = doc.getBlob().getAs('application/pdf');
    parentFolder.createFile(pdfBlob)
}

I've tried testing with the Lock Class but have limited knowledge of how it works and haven't had any success

1
I'm sorry. I misunderstood about your document. How about adding doc.saveAndClose() before var pdfBlob = doc.getBlob().getAs('application/pdf');? Document is here. If this was not directly solution for your situation, please tell me. I would like to think of other solution. - Tanaike
I'm using DocumentApp in my implementation not SpreadsheetApp. I couldn't see a similar .flush() method call in DocumentApp? - ScottMcC
I updated my comment. Could you please confirm it? - Tanaike
Bingo. The .saveAndClose() call did the trick. If you can add a response to my question, I'll mark it as correct. - ScottMcC
Thank you for quick reply. I'm glad your issue was solved. I posted an answer by adding more information. Could you please confirm it? - Tanaike

1 Answers

1
votes

In your case, the issue can been resolved by using saveAndClose().

Modification point :

Please modify as follows.

var parentFolder = DriveApp.getFileById(doc.getId()).getParents().next();
var pdfBlob = doc.getBlob().getAs('application/pdf');
parentFolder.createFile(pdfBlob)
var parentFolder = DriveApp.getFileById(doc.getId()).getParents().next();
doc.saveAndClose(); // Added
var pdfBlob = doc.getBlob().getAs('application/pdf');
parentFolder.createFile(pdfBlob)

Note :

  • In the case of Document and Slides, it uses saveAndClose().
  • In the case of Spreadsheet, it uses flush().

References :

Added :

When the blob is retrieved from the Document using getBlob(), the mimeType of the blob is "application/pdf". So you can also use var pdfBlob = doc.getBlob(); instead of var pdfBlob = doc.getBlob().getAs('application/pdf');.

Edit :

For example, you can retrieve the mimeType of the blob of doc.getBlob() by Logger.log(doc.getBlob().getContentType()). It shows application/pdf. I think that the default mimeType is application/pdf when the Google Docs are retrieved as a blob.

But you can retrieve other mimeTypes from Google Document. You can retrieve the formats which can be exported using GET https://www.googleapis.com/drive/v3/about?fields=exportFormats&key={YOUR_API_KEY}. The format for exporting Google Document is as follows.

"application/vnd.google-apps.document": [
 "application/rtf",
 "application/vnd.oasis.opendocument.text",
 "text/html",
 "application/pdf",
 "application/epub+zip",
 "application/zip",
 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
 "text/plain"
],

From this object, Google Document cannot be exported as application/vnd.google-apps.document. It is considered that this is a specification.