This code works for inserting an image
But I want to fetch the url from a google sheet. I put "UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()");"
in the code below to show my thinking on how to solve this...
function insertImage() {
// Retrieve an image from the web.
var resp = UrlFetchApp.fetch("sourceSheet.getActiveCell().getValue()");
// Create a document.
var doc = DocumentApp.openById("1qWnGAR_WBpHkSdY5frld0VNfHtQ6BSzGxlzVNDi5xMk");
// Append the image to the first paragraph.
doc.getChild(2).asParagraph().appendInlineImage(resp);
}
The end goal is that I have a sheet with the ID of the doc in one column and the ulr of the image to insert in the other column. I want the script to run so that I can insert each image into each doc.
Here's a possible solution. Of course you have to replace the Document and Spreadsheet ids with your own document ids.
function insertImage() {
// Get the target document.
var doc = DocumentApp.openById("1DeAGfM1PXXXXXXXXXXXXXXXXXXwjjeUhhZTfpo");
// Get the Spreadsheet where the url is defined
var sheet = SpreadsheetApp.openById("0Agl8XXXXXXXXXXXXXXXXXXXXXXXXXRScWU2TlE");
// Get the url from the correct celll
var url = sheet.getRange("A1").getValue();
// Retrieve an image from the web.
var resp = UrlFetchApp.fetch(url);
// Append the image to the first paragraph.
doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With