Skip to content

Instantly share code, notes, and snippets.

@rheajt
Created June 13, 2017 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rheajt/71f786d58e61dfab70586875cca7d774 to your computer and use it in GitHub Desktop.
Save rheajt/71f786d58e61dfab70586875cca7d774 to your computer and use it in GitHub Desktop.
google apps script code to append an image to a document with the file id
function onOpen() {
DocumentApp.getUi()
.createMenu('ID-TO-IMAGE')
.addItem('Run', 'runIdToImage')
.addToUi();
}
function runIdToImage() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var tables = body.getTables();
for(var i = 0; i < tables.length; i++) {
var idCell = tables[i]
.getRow(1)
.getCell(1);
var id = idCell.getText();
var image = DriveApp.getFileById(id)
.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var url = image.getUrl();
var appendedImage = idCell.clear().appendImage(image.getBlob());
var imageH = appendedImage.getHeight() / 10;
var imageW = appendedImage.getWidth() / 10;
appendedImage
.setHeight(imageH)
.setWidth(imageW)
.setLinkUrl(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment