Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Image (from URL) from Google Sheet Cell to Google Doc

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.

like image 226
Timothy Kenny Avatar asked Dec 26 '22 18:12

Timothy Kenny


1 Answers

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());
}
like image 135
Eduardo Avatar answered Jan 17 '23 10:01

Eduardo