My goal is to request an image from an endpoint that returns its contents (i.e a blob) and insert it to a cell. Note that the image has not been persisted anywhere, so it does not have a URL. In case I were a JavaScript web developer, I could use an Image object and set its src to the result of URL.createObjectURL(...). But I need to solve this in a Google Script, based on JS.
In the Google Script docs there is this example:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
sheet.insertImage(blob, 1, 1);
I would expect that this code gets the image from the endpoint and inserts it in cell A1:
function onOpen() {
var SPREADSHEET_URL = 'MY_SPREADSHEET_URL';
var SHEET_NAME = 'SHEET_NAME';
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = ss.getSheetByName(SHEET_NAME);
var response = UrlFetchApp.fetch(
"ENDPOINT_URL").getContent();
var image = Utilities.newBlob(response, "image/jpeg", "img.jpeg");
sheet.insertImage(image, 1, 1);
}
However, I get an error saying that the type of my image variable (that is Blob) it is not supported.
The blob format is unsupported. at onOpen.
The official docs say that the method newBlob returns a Blob object. What am I missing?
The blob format is unsupported., the retrieved data from the URL ofENDPOINT_URLmight not be the image data. So, in your situation, can I ask you about whetherENDPOINT_URLis the direct link of the image? - Tanaike"ENDPOINT_URL"so I don't think you can get an answer without reaviling some more info about it. - soMario