【发布时间】:2020-05-08 05:49:38
【问题描述】:
我一直在尝试制作一个系统,我可以在其中更改谷歌文档的内容,然后将其保存为 pdf。该脚本在 Google 表格中运行,所以我不确定这是否是我的问题。我可以使用正确的信息对其进行编辑,但它仍然是文档,而不是 PDF。我也不编码,所以这是我根据其他人的工作得到的。任何帮助都会很棒,谢谢!
function autoFillGoogleDocFromForm(e) {
//e.values is an array of form values
var timestamp = e.values[0];
var InvenName = e.values[1];
var Case = e.values[2];
var AppNumber = e.values[3];
var Firm = e.values[4];
var InvenTitle = e.values[5];
var Date = e.values[6];
var AppType = e.values[7];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('FILE_ID_HERE');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('FOLDER_ID_HERE')
var copy = file.makeCopy(Case + ' ' + 'E Assigmnt' + ' ' + AppType + ' ' + AppNumber + ' ' + InvenName ,folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var doc = DocumentApp.openById(copy.getId());
//Since everything we need to change is in the body, we need to get that
var body = doc.getBody();
var footer = doc.getFooter();
//Then we call all of our replaceText methods
body.replaceText('{{Name}}', InvenName);
body.replaceText('{{Title}}' ,InvenTitle);
body.replaceText('{{AppNumber}}', AppNumber);
body.replaceText('{{Date}}' , Date);
footer.replaceText('{{FirmNumber}}', Firm);
footer.replaceText('{{Case}}', Case);
//Lastly we save and close the document to persist our changes
DocumentApp.getActiveDocument().saveAndClose();
var doc = DocumentApp.getActiveDocument();
var docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
docblob.setName(doc.getName() + ".pdf");
}
【问题讨论】:
标签: javascript google-apps-script google-sheets