使用GAS将表单上传的图片嵌入到Google文档中

时间:2018-08-18 20:38:06

标签: google-apps-script

一段时间以来,我一直在努力寻找答案...

我有一个Google表单(在此示例中,我要说这是车辆的检查清单)。因此,我填写了表格,答案转到工作表,工作表中的应用程序脚本制作了doc模板的副本,替换了关键字,然后保存并通过电子邮件发送pdf。绝对可以。

我正在尝试通过表单添加图像上传作为一些答案。图像上传驱动良好,但我不知道要在应用脚本中放入什么内容以使图像嵌入到doc中,以便图像通过电子邮件发送到pdf上。现在,它仅提供指向该图像的驱动器链接。我希望我有道理。

// Get template from Google Docs and name it
var docTemplate = "doc ID"; 
var docName = "Vehicle check with images";

// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var email_address = "myemailaddress@here.com";
var vehicle_vrn = e.values[1];
var front_desc = e.values[2];
var front_image = e.values[3];
var rear_desc = e.values[4];
var rear_image = e.values[5];
var driver_desc = e.values[6];
var driver_image = e.values[7];
var passenger_desc = e.values[8];
var passenger_image = e.values[9];



// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+vehicle_vrn)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();

// Replace place holder keys,in our google doc template
copyBody.replaceText('keyVrn', vehicle_vrn);
copyBody.replaceText('keyFrontdesc', front_desc);
copyBody.replaceText('keyFrontimage', front_image);
copyBody.replaceText('keyReardesc', rear_desc);
copyBody.replaceText('keyRearimage', rear_image);
copyBody.replaceText('keyDriversdesc', driver_desc);
copyBody.replaceText('keyDriversimage', driver_image);
copyBody.replaceText('keyPassdesc', passenger_desc);
copyBody.replaceText('keyPassimage', passenger_image);


// Save and close the temporary document
copyDoc.saveAndClose();

// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email
var subject = "V Check with images concept";
var body = "Please find attached the completed vehicle check with images for " + vehicle_vrn + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}

1 个答案:

答案 0 :(得分:0)

我对您的情况了解如下。

  • 使用Google表格上传文件时,该文件会上传到Google云端硬盘,因此可以将文件https://drive.google.com/open?id=### fileId ###之类的URL检索为事件对象。
    • 在此脚本中,此类URL用于从文件中检索blob。
  • 假设从您的问题和脚本中,要替换的图像的位置和数量如下。
    • keyVrnkeyFrontdesckeyFrontimagekeyReardesckeyRearimagekeyDriversdesckeyDriversimage,{{1 }}和keyPassdesc。而且每个值都只有一个。
    • keyPassimagefront_imagerear_imagedriver_image是上传的文件。您要从passenger_imagekeyFrontimagekeyRearimagekeyDriversimage替换为它们。
  • 您要将包含替换的文本和图像的文档作为PDF文件发送。

如果我的理解是正确的,那么该修改如何?我认为您的情况有几个答案。因此,请将此视为其中之一。

修改点:

  • 尽管可以使用keyPassimage替换文本值,但是不能使用文本将其替换为图像。因此,我为此使用了以下流程。
    1. 使用findText()查找要替换的文本值。
    2. 使用setText()删除文本。
    3. 使用insertInlineImage()将图像插入已删除的文本中。

反映以上几点的脚本如下。

修改后的脚本:

replaceText()

参考文献:

如果我误解了您的问题,请告诉我。我想修改它。

编辑:

要调整插入图像的大小时,请进行以下修改。在此修改中,图像被转换为​​300像素。请根据您的情况进行修改。

发件人:

// Get template from Google Docs and name it
var docTemplate = "doc ID"; 
var docName = "Vehicle check with images";

function onFormSubmit(e) {
  var replaceTextToImage = function(body, searchText, fileId) {
    var blob = DriveApp.getFileById(fileId).getBlob();
    var r = body.findText(searchText).getElement();
    r.asText().setText("");
    r.getParent().asParagraph().insertInlineImage(0, blob);
  }

  //Get information from form and set as variables
  var email_address = "myemailaddress@here.com";
  var vehicle_vrn = e.values[1];
  var front_desc = e.values[2];
  var front_image = e.values[3].split("=")[1];
  var rear_desc = e.values[4];
  var rear_image = e.values[5].split("=")[1];
  var driver_desc = e.values[6];
  var driver_image = e.values[7].split("=")[1];
  var passenger_desc = e.values[8];
  var passenger_image = e.values[9].split("=")[1];

  // Get document template, copy it as a new temp doc, and save the Doc’s id
  var copyId = DriveApp.getFileById(docTemplate)
  .makeCopy(docName+' for '+vehicle_vrn)
  .getId();
  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);
  // Get the document’s body section
  var copyBody = copyDoc.getBody();

  copyBody.replaceText('keyVrn', vehicle_vrn);
  copyBody.replaceText('keyFrontdesc', front_desc);
  replaceTextToImage(copyBody, 'keyFrontimage', front_image);
  copyBody.replaceText('keyReardesc', rear_desc);
  replaceTextToImage(copyBody, 'keyRearimage', rear_image);
  copyBody.replaceText('keyDriversdesc', driver_desc);
  replaceTextToImage(copyBody, 'keyDriversimage', driver_image);
  copyBody.replaceText('keyPassdesc', passenger_desc);
  replaceTextToImage(copyBody, 'keyPassimage', passenger_image);

  copyDoc.saveAndClose();
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
  var subject = "sample attachment file";
  var body = "sample text: " + vehicle_vrn + "";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
  DriveApp.getFileById(copyId).setTrashed(true);
}

收件人:

  var replaceTextToImage = function(body, searchText, fileId) {
    var blob = DriveApp.getFileById(fileId).getBlob();
    var r = body.findText(searchText).getElement();
    r.asText().setText("");
    r.getParent().asParagraph().insertInlineImage(0, blob);
  }