Reputation: 453
In relation to my post:
In relation to the function described below:
function submitData(form) {
var subject='New Feedback';
var body=Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s', form.name,form.email,form.comment);
var folderId = "my-folder-ID"; // Please set the folder ID. // Added
var blob = Utilities.newBlob(body, MimeType.HTML, form.name).getAs(MimeType.PDF); // Added
var file = DriveApp.getFolderById(folderId).createFile(blob); // Added
var aliases = GmailApp.getAliases()
Logger.log(aliases); //returns the list of aliases you own
Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array
GmailApp.sendEmail('[email protected]','From an alias', 'A message from an alias!', {'from': aliases[0],subject: subject,htmlBody: body, attachments: [blob]}); // Modified
// return file.getUrl(); // Modified
return Utilities.formatString('name: %s <br />Email: %s<br />Comment: %s<br />PDF: %s', form.name,form.email,form.comment,file.getUrl());
}
I would like use a document as a base template for creating a PDF file similar to this one: https://jeffreyeverhart.com/2018/09/17/auto-fill-google-doc-from-google-form-submission/ How can I accomplish this with my application? Which is a custom html form as you can see from above.
Thanks in advance for any help!
Upvotes: 1
Views: 1151
Reputation: 201378
form.name,form.email,form.comment
the template Document, and then, you want to export it as the PDF file.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
form.name,form.email,form.comment
. Then, the Document is converted to the PDF data.Before you run the script, please prepare a Google Document which is used as the template.
In this case, {{name}}
, {{email}}
and {{comment}}
are replaced with form.name
, form.email
and form.comment
, respectively. So please include {{name}}
, {{email}}
and {{comment}}
in the template Document.
When your script is modified, it becomes as follows. And please set the file ID of the template Google Document to templateDocumentId
.
var blob = Utilities.newBlob(body, MimeType.HTML, form.name).getAs(MimeType.PDF);
To:
var templateDocumentId = "###"; // Please set the file ID of the template Google Document
var docId = DriveApp.getFileById(templateDocumentId).makeCopy("temp").getId();
var doc = DocumentApp.openById(docId);
// Please modify as follows.
// var body = doc.getBody();
// body.replaceText("{{name}}", form.name).replaceText("{{email}}", form.email).replaceText("{{comment}}", form.comment);
doc.getBody().replaceText("{{name}}", form.name).replaceText("{{email}}", form.email).replaceText("{{comment}}", form.comment); // Modified
doc.saveAndClose();
var blob = doc.getBlob().setName(form.name);
DriveApp.getFileById(docId).setTrashed(true);
If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 3