问题描述
我从Google Apps脚本开始,尝试创建文件(Google Doc)的副本,然后替换复制文档中的文本并将其转换为PDF.
I am starting in Google Apps Script and trying to create a copy of a file (Google Doc), then replace text in the copied document and convert it to PDF.
该功能正常工作,但是替换的文本不会出现在PDF文件中,而是出现在复制的文件(我替换过的文件)中.
The function works fine, but the text replaced does not appear in the PDF file, but appears in the copied file (where I replaced).
我阅读了有关saveandclose()
方法的信息,该方法用于保存更改并在功能退出之前关闭已复制的文档.但似乎该方法不可用.我非常感谢您的帮助.谢谢.
I read about the saveandclose()
method to save changes and close the document (copied) before the function finishes to eject. But it seem that the method is not available. I really appreciate the help. Thanks.
function replaceconvert() {
var cdoc = DriveApp.getFileById('iddocument').makeCopy('filename321');
var iddoc = cdoc.getId();
var doc = DocumentApp.openById(iddoc);
doc.getBody().replaceText('nombre', 'Juan Perez');
var doc_repl = DriveApp.getFileById(iddoc);
var blob = doc_repl.getAs(MimeType.PDF);
DriveApp.getFolderById(idfolder).createFile(doc1)
}
推荐答案
我确实使用以下对我有用的函数:
I do use the following function, which works for me:
function personaliseAttachment(keyTemplate, member, fileName){
var cloneId = DriveApp.getFileById(keyTemplate).makeCopy('cloneAttachment').getId();
var clone = DocumentApp.openById(cloneId);
var body = clone.getBody();
for (var property in member) {
if (member.hasOwnProperty(property)) {
body.replaceText("{{"+property+"}}", member[property]);
}
}
clone.saveAndClose();
var clonePDF = DriveApp.createFile(clone.getAs('application/pdf'));
clonePDF.setName(fileName);
DriveApp.getFileById(cloneId).setTrashed(true);
return clonePDF;
}
这篇关于转换后的PDF中缺少文档中替换的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!