Search code examples
google-apps-scriptgoogle-docs

Embed image uploaded by form to google doc using GAS


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

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.

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

Solution

  • I understand about your situation as follows.

    • 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.
    • It supposes that from your question and your script, the place and number of images you want to replace are as follows.
      • 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.
    • You want to send the document including the replaced text and images as a PDF file.

    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.

    Modification points :

    • 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. Find the text value you want to replace using findText().
      2. Delete the text using setText().
      3. Insert an image to the deleted text using insertInlineImage().

    The script which reflected above points is as follows.

    Modified script :

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

    References :

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

    Edit :

    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.

    From :

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

    To :

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