使用GAS将表单上传的图片嵌入到Google文档中 [英] Embed image uploaded by form to google doc using GAS

查看:51
本文介绍了使用GAS将表单上传的图片嵌入到Google文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to find the answer to this for a while...

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

I have a google form (for this example I am going to say it is a check list for a vehicle). So I fill in the form, answers go to a sheet and app script within the sheet makes a copy of a doc template, replaces keywords and then saves and emails a pdf. This works absolutely fine.

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

I am trying to add in image upload by form as a few of the answers. The image uploads to drive fine but i dont know what to put in the app script to make the image embed into the doc so that the image is present on the pdf that is emailed. right now it just provides a drive link to the image. I hope i made sense.

// 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);
}

推荐答案

我了解您的情况如下.

  • 使用Google表格上传文件时,该文件会上传到Google云端硬盘,文件的网址如 https://drive.google.com/open?id=### fileId ###可以作为事件对象进行检索.
    • 在此脚本中,此类URL用于从文件中检索blob.
    • When a file is uploaded using Google Form, the file is uploaded to Google Drive, the URL of the file like https://drive.google.com/open?id=### fileId ### can be retrieved as the event object.
      • In this script, such URL is used for retrieving the blob from the file.
      • keyVrn keyFrontdesc keyFrontimage keyReardesc keyRearimage ,文档中的 keyDriversdesc keyDriversimage keyPassdesc keyPassimage .而且每个值都只有一个.
      • front_image rear_image driver_image passenger_image 是上载的文件.您要从 keyFrontimage keyRearimage keyDriversimage keyPassimage 替换为它们.
      • There are keyVrn, keyFrontdesc, keyFrontimage, keyReardesc, keyRearimage, keyDriversdesc, keyDriversimage, keyPassdesc and keyPassimage in the document. And each value is only one.
      • front_image, rear_image, driver_image and passenger_image are the uploaded files. You want to replace from keyFrontimage, keyRearimage, keyDriversimage and keyPassimage to them.

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

      If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.

      • 尽管可以使用 replaceText()替换文本值,但是不能使用文本将其替换为图像.因此,我为此使用了以下流程.
      • Although the text value can be replaced using replaceText(), the text cannot be replaced to an image using it. So I used the following flow for this.
      1. 使用findText()查找要替换的文本值.
      2. 使用setText()删除文本.
      3. 使用insertInlineImage()将图像插入已删除的文本中.

    • 反映以上几点的脚本如下.

      The script which reflected above points is as follows.

      // 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);
      }
      

      参考文献:

      • replaceText()
      • findText()
      • setText()
      • insertInlineImage()
      • 如果我误解了您的问题,请告诉我.我想修改它.

        If I misunderstand your question, please tell me. I would like to modify it.

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

        When you want to resize the inserted image, please modify as follows. In this modification, the image is converted to 300 pixel. Please modify this for your situation.

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

        收件人:

          var replaceTextToImage = function(body, searchText, fileId) {
            var width = 300; // Please set this.
            var blob = DriveApp.getFileById(fileId).getBlob();
            var r = body.findText(searchText).getElement();
            r.asText().setText("");
            var img = r.getParent().asParagraph().insertInlineImage(0, blob);
            var w = img.getWidth();
            var h = img.getHeight();
            img.setWidth(width);
            img.setHeight(width * h / w);
          }
        

        这篇关于使用GAS将表单上传的图片嵌入到Google文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆