google-sheetsgoogle-apps-scriptgoogle-docs

Is it possible get Image from Google Sheets to Google Docs Template when creating


This is my google sheet image.

enter image description here

I want when script creating from template import to here is it possible?

enter image description here

The code on here I am using for creating the google docs

// extract values from array of data
      var date_time = data[i][0];
      convert_time = Utilities.formatDate(new Date(date_time), "GMT+8", "E | dd/MMM/yyyy | hh:mm:ss a");
      var emp_name = data[i][1];
      var emp_id = data[i][2];
      var emp_dept = data[i][3];
      var emp_laptop = data[i][4];
      var emp_serialno = data[i][5];
      var emp_acc1 = data[i][6];
      var emp_acc2 = data[i][7];
      var emp_acc3 = data[i][8];
      var emp_acc4 = data[i][9];
      var emp_acc5 = data[i][10];
      var emp_sign = data[i][11];
      var it_sign = data[i][12];
      
// only proceed if got Body of new File
      if (openNewFile) {
        
        // replace tags with data from sheet
        newFileBody.replaceText('<<date-time>>', convert_time);
        newFileBody.replaceText('<<emp-name>>', emp_name);
        newFileBody.replaceText('<<emp-id>>', emp_id);
        newFileBody.replaceText('<<emp-dept>>', emp_dept);
        newFileBody.replaceText('<<emp-laptop>>', emp_laptop);
        newFileBody.replaceText('<<emp-serialnumber>>', emp_serialno);
        newFileBody.replaceText('<<emp-acc1>>', emp_acc1);
        newFileBody.replaceText('<<emp-acc2>>', emp_acc2);
        newFileBody.replaceText('<<emp-acc3>>', emp_acc3);
        newFileBody.replaceText('<<emp-acc4>>', emp_acc4);
        newFileBody.replaceText('<<emp-acc5>>', emp_acc5);
        newFileBody.replaceText('<<emp-sign>>', emp_sign);
        newFileBody.replaceText('<<it-sign>>', it_sign);

Solution

  • I thought that this answer might be useful for understanding for resolving your issue. Auto Transfer Google sheet generated QR code to Google Doc as Image When the script of this answer is reflected in your showing script, how about the following modification?

    Unfortunately, I cannot know your actual script. So, this is a simple modification. Please reflect this in your actual script.

    Modified script:

    Please add the following function to your script.

    // ref: https://stackoverflow.com/a/71151203/7108653
    var replaceTextToImage = function (body, searchText, url, width = 200) {
      var next = body.findText(searchText);
      if (!next) return;
      var r = next.getElement();
      r.asText().setText("");
      var img = r.getParent().asParagraph().insertInlineImage(0, UrlFetchApp.fetch(url).getBlob());
      if (width && typeof width == "number") {
        var w = img.getWidth();
        var h = img.getHeight();
        img.setWidth(width);
        img.setHeight(width * h / w);
      }
      return next;
    };
    

    And, please modify your script as follows.

    From:

    newFileBody.replaceText('<<emp-sign>>', emp_sign);
    newFileBody.replaceText('<<it-sign>>', it_sign);
    

    To:

    replaceTextToImage(newFileBody, '<<emp-sign>>', emp_sign);
    replaceTextToImage(newFileBody, '<<it-sign>>', it_sign);
    

    Note: