Add images to Google Document via Google Apps Script

ehfeng picture ehfeng · Oct 26, 2011 · Viewed 21.6k times · Source

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

Answer

Eduardo picture Eduardo · Nov 1, 2011

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.