Search code examples
google-apps-scriptgoogle-docs

Insert Image From Drive To Table in Google Document With Apps Script


I have a master document, where I want to change the contents of the document, including replacing certain text with images that I have in Drive.

To replace text, there is no problem, but I have difficulty when replacing Text in a table with an image that I have.

  function insertImage( ) {

  var image   = DriveApp.getFileById('1h293aKzW97sFqzRtREHp3hO_KV8czgKI').getBlob();  
  var copyId  = DriveApp.getFileById('1dY0-q_W-YO_R6WoOE6BqueN9qGFS06TPyrGWGeqChfY')
           .makeCopy('Application'+'_2020-')
           .getId();

 // get the body 
 var copyBody = DocumentApp.openById(copyId).getBody();

// find the position where to add the image
var position = copyBody.findText('<<MY IMAGE>>').getElement();
var offset = copyBody.getChildIndex(position.getParent())

// add the image below and resize it

var insertedImage = copyBody.insertImage(offset + 1,image).setHeight(100).setWidth(100)

// align it to the right 

var styles = {};
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = 
DocumentApp.HorizontalAlignment.RIGHT;
insertedImage.getParent().setAttributes(styles)

// remove the keyphoto placeholder text
position.removeFromParent()
}

I use that script from this Reference After Excecution, I have error notification : Error
Exception: Element does not contain the specified child element.

I want to insert image to the table with replace Text MY IMAGE and place to the table : enter image description here

I am greatful for any help.


Solution

  • Modification points:

    I thought that when I saw your showing script and your showing sample template Document, there were 2 modification points.

    1. In your script, a text of <<MY IMAGE>> is used as the search text. But, in your showing template document, <<MY IMAGE>> is not used. It seems that MY IMAGE is used. In this case, <<MY IMAGE>> cannot be found. This has already been mentioned in Yuri Khristich's comment. Ref

    2. From your showing template document, it seems that the placeholder of MY IMAGE is put into a table cell. In this case, I'm worried that your showing script cannot be used. I think that your showing script can be used as a placeholder in the independent paragraph without a table cell. But, unfortunately, in the case of the table cell, this cannot be used.

    When these points are reflected in your script, how about the following modification?

    Modified script:

    1. Please put a text of <<MY IMAGE>> to the template document. When your showing template document is modified, please replace MY IMAGE with <<MY IMAGE>>.

    2. Please modify your script as follows.

      function insertImage() {
        var image   = DriveApp.getFileById('1h293aKzW97sFqzRtREHp3hO_KV8czgKI').getBlob();
        var copyId  = DriveApp.getFileById('1dY0-q_W-YO_R6WoOE6BqueN9qGFS06TPyrGWGeqChfY').makeCopy('Application'+'_2020-').getId();
        var copyBody = DocumentApp.openById(copyId).getBody();
        var position = copyBody.findText('<<MY IMAGE>>').getElement();
        if (!position) {
          throw new Error("Placeholder of '<<MY IMAGE>>' is not found.");
        }
        var para = position.getParent().asParagraph();
        var insertedImage = para.appendInlineImage(image).setHeight(100).setWidth(100);
        var styles = { [DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]: DocumentApp.HorizontalAlignment.RIGHT };
        insertedImage.getParent().setAttributes(styles)
        para.removeChild(position);
      }
      

    Testing:

    When this script is run, the following result is obtained.

    enter image description here