Skip to content

Instantly share code, notes, and snippets.

@pdparker
Last active May 26, 2020 22:11
Show Gist options
  • Save pdparker/af83a78b3c96def30bf06e34ef4bf1f4 to your computer and use it in GitHub Desktop.
Save pdparker/af83a78b3c96def30bf06e34ef4bf1f4 to your computer and use it in GitHub Desktop.
Inserts an image from url and attaches the url to the image
// Set Globals
var ui = DocumentApp.getUi();
/** Demonstrates a simple way to prompt the user for a date. */
function img_workflow() {
// Set UI prompt + reminder to use unsecured image
var url = ui.prompt('Enter URL of image', 'use http not https', ui.ButtonSet.OK_CANCEL);
var line = url.getResponseText();
// If user clicks OK
if (url.getSelectedButton() == ui.Button.OK) {
// fetch image url
var response = UrlFetchApp.fetch(line);
var binaryData = response.getContent();
var blob = Utilities.newBlob(binaryData, 'image/png','myImage');
// Get cursor position
var doc = DocumentApp.getActiveDocument();
var cursor = doc.getCursor();
// Insert image
var img = cursor.insertInlineImage(blob);
// set URL of image
img.setLinkUrl(line);
// Exit
} else if (url.getSelectedButton() == ui.Button.CANCEL) {
Logger.log('The user canceled the dialog.');
} else {
Logger.log('The user closed the dialog.');
}
}
/** Adds a menu to the Google Sheet. */
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Inset Image Workflow', 'img_workflow')
.addToUi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment