Reputation: 2660
I have a list of images I reference using their filenames:
=image("domainname.com/somepath/"&A2&".jpg")
where in A2 is a filename.
Unfortunately some of these images have .png extension. I try to find a solution to check if file with jpg extension is correct and image is found. If not found I want to use
=image("domainname.com/somepath/"&a2&".png")
For this cell.
IFNA and IFERROR formulas do not work. I tried also to concatenate formulas:
=image("domainname.com/somepath/"&a2&".png")&image("domainname.com/somepath/"&a2&".jpg")
but I see that you can use & only for strings
There are thousands of images in different folders for reference and I cannot control their format. Do you have any idea?
Upvotes: 1
Views: 1112
Reputation: 5963
One workaround I would suggest is to create and use a custom function to validate the image URL.
Sample Custom Function:
/**
* validate image
*
* @param {input} image url to validate.
*
* @customfunction
*/
function IMAGE_TRY(input) {
try{
var response = UrlFetchApp.fetch(input);
return 1;
} catch(err) {
return 0;
}
}
Note that the @customfunction tag is important in the function comment so that this custom function will appear as a suggested formula when you are typing in a cell.
Once you have created a custom function, you can use this to validate the URL and use the correct image URL using IF function.
Sample:
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
Sample Output:
(UPDATE)
OPTIMIZED SOLUTION:
Sample Custom Function:
/**
* get image url with correct file extension.
*
* @param {input} image url string without extension.
*
* @customfunction
*/
function getURL(input) {
try{
var response = UrlFetchApp.fetch(input+".jpg");
return (input.concat(".jpg"));
} catch(err) {
return (input.concat(".png"));
}
}
Sheets Formula:
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
Upvotes: 2