Add images to Google Document via Google Apps Scri

2019-01-17 17:18发布

问题:

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

回答1:

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.