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

房东的猫 提交于 2021-02-05 08:31:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!