Reputation: 61
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
Reputation: 5163
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.
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();
}
Upvotes: 1