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
doc.saveAndClose()beforevar 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. - TanaikeDocumentAppin my implementation notSpreadsheetApp. I couldn't see a similar.flush()method call inDocumentApp? - ScottMcC.saveAndClose()call did the trick. If you can add a response to my question, I'll mark it as correct. - ScottMcC