如何生成以 Google 形式提交的图像回复的 PDF 并将带有 PDF 附件的自动电子邮件发送给回复者

How to Generate PDF of an image response submitted in Google form and send Auto email to the respondent with attachment as PDF

我正在开发一个 google 表单,其中包含一些文本字段和图像字段。现在提交后,我可以生成文本回复的 pdf 文件。回复的pdf也会发送到回复者邮箱。

我已经提到这个 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;    
}

现在我正在寻找解决方案,以便将受访者在 google 表单中提交的图像插入到同一文档中,并自动发送邮件给受访者。

类似的问题也有人问here

为图像创建一个占位符,并通过从上传图像的 url 中检索到的实际图像 blob 替换它

假设图片的URL包含在对“图片”问题的回答中,模板中的占位符称为“{Image}”,修改function createPdf如下:

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();
    ...   
}

这段代码的作用是:

  • 本地化上传图片对应的命名值URL
  • 使用 DriveApp
  • 打开 URL 获取图像 blob
  • 在您的模板中找到为图片保留的占位符
  • 用空字符串替换文本
  • 检索包含占位符的元素
  • 获取元素的父段落
  • 将图像 blob 插入父段落

换句话说,文本占位符被图像取代。