问题
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)
回答1:
Try the saveAndClose() method. You'll have to add another line of code to reopen the doc to do anymore operations (if needed).
来源:https://stackoverflow.com/questions/36893183/google-app-script-only-updates-documents-changes-after-finishing-running-can-i