Reputation: 139
I am developing a google form which have some text field and image field. Now after submission I am able to generate pdf of text responses. Also the pdf of responses is sent to the respondent email.
I have referred to this Video Tutorial.
function afterFormSubmit(e) {
const info = e.namedValues;
const pdfFile=createPDF(info);
const entryRow=e.range.getRow();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("People").getRange(entryRow,6).setValue(pdfFile.getUrl());
sendEmail(e.namedValues["Email"], pdfFile);
}
function sendEmail(email, pdfFile){
GmailApp.sendEmail(email, "Succesfuly filled the form ", {attachments:[pdfFile],name:"Test Case" });
}
function createPDF(info){
const pdfFolder= DriveApp.getFolderById("pdfFolderID");
const tempFolder=DriveApp.getFolderById("tempFolderID");
const templateDoc=DriveApp.getFileById("TemplateDOc");
const newTempFile= templateDoc.makeCopy(tempFolder);
const openDoc= DocumentApp.openById(newTempFile.getId());
const body=openDoc.getBody();
body.replaceText("{Fn}", info['Name'][0]);
openDoc.saveAndClose();
const blobPDF= newTempFile.getAs(MimeType.PDF);
const pdfFile= pdfFolder.createFile(blobPDF).setName(info['Name'][0]+ "_Application for the post" );
tempFolder.removeFile(newTempFile);
return pdfFile;
}
Now I am looking for the solution so that the image submitted by the respondent on the google form should be inserted in the same document and an auto mail be sent to the respondent.
Similar problem is also asked here
Upvotes: 0
Views: 458
Reputation: 26836
Assuming that the URL to the image is contained in the response to the question "Image" and that the placeholder in the template is called "{Image}", modify function createPdf
as following:
function createPDF(info){
...
const body=openDoc.getBody();
body.replaceText("{Fn}", info['Name'][0]);
var imageUrl = info['Image'][0];
var imagePlaceholder = "{Image}";
var id = imageUrl.match(/[\w\_\-]{25,}/)[0];
var img = DriveApp.getFileById(id);
var position = body.findText(imagePlaceholder);
var element = position.getElement();
element.asText().setText("");
element.getParent().asParagraph().insertInlineImage(0, img.getBlob());
openDoc.saveAndClose();
...
}
What this code does is:
In other words, a text placeholder gets replaced by an image.
Upvotes: 2