Reputation: 25
I've been trying to make a system where I change the content of a google doc then save it as a pdf. This script is running in a Google Sheet so I'm not sure if that's my issue. I can get to the point where it's edited with the right info, but it;s still a Doc, not a PDF. Also I don't code, so this is as far as I've gotten based on other people's work. Any help would be great, Thanks!
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");
}
Upvotes: 1
Views: 1850
Reputation: 201398
I believe your goal as follows.
For this, how about this answer?
When your script is modified, please modify as follows.
From:DocumentApp.getActiveDocument().saveAndClose();
var doc = DocumentApp.getActiveDocument();
var docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
docblob.setName(doc.getName() + ".pdf");
To:
doc.saveAndClose();
var docblob = doc.getBlob();
docblob.setName(doc.getName() + ".pdf");
DriveApp.createFile(docblob);
Upvotes: 2