Victoria L.
Victoria L.

Reputation: 61

Creating a document full of images with google apps script

I am trying to use google apps script to make a google document full of images with the name of the image beneath each one. All of the images are in a google drive folder. Here is what I have tried:

function myFunction() {
  
  var doc = DocumentApp.create('This document is a test');
  var body = doc.getBody();
  
  var folderID = 'FOLDER_ID_HERE';
  var folder = DriveApp.getFolderById(folderID);
  var contents = folder.getFiles();
  
  var file;
  
  while (contents.hasNext()){
    var file = contents.next();
    var name = file.getName();
    var url = file.getUrl();
    body.appendParagraph(name);
    //Add image to document here somehow???
  }
  doc.saveAndClose();
}

What's the correct way to load an image from file here?

Upvotes: 0

Views: 603

Answers (1)

CMB
CMB

Reputation: 5163

Solution:

The body.appendImage(blob) method appends an image converted as a blob into Google Docs. Note that the references I have included at the bottom includes methods to adjust the size of the inline image returned by the appendImage() function in case your images do not fit into the document.

Sample Code:

function myFunction() {
  
  var doc = DocumentApp.create('This document is a test');
  var body = doc.getBody();
  
  var folderID = 'FOLDER-ID-HERE';
  var folder = DriveApp.getFolderById(folderID);
  var contents = folder.getFiles();

  while (contents.hasNext()){
    var file = contents.next();
    var name = file.getName();
    var url = file.getUrl();
    body.appendParagraph(name);
    body.appendImage(file.getBlob());
  }
  doc.saveAndClose();
}

Sample Doc:

enter image description here

References:

appendImage()

Inline Images

Upvotes: 1

Related Questions