Insert an Image in a Google Document

332 views Asked by At

I create this script to insert a simple picture into a simple Google Document:

function myInsertImage() {
  var MyImage = "https://de.wikipedia.org/wiki/L%C3%B6we#/media/File:Lion_waiting_in_Namibia.jpg";

  var doc = DocumentApp.getActiveDocument();
  doc.getBody().insertImage(0, MyImage);
}

I get always get this error:

"Method insertImage(number, string) not found"

When I changed insertImage to appendImage, it has the same problem.

Does anyone know where is my fault?

1

There are 1 answers

2
Jessica Rodriguez On

You can try it like this using Google Apps Script:

function myInsertImage() {
  var fileId = "123456789";
  var myImage = DriveApp.getFileById(fileId).getBlob(); 

               or 

                DriveApp.getFileById(fileId).getBlob().getAs('img/png');

  var doc = DocumentApp.getActiveDocument();
  doc.getBody().insertImage(0, myImage);
}

or

function myInsertImage() {
    var url = "https://de.wikipedia.org/wiki/L%C3%B6we#/media/File:Lion_waiting_in_Namibia.jpg";
    var img = UrlFetchApp.fetch(url);
    DocumentApp.getActiveDocument().getBody().insertImage(0, img.getBlob());

}

This will insert an image at the top of the document.

For more infos: