Calvin
Calvin

Reputation: 355

Google app script only updates document's changes after finishing running. Can I force it to refresh before?

I want to replace a piece of text inside a google document and then convert it to PDF

Here's the problem, the document is only updated with the changes AFTER the script ends. Because of this when I run the following code, the PDF doesn't have the changes done yet.

Is there a way to force it to update while the script is running?

var doc_open = DocumentApp.openById(fileid);
var doc_body = doc_open.getBody();

//replace text
doc_body.replaceText('{name}', student.name);
doc_body.replaceText('{email}', student.email);

//create pdf
var docblob = doc_open.getAs('application/pdf');
docblob.setName(filename + ".pdf");
var file = DriveApp.getFolderById(folderid).createFile(docblob)

Upvotes: 2

Views: 2421

Answers (1)

Gerneio
Gerneio

Reputation: 1340

Try the saveAndClose() method. You'll have to add another line of code to reopen the doc to do anymore operations (if needed).

Upvotes: 4

Related Questions