16

How can you add images to a Google Document (not Spreadsheet or Presentation) via Google Apps Script. I don't see an addImage method. Surely the team would not have left this out.

http://code.google.com/googleapps/appsscript/class_document.html

ehfeng
  • 3,807
  • 4
  • 33
  • 42

1 Answers1

18

From the docs:

function insertImage() {
  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch("http://www.google.com/intl/en_com/images/srpr/logo2w.png");

  // Create a document.
  var doc = DocumentApp.openById("");

  // Append the image to the first paragraph.
  doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
}

http://code.google.com/googleapps/appsscript/class_documentapp_listitem.html#appendInlineImage

I'm not sure if you can upload an image. But you sure can insert it into a Paragraph via a url.

Eduardo
  • 22,574
  • 11
  • 76
  • 94
  • I was able to do this with a PNG, but not JPEGs for some reason. I filed a bug report for it. – Fred Feb 20 '13 at 13:42
  • @Frederic - I get it to work with all formats. see [example here](http://stackoverflow.com/questions/15461170/upload-an-image-to-a-google-spreadsheet/15474002#15474002) – Serge insas Mar 23 '13 at 23:17
  • @Eduardo - see other comment for an example of uploading an image to a doc via a form. – Serge insas Mar 23 '13 at 23:18
  • @Sergeinsas I never updated my comment, but I later realized it was because of file size. I resized all my photos and haven't had a problem with them since. – Fred Mar 24 '13 at 12:05