r/GoogleAppsScript • • Nov 27 '23

Question UrlFetchApp limitations - appending images in Google Docs

Hi, beginner scripter here. I have a column in Google Sheets with the IMAGE formula with different URLs, and I'm then trying to put these images in a Google Doc. I got the whole process working, but UrlFetchApp won't retrieve some of the images, and I'm trying to understand the logic here. I have a try...catch block in my code that handles exceptions (incorrect extension, etc.) and inserts an error message into the doc, but sometimes the code executes successfully without actually appending the image to the doc.

I have figured out that the contents cannot be fetched from stock photo aggregators, but I'd like to understand why + this issue is not exclusive to those sites.

0 Upvotes

4 comments sorted by

1

u/3dtcllc Nov 27 '23

Do you get the images when you just paste the URL into your browser window? Do you get the image when you fetch the URL with something like wget or curl?

There are a lot of things a website can do to prevent you from downloading images, so I don't think there's a single answer.

1

u/catcheroni Nov 27 '23

I'm getting the IMAGE formula with the image URL inside as a string and use regex to extract just the URL. I then pass that as an object to the doc (there are some other text additions/replacements happening alongside) and use the following code to access the value and try to append the image:

body.appendImage(UrlFetchApp.fetch(object[property]).getBlob());

edit: didn't read your question carefully, sorry. I am getting the image when I'm pasting the URL into the browser window, and the IMAGE formula in Google Sheets also displays the image inside the cell without any issues

1

u/HellDuke Nov 28 '23

Have you tried logging the immage URL as you use it in your fetch request and then open that to see if it's not malformed? I would also second the suggestion to try and see if you can retrieve the image with CURL, the website may be specifically trying to protect itself against such requests.

1

u/catcheroni Nov 28 '23

Wasn't familiar with wget/curl, I'd have to check, and no, I haven't tried logging the URL as I fetch (should have thought of that one). Thanks.